Commit fde129f2 authored by derek-knox's avatar derek-knox

Templater update to handle html and inline erb

Update templater pre-processing approach to
flag (and thus mitigate) duplicate matches
during the "flag content as code" step
parent d429e468
/**
* The purpose of this file is to modify Markdown source such that templated code (embedded ruby currently) can be temporarily wrapped and unwrapped in codeblocks:
* 1. `wrap()`: temporarily wrap in codeblocks (useful for a WYSIWYG editing experience)
* 2. `unwrap()`: undo the temporarily wrapped codeblocks (useful for Markdown editing experience and saving edits)
*
* Without this `templater`, the templated code is otherwise interpreted as Markdown content resulting in loss of spacing, indentation, escape characters, etc.
*
*/
const ticks = '```';
const marker = 'sse';
const prefix = `${ticks} ${marker}\n`; // Space intentional due to https://github.com/nhn/tui.editor/blob/6bcec75c69028570d93d973aa7533090257eaae0/libs/to-mark/src/renderer.gfm.js#L26
const postfix = `\n${ticks}`;
const flagPrefix = `${marker}-${Date.now()}`;
const template = `.| |\\t|\\n(?!(\\n|${flagPrefix}))`;
const templatedRegex = new RegExp(`(^${prefix}(${template})+?${postfix}$)`, 'gm');
const nonErbMarkupRegex = new RegExp(`^((<(?!%).+>){1}(${template})+(</.+>){1})$`, 'gm');
const embeddedRubyBlockRegex = new RegExp(`(^<%(${template})+%>$)`, 'gm');
const embeddedRubyInlineRegex = new RegExp(`(^.*[<|&lt;]%(${template})+$)`, 'gm');
// Order is intentional (general to specific) where HTML markup is flagged first, then ERB blocks, then inline ERB
// Order in combo with the `flag()` algorithm is used to mitigate potential duplicate pattern matches (ERB nested in HTML for example)
const orderedPatterns = [nonErbMarkupRegex, embeddedRubyBlockRegex, embeddedRubyInlineRegex];
const unwrap = source => {
let text = source;
const matches = text.match(templatedRegex);
const wrapPrefix = `${ticks} ${marker}\n`; // Space intentional due to https://github.com/nhn/tui.editor/blob/6bcec75c69028570d93d973aa7533090257eaae0/libs/to-mark/src/renderer.gfm.js#L26
const wrapPostfix = `\n${ticks}`;
const markPrefix = `${marker}-${Date.now()}`;
if (matches) {
matches.forEach(match => {
const initial = match.replace(`${prefix}`, '').replace(`${postfix}`, '');
text = text.replace(match, initial);
});
}
const reHelpers = {
template: `.| |\\t|\\n(?!(\\n|${markPrefix}))`,
openTag: '<[a-zA-Z]+.*?>',
closeTag: '</.+>',
};
const reTemplated = new RegExp(`(^${wrapPrefix}(${reHelpers.template})+?${wrapPostfix}$)`, 'gm');
const rePreexistingCodeBlocks = new RegExp(`(^${ticks}.*\\n(.|\\s)+?${ticks}$)`, 'gm');
const reHtmlMarkup = new RegExp(
`^((${reHelpers.openTag}){1}(${reHelpers.template})*(${reHelpers.closeTag}){1})$`,
'gm',
);
const reEmbeddedRubyBlock = new RegExp(`(^<%(${reHelpers.template})+%>$)`, 'gm');
const reEmbeddedRubyInline = new RegExp(`(^.*[<|&lt;]%(${reHelpers.template})+$)`, 'gm');
return text;
const patternGroups = {
ignore: [rePreexistingCodeBlocks],
// Order is intentional (general to specific) where HTML markup is marked first, then ERB blocks, then inline ERB
// Order in combo with the `mark()` algorithm is used to mitigate potential duplicate pattern matches (ERB nested in HTML for example)
allow: [reHtmlMarkup, reEmbeddedRubyBlock, reEmbeddedRubyInline],
};
const flag = (source, patterns) => {
const mark = (source, groups) => {
let text = source;
let id = 0;
const hash = {};
patterns.forEach(pattern => {
const matches = text.match(pattern);
if (matches) {
matches.forEach(match => {
const key = `${flagPrefix}${id}`;
text = text.replace(match, key);
hash[key] = match;
id += 1;
});
}
Object.entries(groups).forEach(([groupKey, group]) => {
group.forEach(pattern => {
const matches = text.match(pattern);
if (matches) {
matches.forEach(match => {
const key = `${markPrefix}-${groupKey}-${id}`;
text = text.replace(match, key);
hash[key] = match;
id += 1;
});
}
});
});
return { text, hash };
};
const wrap = source => {
const { text, hash } = flag(unwrap(source), orderedPatterns);
const unmark = (text, hash) => {
let source = text;
let wrappedSource = text;
Object.entries(hash).forEach(([key, value]) => {
wrappedSource = wrappedSource.replace(key, `${prefix}${value}${postfix}`);
const newVal = key.includes('ignore') ? value : `${wrapPrefix}${value}${wrapPostfix}`;
source = source.replace(key, newVal);
});
return wrappedSource;
return source;
};
const unwrap = source => {
let text = source;
const matches = text.match(reTemplated);
if (matches) {
matches.forEach(match => {
const initial = match.replace(`${wrapPrefix}`, '').replace(`${wrapPostfix}`, '');
text = text.replace(match, initial);
});
}
return text;
};
const wrap = source => {
const { text, hash } = mark(unwrap(source), patternGroups);
return unmark(text, hash);
};
export default { wrap, unwrap };
---
title: Add pre-processing step so preexisting codeblocks are preserved prior to flagging content as code in the static site editor's WYSIWYG mode.
merge_request: 38834
author:
type: added
......@@ -30,6 +30,15 @@ Below this line is a block of HTML.
<h1>Heading</h1>
<p>Some paragraph...</p>
</div>
Below this line is a codeblock of the same HTML that should be ignored and preserved.
\`\`\` html
<div>
<h1>Heading</h1>
<p>Some paragraph...</p>
</div>
\`\`\`
`;
const sourceTemplated = `Below this line is a simple ERB (single-line erb block) example.
......@@ -69,6 +78,15 @@ Below this line is a block of HTML.
<p>Some paragraph...</p>
</div>
\`\`\`
Below this line is a codeblock of the same HTML that should be ignored and preserved.
\`\`\` html
<div>
<h1>Heading</h1>
<p>Some paragraph...</p>
</div>
\`\`\`
`;
it.each`
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment