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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
const visit = require('unist-util-visit');
/**
* Given the MDAST ast, look for all fenced Blockquote
*
* @param {object} ast
* @param {vFile} vFile
* @return {function}
*/
function visitBlockquote(ast) {
return visit(ast, 'blockquote', (node, index, parent) => {
const firstNode = node.children[0];
if (firstNode.type === 'paragraph') {
if (firstNode.children[0].type === 'text') {
if (firstNode.children[0].value.startsWith('!secret')) {
node.type = 'div';
firstNode.children[0].value = firstNode.children[0].value.substr(7);
let sum = '';
if (firstNode.children[0].value.indexOf('\n') >= 0) {
sum = firstNode.children[0].value.substr(0,
firstNode.children[0].value.indexOf('\n'));
firstNode.children[0].value = firstNode.children[0].value.substr(
firstNode.children[0].value.indexOf('\n'));
} else {
sum = firstNode.children[0].value;
firstNode.children[0].value = '';
}
const secret = {
type: 'special-box-secret',
children: [{
type: 'summary',
data: {
hName: 'summary',
hChildren: [{
type: 'text',
value: sum ? sum : 'Spoiler'
}]
}
},
node],
data: {
hName: 'details',
hProperties: {
className: 'special-box secret'
}
}
};
parent.children.splice(index, 1, secret);
return node;
} else if (firstNode.children[0].value.startsWith('!information') ||
firstNode.children[0].value.startsWith('!good') ||
firstNode.children[0].value.startsWith('!bad') ||
firstNode.children[0].value.startsWith('!comment') ||
firstNode.children[0].value.startsWith('!attention') ||
firstNode.children[0].value.startsWith('!question')) {
node.type = 'div';
node.data = {
hName: 'div',
hProperties: {
className: 'special-box-content'
}
};
let type = '';
if (firstNode.children[0].value.indexOf('\n') > 0) {
type = firstNode.children[0].value.substr(1, firstNode.children[0].value.indexOf('\n'));
firstNode.children[0].value = firstNode.children[0].value.substr(firstNode.children[0].value.indexOf('\n'));
console.log(type);
} else {
type = firstNode.children[0].value.substr(1);
firstNode.children[0].value = '';
}
const box = {
type: 'special-box-div',
children: [node],
data: {
hName: 'div',
hProperties: {
className: 'special-box ' + type
}
}
};
parent.children.splice(index, 1, box);
return node;
}
}
}
return node;
});
}
/**
* Returns the transformer which acst on the MDAST tree and given VFile.
*
* @link https://github.com/unifiedjs/unified#function-transformernode-file-next
* @link https://github.com/syntax-tree/mdast
* @link https://github.com/vfile/vfile
* @return {function}
*/
function box() {
/**
* @param {object} ast MDAST
* @param {vFile} vFile
* @param {function} next
* @return {object}
*/
return function (ast, vFile, next) { // Transformer
visitBlockquote(ast);
if (typeof next === 'function') {
return next(null, ast, vFile);
}
return ast;
};
}
module.exports = box;
|