-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathlink-environment-variables.mjs
54 lines (45 loc) · 1.22 KB
/
link-environment-variables.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
import { visit } from "unist-util-visit";
const isString = (value) => typeof value === "string";
const isCubeEnvVar = (value) => {
const prefixes = [
"CUBEJS_",
"CUBESTORE_",
"CUBESQL_",
]
return (
!value.includes("=") &&
!value.endsWith("_*") &&
prefixes.some(prefix => value.startsWith(prefix))
);
};
const getAllIndexesByType = (arr, type) => {
var indexes = [],
i;
for (i = 0; i < arr.length; i++) {
if (arr[i].type === type) {
indexes.push(i);
}
}
return indexes;
};
export default function retextSentenceSpacing() {
return (tree) => {
visit(tree, ["paragraph", "tableCell"], (node) => {
const indexes = getAllIndexesByType(node.children, "inlineCode");
for (const index of indexes) {
const currentNode = node.children[index];
const value = currentNode.value;
if (value && isString(value) && isCubeEnvVar(value)) {
const newNode = {
children: [currentNode],
title: null,
type: "link",
url: `/reference/configuration/environment-variables#${value.toLowerCase()}`
};
node.children.splice(index, 1, newNode);
}
}
});
return tree;
};
}