-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
104 lines (80 loc) · 2.14 KB
/
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
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
// Process ~subscript~
'use strict';
var COLOR_OPEN_REGEX = /{\s*color\s*:\s*(\w+\s*)}/;
var COLOR_CLOSE_REGEX = /{\s*color\s*}/;
var TOKEN_TYPE = 'color_text';
var MARKUP = "{color}";
function getColor(attrs) {
if(!attrs || attrs.length < 1) { return null; }
for(var ii=0; ii< attrs.length; ii++) {
var att = attrs[ii];
if(att && att.length > 1) {
if(att[0] === 'color') {
return att[1];
}
}
}
return null;
}
function color(state, silent) {
var found,
content,
token,
startResult,
endResult,
result,
nesting = 1,
isOpen = true,
max = state.posMax,
start = state.pos;
if (state.src.charCodeAt(start) !== 0x7B/* ~ */) { return false; }
if (silent) { return false; } // don't run any pairs in validation mode
content = state.src.slice(start);
startResult = COLOR_OPEN_REGEX.exec(content);
endResult = COLOR_CLOSE_REGEX.exec(content);
if(!startResult && !endResult) { return false; }
if(startResult && endResult) {
if(startResult.index < endResult.index) {
result = startResult;
} else {
nesting = -1;
result = endResult;
}
} else if(startResult) {
result = startResult;
} else {
result = endResult;
nesting = -1;
}
isOpen = result.length === 2;
state.posMax = start + result[0].length;
token = state.push(TOKEN_TYPE, 'span', nesting);
token.markup = MARKUP;
if(isOpen) {
token.attrPush(['color', result[1]]);
}
state.pos = state.posMax;
state.posMax = max;
return true;
}
function renderDefault(tokens, idx, _options, env, self) {
var token = tokens[idx];
var result = ['<'];
if(token.nesting === -1) {
result.push('/');
}
result.push('span');
if(token.nesting === 1) {
var color = getColor(token.attrs);
if(color) {
result.push(' style="color:',color,';"');
}
}
result.push('>');
return result.join('');
}
module.exports = function sub_plugin(md, name, options) {
options = options || {};
md.inline.ruler.after('emphasis', 'span', color);
md.renderer.rules[TOKEN_TYPE] = options.render || renderDefault;
};