-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
35 lines (32 loc) · 1.05 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
module.exports = function markdownItBidi(md) {
const rules = [
'heading_open',
'blockquote_open',
'paragraph_open',
'bullet_list_open',
'ordered_list_open',
'table_open',
'th_open',
'td_open'
];
const bidi = defaultRenderer => (tokens, idx, opts, env, self) => {
const token = tokens[idx];
const prevToken = tokens[idx - 1];
if (token.type === 'th_open' && prevToken.type === 'tr_open') {
return defaultRenderer(tokens, idx, opts, env, self);
}
// omit this token if this is the first child of an element
if (prevToken && rules.includes(prevToken.type) && token.level > prevToken.level) {
return defaultRenderer(tokens, idx, opts, env, self);
}
token.attrSet('dir', 'auto');
return defaultRenderer(tokens, idx, opts, env, self);
};
const proxy = (tokens, idx, opts, _, self) => {
return self.renderToken(tokens, idx, opts);
};
rules.forEach(rule => {
const defaultRenderer = md.renderer.rules[rule] || proxy;
md.renderer.rules[rule] = bidi(defaultRenderer);
});
};