-
Notifications
You must be signed in to change notification settings - Fork 58
/
index.js
170 lines (155 loc) · 4.92 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
'use strict';
var utils = require('./utils.js');
module.exports = function attributes(md) {
function curlyAttrs(state){
var tokens = state.tokens;
var l = tokens.length;
for (var i = 0; i < l; ++i) {
// fenced code blocks
if (tokens[i].block && tokens[i].info && hasCurly(tokens[i].info)) {
var codeCurlyStart = tokens[i].info.indexOf('{');
var codeCurlyEnd = tokens[i].info.length - 1;
var codeAttrs = utils.getAttrs(tokens[i].info, codeCurlyStart + 1, codeCurlyEnd);
utils.addAttrs(codeAttrs, tokens[i]);
tokens[i].info = tokens[i].info.substring(0, codeCurlyStart);
continue;
}
// block tokens contain markup
// inline tokens contain the text
if (tokens[i].type !== 'inline') {
continue;
}
var inlineTokens = tokens[i].children;
if (!inlineTokens || inlineTokens.length <= 0) {
continue;
}
// attributes in inline tokens
for (var j=0, k=inlineTokens.length; j<k; ++j) {
// should be inline token of type text
if (!inlineTokens[j] || inlineTokens[j].type !== 'text') {
continue;
}
// token before should not be opening
if (!inlineTokens[j - 1] || inlineTokens[j - 1].nesting === 1) {
continue;
}
// token should contain { in begining
if (inlineTokens[j].content[0] !== '{') {
continue;
}
// } should be found
var endChar = inlineTokens[j].content.indexOf('}');
if (endChar === -1) {
continue;
}
// which token to add attributes to
var attrToken = matchingOpeningToken(inlineTokens, j - 1);
if (!attrToken) {
continue;
}
var attrs = utils.getAttrs(inlineTokens[j].content, 1, endChar);
if (attrs.length !== 0) {
// remove {}
inlineTokens[j].content = inlineTokens[j].content.substr(endChar + 1);
// add attributes
attrToken.info = "b";
utils.addAttrs(attrs, attrToken);
}
}
// attributes for blocks
if (hasCurly(tokens[i].content)) {
var content = last(inlineTokens).content;
var curlyStart = content.lastIndexOf('{');
var attrs = utils.getAttrs(content, curlyStart + 1, content.length - 1);
if (content[curlyStart - 1] === ' ') {
// trim space before {}
curlyStart -= 1;
}
// if list and `\n{#c}` -> apply to bullet list open
// `- iii \n{#c}` -> `<ul id="c"><li>iii</li></ul>`
var nextLastInline = nextLast(inlineTokens);
var possibleBulletListOpen = secondTokenNotHidden(tokens, i - 1);
if (nextLastInline && possibleBulletListOpen &&
nextLastInline.type === 'softbreak' &&
possibleBulletListOpen.type === 'bullet_list_open') {
utils.addAttrs(attrs, secondTokenNotHidden(tokens, i - 1));
// remove softbreak and {} inline tokens
tokens[i].children = inlineTokens.slice(0, -2);
} else {
// some blocks are hidden, example li > paragraph_open
utils.addAttrs(attrs, firstTokenNotHidden(tokens, i - 1));
last(inlineTokens).content = content.slice(0, curlyStart);
}
}
}
}
md.core.ruler.before('replacements', 'curly_attributes', curlyAttrs);
// render inline code blocks with attrs
md.renderer.rules.code_inline = renderCodeInline;
};
function renderCodeInline(tokens, idx, _, __, slf) {
var token = tokens[idx];
return '<code'+ slf.renderAttrs(token) +'>'
+ utils.escapeHtml(tokens[idx].content)
+ '</code>';
}
/**
* test if string has proper formated curly
*/
function hasCurly(str) {
// we need minimum four chars, example {.b}
if (!str || !str.length || str.length < 4) {
return false;
}
// should end in }
if (str.charAt(str.length - 1) !== '}') {
return false;
}
// should start with {
if (str.indexOf('{') === -1) {
return false;
}
return true;
}
/**
* some blocks are hidden (not rendered)
*/
function firstTokenNotHidden(tokens, i) {
if (tokens[i] && tokens[i].hidden) {
return firstTokenNotHidden(tokens, i - 1);
}
return tokens[i];
}
/**
* same as firstTokenNotHidden, but sTNH([ tok1, tok2, hidden ], 2) gives tok1
*/
function secondTokenNotHidden(tokens, i) {
if (tokens[i] && tokens[i].hidden) {
return secondTokenNotHidden(tokens, i - 1);
}
return firstTokenNotHidden(tokens, i - 1);
}
/**
* find corresponding opening block
*/
function matchingOpeningToken(tokens, i) {
if (tokens[i].type === 'softbreak') {
return false;
}
// non closing blocks, example img
if (tokens[i].nesting === 0) {
return tokens[i];
}
var type = tokens[i].type.replace('_close', '_open');
for (; i >= 0; --i) {
if (tokens[i].type === type) {
return tokens[i];
}
}
}
function last(arr) {
return arr.slice(-1)[0];
}
function nextLast(arr) {
return arr.slice(-2, -1)[0];
}