-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
182 lines (149 loc) · 4.6 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
"use strict";
var classy,
// magic char codes
CHAR_0 = 0x30,
CHAR_9 = 0x39,
CHAR_A_UPPER = 0x41,
CHAR_Z_UPPER = 0x5A,
CHAR_A_LOWER = 0x61,
CHAR_Z_LOWER = 0x7A,
CHAR_UNDERSCORE = 0x5F,
CHAR_DASH = 0x2D,
CHAR_SPACE = 0x20,
CHAR_OPEN_CURLY = 0x7B,
CHAR_CLOSE_CURLY = 0x7D,
CHAR_NEWLINE = 0xA;
function isValidClassChar(code) {
return (code >= CHAR_0 && code <= CHAR_9) ||
(code >= CHAR_A_UPPER && code <= CHAR_Z_UPPER) ||
(code >= CHAR_A_LOWER && code <= CHAR_Z_LOWER) ||
code === CHAR_UNDERSCORE ||
code === CHAR_DASH ||
code === CHAR_SPACE;
}
function parse(state) {
var pos = state.pos,
initialPos = pos,
classString = "",
i,
pendingText,
preferOuter = false,
token;
if (state.src.charCodeAt(pos) !== CHAR_OPEN_CURLY) {
return false;
}
// advance to account for opening brace
pos += 1;
// grab everything til the closing brace
while (state.src.charCodeAt(pos) !== CHAR_CLOSE_CURLY) {
if (!isValidClassChar(state.src.charCodeAt(pos))) {
return false;
}
classString += state.src.charAt(pos);
pos += 1;
}
// advance to account for closing brace
pos += 1;
// only count curly brackets as a classy token if
// - at the end of the element
// - just before a newline
if (pos !== state.posMax && state.src.charCodeAt(pos) !== CHAR_NEWLINE) {
return false;
}
// `preferOuter` keeps track of whether, in an ambiguous case
// (for instance with <ul>s and <li>s)
// we should prefer to add it on the containing element
if (state.src.charCodeAt(initialPos - 1) === CHAR_NEWLINE) {
preferOuter = true;
}
state.pos = pos;
// work back through the tokens, checking if any of them is an open inline tag
// if it does turn out we're in an inline tag, the class belongs to that
//
// NOTE TO SELF refactor this, add handling for <a> tags as well
for (i = state.tokens.length - 1; i >= 0; i -= 1) {
if (state.tokens[i].type === "em_close"
|| state.tokens[i].type === "strong_close") {
break;
}
if (state.tokens[i].type === "em_open"
|| state.tokens[i].type === "strong_open") {
state.tokens[i].attrPush(["class", classString]);
// there might be a leftover space at the end
pendingText = state.pending;
if (pendingText.charCodeAt(pendingText.length - 1) === CHAR_SPACE) {
state.pending = pendingText.substring(0, pendingText.length - 1);
}
return true;
}
}
token = state.push("classy", "classy", 0);
token.content = classString;
token.hidden = true;
token.preferOuter = preferOuter;
return true;
}
function getClassyFromInlineToken(inlineToken) {
var classy,
tokens = inlineToken.children,
numChildren = tokens.length;
// the token *at the end* of the inline tag
// should be classy
//
// also, don't do anything if the only token present is a classy token
if (tokens[numChildren - 1].type !== "classy"
|| tokens.length === 1) {
return null;
}
classy = tokens.pop();
numChildren -= 1;
// clean up after token was removed
if (tokens[numChildren - 1].type === "softbreak") {
// we may need to get rid of the newline just before classy statement
tokens.pop(numChildren - 1);
} else {
// or there might be some whitespace
// we may need to trim on the previous element
tokens[numChildren - 1].content = tokens[numChildren - 1].content.trim();
}
return classy;
}
function getOpeningToken(tokens, preferOuter, currentIndex) {
var closingTokenIndex = currentIndex + 1,
openingTokenType,
i;
if (tokens[closingTokenIndex].hidden) {
closingTokenIndex += 1;
}
if (preferOuter && tokens[closingTokenIndex + 1]
&& /_close$/.test(tokens[closingTokenIndex + 1].type)) {
closingTokenIndex += 1;
}
openingTokenType = tokens[closingTokenIndex].type.replace("_close", "_open");
for (i = currentIndex; i >= 0; i -= 1) {
if (tokens[i].type === openingTokenType
&& tokens[i].level === tokens[closingTokenIndex].level) {
return tokens[i];
}
}
}
function parseBlock(state) {
var i,
openingToken,
classy;
for (i = 0; i < state.tokens.length; i += 1) {
if (state.tokens[i].type === "inline") {
classy = getClassyFromInlineToken(state.tokens[i]);
while (classy) {
openingToken = getOpeningToken(state.tokens, classy.preferOuter, i);
openingToken.attrPush(["class", classy.content]);
classy = getClassyFromInlineToken(state.tokens[i]);
}
}
}
}
classy = function (md) {
md.inline.ruler.push("classy", parse);
md.core.ruler.push("classy", parseBlock);
};
module.exports = classy;