forked from aptos-labs/aptos-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdownPlugins.js
60 lines (52 loc) · 1.69 KB
/
markdownPlugins.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
const remarkableKatex = require('remarkable-katex');
module.exports = [
function enableKatex(md) {
md.use(remarkableKatex);
},
/**
* Enable some defaults on the Markdown class
*/
function enableInlineRuler(md) {
md.inline.ruler.enable([
'sub',
'sup'
]);
},
function addEmDash(md) {
const rule = (textRule) => (tokens, idx, options, env) => {
const text = textRule(tokens, idx, options, env);
return text.replace('---', ' — ');
};
md.renderer.rules.text = rule(md.renderer.rules.text);
},
/**
* This will add a class to an image. To use structure as:
* [<title>](<img path){: .<class>}
*
* Example - add "download" class to an image:
* [PDF](assets/illustrations/move-language-pdf.png){: .download}
*/
function addImageClass(md) {
// This takes a render method in as an arg.
const rule = (imageRule) => (tokens, idx, options, env) => {
// Get the default image
const img = imageRule(tokens, idx, options, env);
const clsTkn = tokens[idx+1];
// The token we are looking for will be text with content
if (!clsTkn || clsTkn.type !== 'text' || !clsTkn.content) {
return img;
}
//Finds the "{: .<className>}" and pulls out the className only
const getClassName = (name) => {
return name.match(/\{\:\s*\.[\w-]*\s*\}/g)
? name.match(/(\w|\-)+/g)
: '';
}
const classString = ` class="${getClassName(clsTkn.content)}">`;
// Remove the special token or it will get rendered
clsTkn.content = '';
return img.slice(0, -1) + classString;
};
md.renderer.rules.image = rule(md.renderer.rules.image);
}
];