-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
67 lines (59 loc) · 1.65 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
"use strict";
const visit = require("unist-util-visit");
const is = require("unist-util-is");
const pangu = require("pangu");
const setOptions = require("./set-options");
// List of Markdown AST: <https://github.com/syntax-tree/mdast>
// AST Explorer: <https://astexplorer.net/#/gist/7a794a8fc43b2e75e27024c85fb77aad/0934495eb735dffdf739dc7943f7848940070f8e>
//
// AST we should format:
// 1. text node:
// * paragraph children
// * blockquote children
// * heading children
// * emphasis children
// * strong children
// * listItems children
// * tableCell children
// * delete children
// * link children
// * image children
// * footnote children
// 2. inlineCode value
// 3. link title
// 4. image title/alt
// 5. imageReference alt
// 6. definition title
//
//
// AST we ignored:
// 1. YAML
// 2. html (it can contain link: <a> <img>)
// 3. 临接情况
// 1. 粗体:我的a**粗体**
// 2. 强调:我的a*强调*
// 3. ...
function format(value) {
if (!value) return value;
return pangu.spacing(value);
}
function visitor(node) {
if (is(node, "text") || is(node, "inlineCode")) {
node.value = format(node.value);
}
if (is(node, "link") || is(node, "image") || is(node, "definition")) {
node.title = format(node.title);
}
if (is(node, "image") || is(node, "imageReference")) {
node.alt = format(node.alt);
}
}
module.exports = function attacher(options) {
const settings = setOptions(options || {});
const subset = Object.keys(settings).filter((e) => settings[e]);
return function transformer(tree, file) {
visit(tree, (node) => {
if (is(node, subset)) visitor(node);
});
};
};