forked from arve0/markdown-it-attrs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
219 lines (194 loc) · 6.18 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
'use strict';
const patternsConfig = require('./patterns.js');
/**
* @typedef {import('markdown-it')} MarkdownIt
*
* @typedef {import('markdown-it/lib/rules_core/state_core.mjs').default} StateCore
*
* @typedef {import('markdown-it/lib/token.mjs').default} Token
*
* @typedef {import('markdown-it/lib/token.mjs').Nesting} Nesting
*
* @typedef {Object} Options
* @property {!string} leftDelimiter left delimiter, default is `{`(left curly bracket)
* @property {!string} rightDelimiter right delimiter, default is `}`(right curly bracket)
* @property {AllowedAttribute[]} allowedAttributes empty means no limit
*
* @typedef {string|RegExp} AllowedAttribute rule of allowed attribute
*
* @typedef {[string, string]} AttributePair
*
* @typedef {[number, number]} SourceLineInfo
*
* @typedef {Object} CurlyAttrsPattern
* @property {string} name
* @property {DetectingRule[]} tests
* @property {(tokens: Token[], i: number, j?: number) => void} transform
*
* @typedef {Object} MatchedResult
* @property {boolean} match true means matched
* @property {number?} j postion index number of Array<{@link Token}>
*
* @typedef {(str: string) => boolean} DetectingStrRule
*
* @typedef {Object} DetectingRule rule for testing {@link Token}'s properties
* @property {number=} shift offset index number of Array<{@link Token}>
* @property {number=} position fixed index number of Array<{@link Token}>
* @property {(string | DetectingStrRule)=} type
* @property {(string | DetectingStrRule)=} tag
* @property {DetectingRule[]=} children
* @property {(string | DetectingStrRule)=} content
* @property {(string | DetectingStrRule)=} markup
* @property {(string | DetectingStrRule)=} info
* @property {Nesting=} nesting
* @property {number=} level
* @property {boolean=} block
* @property {boolean=} hidden
* @property {AttributePair[]=} attrs
* @property {SourceLineInfo[]=} map
* @property {any=} meta
*/
/** @type {Options} */
const defaultOptions = {
leftDelimiter: '{',
rightDelimiter: '}',
allowedAttributes: []
};
/**
* @param {MarkdownIt} md
* @param {Options=} options_
*/
module.exports = function attributes(md, options_) {
let options = Object.assign({}, defaultOptions);
options = Object.assign(options, options_);
const patterns = patternsConfig(options);
/**
* @param {StateCore} state
*/
function curlyAttrs(state) {
const tokens = state.tokens;
for (let i = 0; i < tokens.length; i++) {
for (let p = 0; p < patterns.length; p++) {
const pattern = patterns[p];
let j = null; // position of child with offset 0
const match = pattern.tests.every(t => {
const res = test(tokens, i, t);
if (res.j !== null) { j = res.j; }
return res.match;
});
if (match) {
pattern.transform(tokens, i, j);
if (pattern.name === 'inline attributes' || pattern.name === 'inline nesting 0') {
// retry, may be several inline attributes
p--;
}
}
}
}
}
md.core.ruler.before('linkify', 'curly_attributes', curlyAttrs);
};
/**
* Test if t matches token stream.
*
* @param {Token[]} tokens
* @param {number} i
* @param {DetectingRule} t
* @returns {MatchedResult}
*/
function test(tokens, i, t) {
/** @type {MatchedResult} */
const res = {
match: false,
j: null // position of child
};
const ii = t.shift !== undefined
? i + t.shift
: t.position;
if (t.shift !== undefined && ii < 0) {
// we should never shift to negative indexes (rolling around to back of array)
return res;
}
const token = get(tokens, ii); // supports negative ii
if (token === undefined) { return res; }
for (const key of Object.keys(t)) {
if (key === 'shift' || key === 'position') { continue; }
if (token[key] === undefined) { return res; }
if (key === 'children' && isArrayOfObjects(t.children)) {
if (token.children.length === 0) {
return res;
}
let match;
/** @type {DetectingRule[]} */
const childTests = t.children;
/** @type {Token[]} */
const children = token.children;
if (childTests.every(tt => tt.position !== undefined)) {
// positions instead of shifts, do not loop all children
match = childTests.every(tt => test(children, tt.position, tt).match);
if (match) {
// we may need position of child in transform
const j = last(childTests).position;
res.j = j >= 0 ? j : children.length + j;
}
} else {
for (let j = 0; j < children.length; j++) {
match = childTests.every(tt => test(children, j, tt).match);
if (match) {
res.j = j;
// all tests true, continue with next key of pattern t
break;
}
}
}
if (match === false) { return res; }
continue;
}
switch (typeof t[key]) {
case 'boolean':
case 'number':
case 'string':
if (token[key] !== t[key]) { return res; }
break;
case 'function':
if (!t[key](token[key])) { return res; }
break;
case 'object':
if (isArrayOfFunctions(t[key])) {
const r = t[key].every(tt => tt(token[key]));
if (r === false) { return res; }
break;
}
// fall through for objects !== arrays of functions
default:
throw new Error(`Unknown type of pattern test (key: ${key}). Test should be of type boolean, number, string, function or array of functions.`);
}
}
// no tests returned false -> all tests returns true
res.match = true;
return res;
}
function isArrayOfObjects(arr) {
return Array.isArray(arr) && arr.length && arr.every(i => typeof i === 'object');
}
function isArrayOfFunctions(arr) {
return Array.isArray(arr) && arr.length && arr.every(i => typeof i === 'function');
}
/**
* Get n item of array. Supports negative n, where -1 is last
* element in array.
* @param {Token[]} arr
* @param {number} n
* @returns {Token=}
*/
function get(arr, n) {
return n >= 0 ? arr[n] : arr[arr.length + n];
}
/**
* get last element of array, safe - returns {} if not found
* @param {DetectingRule[]} arr
* @returns {DetectingRule}
*/
function last(arr) {
return arr.slice(-1)[0] || {};
}