forked from arve0/markdown-it-attrs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
155 lines (131 loc) · 4.11 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
'use strict';
const patternsConfig = require('./patterns.js');
const defaultOptions = {
leftDelimiter: '{',
rightDelimiter: '}',
allowedAttributes: [],
disableParagraphBlock: false
};
module.exports = function attributes(md, options_) {
let options = Object.assign({}, defaultOptions);
options = Object.assign(options, options_);
const patterns = patternsConfig(options);
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 {array} tokens
* @param {number} i
* @param {object} t Test to match.
* @return {object} { match: true|false, j: null|number }
*/
function test(tokens, i, t) {
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;
const childTests = t.children;
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 {array} arr
* @param {number} n
*/
function get(arr, n) {
return n >= 0 ? arr[n] : arr[arr.length + n];
}
// get last element of array, safe - returns {} if not found
function last(arr) {
return arr.slice(-1)[0] || {};
}