discourse/app/assets/javascripts/pretty-text/engines/discourse-markdown/helpers.js.es6
Sam Saffron d0c5205a52 Feature: Change markdown engine to markdown it
This commit removes the old evilstreak markdownjs engine.

- Adds specs to WhiteLister and changes it to stop using globals
    (Fixes large memory leak)
- Fixes edge cases around bbcode handling
- Removes mdtest which is no longer valid (to be replaced with
    CommonMark)
- Updates MiniRacer to correct minor unmanaged memory leak
- Fixes plugin specs
2017-07-17 11:41:34 -04:00

61 lines
1.8 KiB
JavaScript

// 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
// });
// based off https://github.com/markdown-it/markdown-it-emoji/blob/master/dist/markdown-it-emoji.js
//
export function textReplace(state, callback, skipAllLinks) {
var i, j, l, tokens, token,
blockTokens = state.tokens,
linkLevel = 0;
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];
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; }
}
}
if (token.type === 'text' && linkLevel === 0) {
let split;
if(split = callback(token.content, state)) {
// replace current node
blockTokens[j].children = tokens = state.md.utils.arrayReplaceAt(
tokens, i, split
);
}
}
}
}
}