-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarkdown-it-dynamic.js
More file actions
61 lines (50 loc) · 1.74 KB
/
Copy pathmarkdown-it-dynamic.js
File metadata and controls
61 lines (50 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// this a dynamic text markdown parser for markdown-it that is mostly a fork
// of the '~' markdown-it-sub parser but to handle open/closing tags
//
// dynamic_open and dynamic_close are there in case we decide to do something with them,
// but for now they render noop in the actual editor (see dynamic-text.js)
let dynamicTextParser = (state, silent) => {
let UNESCAPE_MD_RE = /\\([\\!"#$%&'()*+,.\/:;<=>?@[\]^_`{|}~-])/g;
const max = state.posMax;
const start = state.pos;
if (state.src.charCodeAt(start) !== 123 /* { */) {
return false;
}
if (silent) { return false; }
if (start + 2 >= max) { return false; }
let found;
while (state.pos < max) {
if (state.src.charCodeAt(state.pos) === 125 /* } */) {
found = true;
break;
}
state.md.inline.skipToken(state);
}
if (!found || start + 1 === state.pos) {
state.pos = start;
return false;
}
const content = state.src.slice(start + 1, state.pos);
// disallow unescaped spaces/newlines
if (content.match(/(^|[^\\])(\\\\)*\s/)) {
state.pos = start;
return false;
}
state.posMax = state.pos;
state.pos = start + 1;
// Earlier we checked !silent, but this implementation does not need it
let token = state.push('dynamic_open', 'dynamic', 1);
token.markup = '<';
token = state.push('dynamic', 'text', 0);
token.content = content.replace(UNESCAPE_MD_RE, '$1');
token = state.push('dynamic_close','dynamic', -1);
token.markup = '>';
state.pos = state.posMax + 1;
state.posMax = max;
return true;
}
export default function(md, opts) {
md.inline.ruler.after('emphasis', 'dynamic', dynamicTextParser);
md.renderer.rules.dynamic_open = () => {return '<'}
md.renderer.rules.dynamic_open = () => {return '>'}
};