Commit 75cca704 authored by Enrique Alcántara's avatar Enrique Alcántara

Merge branch '232567-templater-exclude-pre-existing-codeblocks' into 'master'

Templater update to handle html and inline erb

Closes #232567

See merge request gitlab-org/gitlab!38834
parents 2312de0a fde129f2
/**
* 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 ticks = '```';
const marker = 'sse'; 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 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 postfix = `\n${ticks}`; const wrapPostfix = `\n${ticks}`;
const flagPrefix = `${marker}-${Date.now()}`; const markPrefix = `${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);
if (matches) { const reHelpers = {
matches.forEach(match => { template: `.| |\\t|\\n(?!(\\n|${markPrefix}))`,
const initial = match.replace(`${prefix}`, '').replace(`${postfix}`, ''); openTag: '<[a-zA-Z]+.*?>',
text = text.replace(match, initial); 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 text = source;
let id = 0; let id = 0;
const hash = {}; const hash = {};
patterns.forEach(pattern => { Object.entries(groups).forEach(([groupKey, group]) => {
group.forEach(pattern => {
const matches = text.match(pattern); const matches = text.match(pattern);
if (matches) { if (matches) {
matches.forEach(match => { matches.forEach(match => {
const key = `${flagPrefix}${id}`; const key = `${markPrefix}-${groupKey}-${id}`;
text = text.replace(match, key); text = text.replace(match, key);
hash[key] = match; hash[key] = match;
id += 1; id += 1;
}); });
} }
}); });
});
return { text, hash }; return { text, hash };
}; };
const wrap = source => { const unmark = (text, hash) => {
const { text, hash } = flag(unwrap(source), orderedPatterns); let source = text;
let wrappedSource = text;
Object.entries(hash).forEach(([key, value]) => { 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 }; 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. ...@@ -30,6 +30,15 @@ Below this line is a block of HTML.
<h1>Heading</h1> <h1>Heading</h1>
<p>Some paragraph...</p> <p>Some paragraph...</p>
</div> </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. 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. ...@@ -69,6 +78,15 @@ Below this line is a block of HTML.
<p>Some paragraph...</p> <p>Some paragraph...</p>
</div> </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` 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