2017-06-08 18:02:30 -04:00
|
|
|
// since the markdown.it interface is a bit on the verbose side
|
|
|
|
// we can keep some general patterns here
|
|
|
|
|
|
|
|
|
|
|
|
export default null;
|
|
|
|
|
|
|
|
// creates a rule suitable for inline parsing and replacement
|
|
|
|
//
|
|
|
|
// example:
|
|
|
|
// const rule = inlineRegexRule(md, {
|
|
|
|
// start: '#',
|
|
|
|
// matcher: /^#([\w-:]{1,101})/i,
|
|
|
|
// emitter: emitter
|
|
|
|
// });
|
2017-07-07 13:03:36 -04:00
|
|
|
|
2017-06-08 18:02:30 -04:00
|
|
|
|
|
|
|
// based off https://github.com/markdown-it/markdown-it-emoji/blob/master/dist/markdown-it-emoji.js
|
|
|
|
//
|
2017-07-07 13:03:36 -04:00
|
|
|
export function textReplace(state, callback, skipAllLinks) {
|
2017-06-08 18:02:30 -04:00
|
|
|
var i, j, l, tokens, token,
|
|
|
|
blockTokens = state.tokens,
|
2017-07-07 13:03:36 -04:00
|
|
|
linkLevel = 0;
|
2017-06-08 18:02:30 -04:00
|
|
|
|
|
|
|
for (j = 0, l = blockTokens.length; j < l; j++) {
|
|
|
|
if (blockTokens[j].type !== 'inline') { continue; }
|
|
|
|
tokens = blockTokens[j].children;
|
|
|
|
|
|
|
|
// We scan from the end, to keep position when new tags added.
|
|
|
|
// Use reversed logic in links start/end match
|
|
|
|
for (i = tokens.length - 1; i >= 0; i--) {
|
|
|
|
token = tokens[i];
|
|
|
|
|
2017-07-07 13:03:36 -04:00
|
|
|
if (skipAllLinks) {
|
|
|
|
if (token.type === 'link_open' || token.type === 'link_close') {
|
|
|
|
linkLevel -= token.nesting;
|
|
|
|
} else if (token.type === 'html_inline') {
|
|
|
|
if (token.content.substr(0,2).toLowerCase() === "<a") {
|
|
|
|
linkLevel++;
|
|
|
|
} else if (token.content.substr(0,4).toLowerCase() === "</a>") {
|
|
|
|
linkLevel--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (token.type === 'link_open' || token.type === 'link_close') {
|
|
|
|
if (token.info === 'auto') { linkLevel -= token.nesting; }
|
|
|
|
}
|
2017-06-08 18:02:30 -04:00
|
|
|
}
|
|
|
|
|
2017-07-07 13:03:36 -04:00
|
|
|
if (token.type === 'text' && linkLevel === 0) {
|
2017-06-08 18:02:30 -04:00
|
|
|
let split;
|
|
|
|
if(split = callback(token.content, state)) {
|
|
|
|
// replace current node
|
|
|
|
blockTokens[j].children = tokens = state.md.utils.arrayReplaceAt(
|
|
|
|
tokens, i, split
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|