- Notifications
You must be signed in to change notification settings - Fork 249
/
Copy pathextract-tocs.mjs
219 lines (174 loc) · 6.11 KB
/
extract-tocs.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* This script is used for generating the table of contents for prose
* text documents
*/
import{unified}from"unified";
importglobfrom"glob";
importpathfrom"path";
importfsfrom"fs";
import{URL}from"url";
import{defaultProcessor}from"./markdown.js";
constpathname=newURL(".",import.meta.url).pathname;
const__dirname=
process.platform!=="win32" ? pathname : pathname.substring(1);
// orderArr: ["introduction", "overview",,...]
constorderFiles=(filepaths,orderArr)=>{
constorder=orderArr.reduce((acc,next,i)=>{
acc[next]=null;
returnacc;
},{});
filepaths.forEach((filepath)=>{
constid=path.basename(filepath,path.extname(filepath));
order[id]=filepath;
});
// last sanity check if there's an unmatched filepath
Object.entries(order).forEach(([name,filepath])=>{
// may happen e.g. due to invalid file paths within
// sidebar json
if(filepath==null){
thrownewError(
`Cannot find file for "${name}". Does it exist in the pages folder?`
);
}
});
returnObject.values(order);
};
// Used for collapsing nested inlineCodes with the actual header text
constcollapseHeaderChildren=(children)=>{
returnchildren.reduce((acc,node)=>{
if(node.type==="link"){
returnacc+collapseHeaderChildren(node.children);
}
// Prevents 'undefined' values in our headers
elseif(node.value==null){
returnacc;
}
returnacc+node.value;
},"");
};
// sidebarJson: { [category: string]: array<plain_filename_without_ext> }
constprocessFile=(filepath,sidebarJson={})=>{
constcontent=fs.readFileSync(filepath,"utf8");
constresult=defaultProcessor.processSync(content);
constdata=result.data.matter;
constpagesPath=path.resolve("./pages");
constrelFilepath=path.relative(pagesPath,filepath);
constparsedPath=path.parse(relFilepath);
constfilename=path.basename(filepath,path.extname(filepath));
consttitle=data.title||result.data.mainHeader||filename;
letcategory;
for(const[categoryName,items]ofObject.entries(sidebarJson)){
if(items.find((item)=>filename===item)){
category=categoryName;
break;
}
}
constdataset={
id: filename,
headers: result.data.headers,
href: path.join(parsedPath.dir,parsedPath.name),
title,
};
if(category!=null){
dataset.category=category;
}
returndataset;
};
constcreateTOC=(result)=>{
// Currently we reorder the data to a map, the key is
// reflected as the router pathname, as defined by the
// NextJS router
returnresult.reduce((acc,data)=>{
const{ title, headers, category, id }=data;
acc["/"+data.href]={
id,
title,
headers,
category,
};
returnacc;
},{});
};
constcreateManualToc=(version)=>{
constversionNoDot=version.replaceAll(".","");
constMD_DIR=path.join(__dirname,`../pages/docs/manual/${version}`);
constSIDEBAR_JSON=path.join(
__dirname,
`../data/sidebar_manual_${versionNoDot}.json`
);
constTARGET_FILE=path.join(
__dirname,
`../index_data/manual_${versionNoDot}_toc.json`
);
constsidebarJson=JSON.parse(fs.readFileSync(SIDEBAR_JSON));
constFILE_ORDER=Object.values(sidebarJson).reduce((acc,items)=>{
returnacc.concat(items);
},[]);
constfiles=glob.sync(`${MD_DIR}/*.?(js|md?(x))`);
constordered=orderFiles(files,FILE_ORDER);
constresult=ordered.map((filepath)=>processFile(filepath,sidebarJson));
consttoc=createTOC(result);
fs.writeFileSync(TARGET_FILE,JSON.stringify(toc),"utf8");
};
constcreateReactToc=(version)=>{
constversionLabel=version.replace(/\./g,"");
constMD_DIR=path.join(__dirname,"../pages/docs/react");
constSIDEBAR_JSON=path.join(
__dirname,
`../data/sidebar_react_${versionLabel}.json`
);
constTARGET_FILE=path.join(
__dirname,
`../index_data/react_${versionLabel}_toc.json`
);
constsidebarJson=JSON.parse(fs.readFileSync(SIDEBAR_JSON));
constFILE_ORDER=Object.values(sidebarJson).reduce((acc,items)=>{
returnacc.concat(items);
},[]);
constfiles=glob.sync(`${MD_DIR}/${version}/*.md?(x)`);
constordered=orderFiles(files,FILE_ORDER);
constresult=ordered.map((filepath)=>processFile(filepath,sidebarJson));
consttoc=createTOC(result);
fs.writeFileSync(TARGET_FILE,JSON.stringify(toc),"utf8");
};
constcreateCommunityToc=()=>{
constMD_DIR=path.join(__dirname,"../pages/community");
constSIDEBAR_JSON=path.join(__dirname,"../data/sidebar_community.json");
constTARGET_FILE=path.join(__dirname,"../index_data/community_toc.json");
constsidebarJson=JSON.parse(fs.readFileSync(SIDEBAR_JSON));
constFILE_ORDER=Object.values(sidebarJson).reduce((acc,items)=>{
returnacc.concat(items);
},[]);
constfiles=glob.sync(`${MD_DIR}/*.?(js|md?(x))`);
constordered=orderFiles(files,FILE_ORDER);
constresult=ordered.map((filepath)=>processFile(filepath,sidebarJson));
consttoc=createTOC(result);
fs.writeFileSync(TARGET_FILE,JSON.stringify(toc),"utf8");
};
constcreateReasonCompilerToc=()=>{
constMD_DIR=path.join(__dirname,"../pages/docs/reason-compiler/latest");
constTARGET_FILE=path.join(
__dirname,
"../index_data/reason_compiler_toc.json"
);
constfiles=glob.sync(`${MD_DIR}/*.md?(x)`);
constresult=files.map(processFile);
consttoc=createTOC(result);
fs.writeFileSync(TARGET_FILE,JSON.stringify(toc),"utf8");
};
/*
const debugToc = () => {
const MD_DIR = path.join(__dirname, "../pages/docs/manual/latest");
const files = glob.sync(`${MD_DIR}/introduction.md?(x)`);
const result = files.map(processFile);
const toc = createTOC(result);
console.log(JSON.stringify(toc, null, 2));
};
debugToc();
*/
letmanualVersions=["v12.0.0","v11.0.0","v10.0.0","v9.0.0","v8.0.0"];
letreactManualVersions=["latest","v0.10.0","v0.11.0"];
manualVersions.forEach(createManualToc);
reactManualVersions.forEach(createReactToc);
createCommunityToc();
createReasonCompilerToc();