forked from wavesheep/markdown-it-plain-text
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
37 lines (33 loc) · 975 Bytes
/
index.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
'use strict';
module.exports = function plainTextPlugin(md) {
function plainTextRule(state) {
var text = scan(state.tokens);
// remove redundant white spaces
md.plainText = text.replace(/\s+/g, " ");
}
function scan(tokens) {
var text = '';
for (var i = 0; i < tokens.length; i++) {
var token = tokens[i];
if (token.children !== null) {
text += scan(token.children);
} else {
if (
token.type === 'text' ||
token.type === 'fence' ||
token.type === 'html_block' ||
token.type === 'code_block' ||
token.type === 'code_inline' ||
token.type === 'html_inline' ||
token.type === 'emoji'
) {
text += token.content;
} else if (/[a-zA-Z]+_close/.test(token.type)) { // prevent words from sticking together
text += " "
}
}
}
return text;
}
md.core.ruler.push('plainText', plainTextRule);
}