forked from Mergifyio/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remark-headings-plugin.mjs
81 lines (70 loc) · 2.35 KB
/
remark-headings-plugin.mjs
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
import {visit} from 'unist-util-visit'
import {toString} from 'mdast-util-to-string'
import configSchema from './static/mergify-configuration-openapi.json' assert {
type: "json",
}
export function remarkHeadingsPlugin() {
return async function transformer(tree, file) {
let headings = []
visit(tree, `heading`, heading => {
headings.push({
value: toString(heading),
depth: heading.depth,
})
})
const mdxFile = file
if (!mdxFile.data.meta) {
mdxFile.data.meta = {}
}
mdxFile.data.meta.headings = headings
}
}
export function remarkTablePlugin() {
return async function transformer(tree, file) {
const tables = []
visit(tree, 'mdxJsxFlowElement', element => {
switch(element.name) {
case 'OptionsTable':
const name = element.attributes.find(attr => attr.type === 'mdxJsxAttribute' && attr.name === 'name').value
const optionsTableData = configSchema?.definitions?.[name]?.properties
tables.push({
node: JSON.stringify(element),
data: JSON.stringify(optionsTableData),
content: null
})
break;
case 'PullRequestAttributesTable':
const pullRequestAttributes = configSchema?.definitions?.PullRequestAttribute?.enum
tables.push({
node: JSON.stringify(element),
data: JSON.stringify(pullRequestAttributes),
content: null
})
break;
case 'ActionOptionsTable':
const action = element.attributes.find(attr => attr.type === 'mdxJsxAttribute' && attr.name === 'action').value
const actionOptions = configSchema?.definitions?.Actions?.properties?.[action]?.properties
tables.push({
node: JSON.stringify(element),
data: JSON.stringify(actionOptions),
content: null
})
break;
case 'Table':
tables.push({
node: JSON.stringify(element),
// For raw tables, we need the content as string
// for algolia to search into
content: toString(element),
data: null
})
break;
}
})
const mdxFile = file
if (!mdxFile.data.meta) {
mdxFile.data.meta = {}
}
mdxFile.data.meta.tables = tables
}
}