-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
105 lines (94 loc) · 2.73 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
105
// Process -> center text <-
'use strict';
module.exports = function centertext_plugin(md) {
function tokenize(state, silent) {
var token,
max = state.posMax,
start = state.pos,
marker = state.src.charCodeAt(start);
if (start + 1 > max) { return false; }
if (silent) { return false; } // don't run any pairs in validation mode
if (marker === 45/* - */ &&
state.src.charCodeAt(start + 1) === 62/* > */
) {
state.scanDelims(state.pos, true);
token = state.push('text', '', 0);
token.content = '->';
state.delimiters.push({
marker: token.content,
jump: 0,
token: state.tokens.length - 1,
level: state.level,
end: -1,
open: true,
close: false
});
} else if (marker === 60/* < */ &&
state.src.charCodeAt(start + 1) === 45/* - */
) {
// found the close marker
state.scanDelims(state.pos, true);
token = state.push('text', '', 0);
token.content = '<-';
state.delimiters.push({
marker: token.content,
jump: 0,
token: state.tokens.length - 1,
level: state.level,
end: -1,
open: false,
close: true
});
} else {
// neither
return false;
}
state.pos += 2;
return true;
}
// Walk through delimiter list and replace text tokens with tags
//
function postProcess(state) {
var i,
foundStart = false,
foundEnd = false,
delim,
token,
delimiters = state.delimiters,
max = state.delimiters.length;
for (i = 0; i < max; i++) {
delim = delimiters[i];
if (delim.marker === '->') {
foundStart = true;
} else if (delim.marker === '<-') {
foundEnd = true;
}
}
if (foundStart && foundEnd) {
for (i = 0; i < max; i++) {
delim = delimiters[i];
if (delim.marker === '->') {
foundStart = true;
token = state.tokens[delim.token];
token.type = 'centertext_open';
token.tag = 'div';
token.nesting = 1;
token.markup = '->';
token.content = '';
token.attrs = [ [ 'class', 'text-align-center' ] ];
} else if (delim.marker === '<-') {
if (foundStart) {
token = state.tokens[delim.token];
token.type = 'centertext_close';
token.tag = 'div';
token.nesting = -1;
token.markup = '<-';
token.content = '';
}
}
}
}
}
md.inline.ruler.before('emphasis', 'centertext', tokenize);
md.inline.ruler2.before('emphasis', 'centertext', postProcess);
};