-
Notifications
You must be signed in to change notification settings - Fork 1
/
remarkCrel.js
67 lines (57 loc) · 2.44 KB
/
remarkCrel.js
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
62
63
64
65
66
67
const crel = global.crel || require('crel');
module.exports = function(opts) {
opts = Object.assign({ ignoreParagraphs: false }, opts);
this.Compiler = class {
constructor(node, file) {
this.node = node;
this.file = file;
}
_toDOM(parentNode) {
const result = [];
if (parentNode.type === 'root' && opts.ignoreParagraphs) {
for (let paragraphIdx = 0; paragraphIdx < parentNode.children.length; paragraphIdx++) {
const paragraphNode = parentNode.children[paragraphIdx];
if (!paragraphNode.children || paragraphNode.children.length === 0) {
continue;
}
if (paragraphIdx > 0) {
// NOTE(netux): this is needed to render more than one new line, as two new lines or more are the delimited for
// paragraphs, and we can't turn paragraphs off.
const lineDifference = paragraphNode.position.start.line - parentNode.children[paragraphIdx - 1].position.end.line;
for (let j = 0; j < lineDifference; j++) {
result.push('\n')
}
}
result.push(... this._toDOM(paragraphNode));
}
} else {
for (const node of parentNode.children) {
const next = node.children ? (() => this._toDOM(node)) : (() => node.value);
const nodeRenderer = this.visitors[node.type] || this.visitors._default;
const el = nodeRenderer(node, next);
result.push(el);
}
}
return result;
}
compile() {
return this._toDOM(this.node);
}
};
this.Compiler.prototype.visitors = {
_default: (node, next) => crel('span', { 'data-mdst-type': node.type }, next()),
paragraph: (node, next) => crel('p', next()),
text: (node, next) => node.value,
break: (node, next) => crel('br'),
link: (node, next) => crel('a', { href: node.url, target: '_blank' }, next()),
emphasis: (node, next) => crel('i', next()),
strong: (node, next) => crel('b', next()),
underline: (node, next) => crel('u', next()),
delete: (node, next) => crel('s', next()),
inlineCode: (node, next) => crel('code', next()),
mention: (node, next) => crel('span', { class: 'mention' }, next()),
coordinate: (node, next) => crel('a', { href: node.url }, next()),
emoji: (node, next) => crel('span', { title: `:${node.emojiName}:` }, node.value),
fontAwesomeIcon: (node, next) => crel('i', { class: node.classes.join(' ') })
};
};