From 63ba9f4fb9a6a963d2669f0bb6e9e4b9090f48a7 Mon Sep 17 00:00:00 2001 From: Trevor Buckner Date: Sat, 10 Jul 2021 19:01:27 -0400 Subject: [PATCH] Change {{ span to Marked.js extension --- shared/naturalcrit/markdown.js | 84 ++++++++++++++++++++++------------ 1 file changed, 56 insertions(+), 28 deletions(-) diff --git a/shared/naturalcrit/markdown.js b/shared/naturalcrit/markdown.js index 317c8fdc0..3bc6c7693 100644 --- a/shared/naturalcrit/markdown.js +++ b/shared/naturalcrit/markdown.js @@ -19,12 +19,62 @@ renderer.paragraph = function(text){ let match; if(text.startsWith('${text}

\n`; }; +const mustacheSpans = { + name: 'mustacheSpans', + level: 'inline', // Is this a block-level or inline-level tokenizer? + start(src) { return src.match(/{{[^{]/)?.index; }, // Hint to Marked.js to stop and check for a match + tokenizer(src, tokens) { + const completeSpan = /^{{[^\n]*}}/; // Regex for the complete token + const inlineRegex = /{{(?:="[\w,\-. ]*"|[^"'{}}\s])*\s*|}}/g; + const match = completeSpan.exec(src); + if (match) { + //Find closing delimiter + let blockCount = 0; + let tags = ''; + let endIndex = 0; + let delim; + while (delim = inlineRegex.exec(match[0])) { + if(delim[0].startsWith('{{')) { + tags = tags || ' ' + processStyleTags(delim[0].substring(2)); + blockCount++; + } + else if(delim[0] == '}}' && blockCount !== 0) { + blockCount--; + if(blockCount == 0) { + endIndex = inlineRegex.lastIndex; + break; + } + } + } + + if(endIndex) { + const raw = src.slice(0, endIndex); + const text = raw.slice((raw.indexOf(' ')+1) || -2, -2) + + return { // Token to generate + type: 'mustacheSpans', // Should match "name" above + raw: raw, // Text to consume from the source + text: text, // Additional custom properties + tags: tags, + tokens: this.inlineTokens(text) // inlineTokens to process **bold**, *italics*, etc. + } + } + } + }, + renderer(token) { + console.log(token); + return `{ - - if(splitText) r.push(Markdown.parseInline(splitText, { renderer: renderer })); - - const block = matches[matchIndex] ? matches[matchIndex].trimLeft() : ''; - if(block && block.startsWith('{{')) { - const values = processStyleTags(block.substring(2)); - r.push(`tag.replace(/="(.*)"/g, ':$1;')); - return `${classes.join(' ')}" ${id ? `id="${id}"` : ''} ${styles ? `style="${styles.join(' ')}"` : ''}`; + return `${classes.join(' ')}" ${id ? `id="${id}"` : ''} ${styles.length ? `style="${styles.join(' ')}"` : ''}`; }; module.exports = {