-
Notifications
You must be signed in to change notification settings - Fork 0
/
remark-typograf.js
128 lines (110 loc) · 3.56 KB
/
remark-typograf.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
const visit = require("unist-util-visit");
const Typograf = require("typograf");
const escapeRegexp = require("lodash.escaperegexp");
function remarkTypograf(config = {}) {
let { typograf, builtIn = true, keywords = [], ...typografConfig } = config;
if (!typograf && builtIn === false) {
throw new Error(
"Typograf option should be specified. Please pass instance typograf as option or set builtIn to true"
);
}
if (typograf && builtIn === true) {
throw new Error(
"`builtIn` option is true and `typograf` also is passed. Please set `builtIn` to false or clean `typograf` option"
);
}
if (
builtIn === true &&
typografConfig.locale &&
!Array.isArray(typografConfig.locale)
) {
throw new Error(
`Locale config should be array of string. e.g. {"locale": ["ru"]}`
);
}
if (
builtIn === true &&
!Array.isArray(typografConfig && typografConfig.locale)
) {
typografConfig.locale = ["ru"];
}
if (!typograf) {
typograf = new Typograf(typografConfig);
}
function getTextNodes(tree) {
const textNodes = [];
if (tree.type === "inlineCode") {
textNodes.push("`" + tree.value + "`");
return textNodes;
}
if (typeof tree.value === "string") {
textNodes.push(tree.value);
return textNodes;
}
visit(tree, "inlineCode", (node) => {
textNodes.push("`" + node.value + "`");
});
visit(tree, "text", (node) => {
textNodes.push(node.value);
});
return textNodes;
}
function getLeftSiblingText(index, children) {
const textNodes = getTextNodes(children[index - 1]);
return typograf.execute(textNodes.pop() || "");
}
function getRightSiblingText(index, children) {
const textNodes = getTextNodes(children[index + 1]);
return typograf.execute(textNodes.shift() || "");
}
function applyTypograf(text, index, parent) {
if (index === 0 && parent.children.length > 1) {
const rightText = getRightSiblingText(index, parent.children);
const typografedText = typograf.execute(text + rightText);
return typografedText.substring(
0,
typografedText.length - rightText.length
);
}
if (index === parent.children.length - 1 && parent.children.length > 1) {
const leftText = getLeftSiblingText(index, parent.children);
const typografedText = typograf.execute(leftText + text);
return typografedText.substring(
leftText.length,
typografedText.length + leftText.length
);
}
if (parent.children.length > 1) {
const leftText = getLeftSiblingText(index, parent.children);
const rightText = getRightSiblingText(index, parent.children);
const typografedText = typograf.execute(leftText + text + rightText);
return typografedText.substring(
leftText.length,
typografedText.length - rightText.length
);
}
return typograf.execute(text);
}
function visitor(node, index, parent) {
let text = node.value;
keywords.forEach((keyword) => {
const hex = Buffer.from(keyword).toString("hex");
const regExp = new RegExp(`${escapeRegexp(keyword)}`, `g`);
text = text.replace(regExp, hex);
});
text = applyTypograf(text, index, parent);
keywords.forEach((keyword) => {
const hex = Buffer.from(keyword).toString("hex");
const regExp = new RegExp(`${escapeRegexp(hex)}`, `g`);
text = text.replace(regExp, keyword);
});
node.value = text;
}
function transform(tree) {
visit(tree, "text", visitor);
}
return transform;
}
module.exports = {
remarkTypograf,
};