-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformat.mjs
115 lines (102 loc) · 2.72 KB
/
format.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
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
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { remark } from "remark";
import { remove } from "unist-util-remove";
import { visit } from "unist-util-visit";
import lz from "lz-string";
import prettier from "remark-prettier";
import toc from "remark-toc";
import consistent from "remark-preset-lint-consistent";
import recommended from "remark-preset-lint-recommended";
const [, , fileReference] = process.argv;
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const studio = `https://studio.apollographql.com/sandbox/explorer`;
// const endpoint = `https://graphql.withalpaca.travel/?accessToken=UPDATE_TOKEN`;
function removeExisting() {
return (tree) => {
remove(tree, (node) => {
const [first] = node.children || [];
if (first && /^Sandbox:/.test(first.value)) {
return true;
}
return false;
});
};
}
function link(doc) {
const explorerURLState = lz.compressToEncodedURIComponent(
JSON.stringify({ document: doc })
);
return `${studio}?explorerURLState=${explorerURLState}`;
}
function debug() {
return (tree) => {
visit(tree, "link", (node) => {
console.log(node);
});
};
}
function addLink() {
return (tree) => {
tree.children = tree.children.flatMap((child) => {
if (child.type === "code" && child.lang === "graphql") {
return [
child,
{
type: "paragraph",
children: [
{
type: "text",
value: "Sandbox: ",
},
{
type: "link",
title: null,
url: "/topics/graphql/Apollo%20Sandbox/",
children: [
{
type: "text",
value: "Configure",
},
],
},
{
type: "text",
value: " | ",
},
{
type: "link",
title: null,
url: link(child.value),
children: [
{
type: "text",
value: "Try Operation",
},
],
},
],
},
];
}
return [child];
});
};
}
main();
async function main() {
const ref = path.resolve(__dirname, fileReference);
console.log(`Formatting: ${ref}`);
const file = await remark()
.use(removeExisting)
.use(addLink)
.use(consistent)
.use(recommended)
.use(toc)
.use(prettier)
// .use(debug)
.process(fs.readFileSync(ref, "utf8"));
fs.writeFileSync(ref, file.toString("utf8"));
}