Skip to content

Commit 694f199

Browse files
wip(docs): factorize evalHtmlComments tools
1 parent caf3e08 commit 694f199

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-21
lines changed

build/evalHtmlCommentsTools.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const regexpReservedChars = '\\.+*?^$|[{()';
2+
3+
const regexpReserved = new RegExp('([' + regexpReservedChars.split('').map(char => '\\'+char).join('') + '])', 'gu');
4+
5+
function regexpQuote(str) {
6+
7+
return str.replace(regexpReserved, '\\$1');
8+
}
9+
10+
function blockList(wholeContent) {
11+
12+
return [...wholeContent.matchAll(/<\!--(.*?)--\>/g)].map(e => e[1]);
13+
}
14+
15+
const replaceBlock = (currentContent, tag, content) => {
16+
17+
const block = [ `<\!--${ tag }--\>`, `<\!--/${ tag }--\>` ];
18+
const regexp = new RegExp(regexpQuote(block[0]) + '([ \\t]*\\r?\\n?)([^]*?)(\\s*)' + regexpQuote(block[1]), 'g');
19+
return currentContent.replace(regexp, (all, wsLeft, prevContent, wsRight) => block[0] + wsLeft + (typeof content === 'function' ? content(prevContent) : content) + wsRight + block[1]);
20+
}
21+
22+
23+
module.exports = {
24+
blockList,
25+
replaceBlock,
26+
}

docs/examples.md

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -690,30 +690,11 @@ In the following example we use a trick to preserve reactivity through the `Vue.
690690

691691
<!---
692692
693-
const regexpReservedChars = '\\.+*?^$|[{()';
694-
695-
const regexpReserved = new RegExp('([' + regexpReservedChars.split('').map(char => '\\'+char).join('') + '])', 'gu');
696-
697-
function regexpQuote(str) {
698-
699-
return str.replace(regexpReserved, '\\$1');
700-
}
701-
702-
function blockList(wholeContent) {
703-
704-
return [...wholeContent.matchAll(/<\!--(.*?)--\>/g)].map(e => e[1]);
705-
}
706-
707-
const replaceBlock = (currentContent, tag, content) => {
708-
709-
const block = [ `<\!--${ tag }--\>`, `<\!--/${ tag }--\>` ];
710-
const regexp = new RegExp(regexpQuote(block[0]) + '([ \\t]*\\r?\\n?)[^]*?(\\s*)' + regexpQuote(block[1]), 'g');
711-
return currentContent.replace(regexp, block[0] + '$1' + content + '$2' + block[1]);
712-
}
693+
const { blockList, replaceBlock } = require('./evalHtmlCommentsTools.js');
713694
714695
function ghAnchor(header) {
715696
716-
return header.trim().toLowerCase().replace(/[^\w\- ]+/g, '').replace(/\s/g, '-').replace(/\-+$/, '');
697+
return header.trim().toLowerCase().replace(/[^\w\- ]+/g, '').replace(/\s/g, '-').replace(/\-+$/, '');
717698
}
718699
719700
const toc = [...ctx.wholeContent.matchAll(/^(#{1,3}) ?([^#].*)$/mg)]

0 commit comments

Comments
 (0)