Commit 773784d4 authored by Jose Ivan Vargas's avatar Jose Ivan Vargas

Merge branch '232567-templater-inline-erb' into 'master'

Resolve "Custom renderers cleanup - erb"

See merge request gitlab-org/gitlab!38791
parents d70ffbc1 9f41ff2b
const marker = 'sse';
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 code = '.| |\\t|\\n(?!\\n)';
const templatedRegex = new RegExp(`(^${prefix}(${code})+${postfix}$)`, 'gm');
const embeddedRubyRegex = new RegExp(`(^<%(${code})+%>$)`, 'gm');
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);
if (matches) {
matches.forEach(match => {
const initial = match.replace(prefix, '').replace(postfix, '');
const initial = match.replace(`${prefix}`, '').replace(`${postfix}`, '');
text = text.replace(match, initial);
});
}
return text;
};
const flag = (source, patterns) => {
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;
});
}
});
return { text, hash };
};
const wrap = source => {
let text = unwrap(source);
const matches = text.match(embeddedRubyRegex);
if (matches) {
matches.forEach(match => {
text = text.replace(match, `${prefix}${match}${postfix}`);
});
}
return text;
const { text, hash } = flag(unwrap(source), orderedPatterns);
let wrappedSource = text;
Object.entries(hash).forEach(([key, value]) => {
wrappedSource = wrappedSource.replace(key, `${prefix}${value}${postfix}`);
});
return wrappedSource;
};
export default { wrap, unwrap };
---
title: Add pre-processing step so inline ERB and HTML syntax are wrapped in codeblocks for code vs. content editing in the static site editor's WYSIWYG mode.
merge_request: 38791
author:
type: added
......@@ -2,25 +2,42 @@
import templater from '~/static_site_editor/services/templater';
describe('templater', () => {
const source = `Some text
const source = `Below this line is a simple ERB (single-line erb block) example.
<% some erb code %>
Some more text
Below this line is a complex ERB (multi-line erb block) example.
<% if apptype.maturity && (apptype.maturity != "planned") %>
<% maturity = "This application type is at the \"#{apptype.maturity}\" level of maturity." %>
<% end %>
With even text with indented code above.
Below this line is a non-erb (single-line HTML) markup example that also has erb.
<a href="<%= compensation_roadmap.role_path %>"><%= compensation_roadmap.role_path %></a>
Below this line is a non-erb (multi-line HTML block) markup example that also has erb.
<ul>
<% compensation_roadmap.recommendation.recommendations.each do |recommendation| %>
<li><%= recommendation %></li>
<% end %>
</ul>
Below this line is a block of HTML.
<div>
<h1>Heading</h1>
<p>Some paragraph...</p>
</div>
`;
const sourceTemplated = `Some text
const sourceTemplated = `Below this line is a simple ERB (single-line erb block) example.
\`\`\` sse
<% some erb code %>
\`\`\`
Some more text
Below this line is a complex ERB (multi-line erb block) example.
\`\`\` sse
<% if apptype.maturity && (apptype.maturity != "planned") %>
......@@ -28,7 +45,30 @@ Some more text
<% end %>
\`\`\`
With even text with indented code above.
Below this line is a non-erb (single-line HTML) markup example that also has erb.
\`\`\` sse
<a href="<%= compensation_roadmap.role_path %>"><%= compensation_roadmap.role_path %></a>
\`\`\`
Below this line is a non-erb (multi-line HTML block) markup example that also has erb.
\`\`\` sse
<ul>
<% compensation_roadmap.recommendation.recommendations.each do |recommendation| %>
<li><%= recommendation %></li>
<% end %>
</ul>
\`\`\`
Below this line is a block of HTML.
\`\`\` sse
<div>
<h1>Heading</h1>
<p>Some paragraph...</p>
</div>
\`\`\`
`;
it.each`
......@@ -38,7 +78,7 @@ With even text with indented code above.
${'unwrap'} | ${sourceTemplated} | ${source}
${'unwrap'} | ${source} | ${source}
`(
'wraps $initial in a templated sse codeblock if $fn is wrap, unwraps otherwise',
'wraps $initial in a templated sse codeblocks if $fn is wrap, unwraps otherwise',
({ fn, initial, target }) => {
expect(templater[fn](initial)).toMatch(target);
},
......
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