UNPKG

markdown-it-flowdock

Version:

Flowdock hashtag and mention parser for markdown-it

72 lines (56 loc) 2.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function fixUrlsEndingInAParen(_x) { var _again = true; _function: while (_again) { var canidate = _x; _again = false; var matches = canidate.match(/(\(<[^>]+>\))|<([^>]+)>\)/); if (matches !== undefined && matches !== null) { var brokenLink = matches[2]; if (brokenLink !== undefined && brokenLink !== null) { var fixedCanidate = canidate.replace("<" + matches[2] + ">)", "<" + matches[2] + ")>"); _x = fixedCanidate; _again = true; matches = brokenLink = fixedCanidate = undefined; continue _function; } } return canidate; } } function replaceUrlTextWithAutoLinkUrl(text) { var urlMatcher = /([\x2D\.:_a-z\u017F\u212A]+:\/\/(?:[\0-\x08\x0E-\x1F!-\x9F\xA1-\u167F\u1681-\u180D\u180F-\u1FFF\u200B-\u2027\u202A-\u202E\u2030-\u205E\u2060-\u2FFF\u3001-\uD7FF\uE000-\uFEFE\uFF00-\uFFFF]|[\uD800-\uDBFF][\uDC00-\uDFFF]|[\uD800-\uDBFF](?![\uDC00-\uDFFF])|(?:[^\uD800-\uDBFF]|^)[\uDC00-\uDFFF])+[0-9A-JL-RT-Z_a-z]\x2D?\/?)/ig; var firstPass = text.replace(urlMatcher, "<$1>"); return fixUrlsEndingInAParen(firstPass).replace(/(<_([^>]+)_>)/, "_[$2]($2)_") // Fix for URLs included in _<url>_ .replace(/\[([^\]]+)\]\(<([^>]+)>\)/, "[$1]($2)") // Fix for URLs already in markdown syntax []() .replace(/<(onenote:[^>]+)>/, "[$1]($1)") // Fix for onenote urls .replace(/<<([^>]+)>>/ig, "<$1>"); // Fix for URLs already surrounded by <> } function dealWithCodeBlock(text) { var startOfCodeBlock = text.indexOf('`'); if (startOfCodeBlock === -1) { return replaceUrlTextWithAutoLinkUrl(text); } var frontPart = text.slice(0, startOfCodeBlock); var nextCodeBlockMarker = text.indexOf('`', startOfCodeBlock + 1); var endOfCodeBlock = nextCodeBlockMarker === -1 ? text.length : nextCodeBlockMarker + 1; var codeBlock = text.slice(startOfCodeBlock, endOfCodeBlock); return replaceUrlTextWithAutoLinkUrl(frontPart) + codeBlock + dealWithCodeBlock(text.slice(endOfCodeBlock)); } function flowdockUrl(state) { var tokens = state.tokens; for (var i = 0; i < tokens.length; i++) { var token = tokens[i]; if (token.type !== 'inline') { continue; } token.content = dealWithCodeBlock(token.content); } } exports["default"] = function (md, options) { md.core.ruler.after('block', 'flowdock-url', flowdockUrl); }; module.exports = exports["default"];