This repository has been archived by the owner on Sep 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (47 loc) · 1.86 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
const visit = require("unist-util-visit")
const toString = require("mdast-util-to-string")
const gridTablesParser = require('remark-grid-tables')
const pattern = /[\u2E80-\u2EFF\u2F00-\u2FDF\u3000-\u303F\u31C0-\u31EF\u3200-\u32FF\u3300-\u33FF\u3400-\u4DBF\u4DC0-\u4DFF\u4E00-\u9FBF\uF900-\uFAFF\uFE30-\uFE4F\uFF00-\uFFEF]/
const isTranslation = text => text && pattern.test(text)
module.exports = async ({ markdownAST }) => {
visit(markdownAST, (node, index, parent) => {
const type = node.type
if (type !== 'heading' && type !== 'paragraph') {
return
}
!node.data && (node.data = {})
if (index === 0 || !isTranslation(toString(node))) {
return
}
const previous = parent.children[index - 1]
if (!isTranslation(toString(previous)) && isTranslation(toString(node)) && previous.type === type) {
if (previous.data.hProperties && previous.data.hProperties["translation-origin"]) {
return
}
node.data.hProperties = {
...node.data.hProperties,
"translation-result": "on"
}
previous.data.hProperties = {
...previous.data.hProperties,
"translation-origin": "off"
}
type === 'heading' && (previous.data.hProperties = {
...previous.data.hProperties,
"id": toString(node)
})
if (type === 'heading') {
const isLink = element => element.type === 'link'
const index1 = node.children.findIndex(isLink)
const index2 = previous.children.findIndex(isLink)
if (index1 !== -1 && index2 !== -1) {
previous.children[index2].url = node.children[index1].url
}
}
parent.children.splice(index - 1, 1, parent.children[index])
parent.children.splice(index, 1, previous)
}
})
return markdownAST
}
module.exports.setParserPlugins = () => [gridTablesParser]