Skip to content

Commit 5c225d5

Browse files
committed
Added scripts
1 parent a9aa6ae commit 5c225d5

12 files changed

+21036
-4738
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
dist
22
node_modules
33
tsconfig.tsbuildinfo
4+
debugging
5+
.vscode
6+
vscode*
7+
table-generation/generated

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2022 Jason Siefken
3+
Copyright (c) 2015-2022 Michael Brade, Jason Siefken
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

package-lock.json

+6,830-4,737
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+7
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,20 @@
1717
"esbuild": "^0.14.39",
1818
"esbuild-jest": "^0.5.0",
1919
"esbuild-node-externals": "^1.4.1",
20+
"esbuild-runner": "^2.2.1",
2021
"jest": "^28.1.0",
22+
"mdast-builder": "^1.1.1",
23+
"mdast-util-inject": "^1.1.0",
2124
"mkdirp": "^1.0.4",
2225
"pegjs": "^0.10.0",
2326
"pegjs-loader": "^0.5.6",
2427
"prettier": "^2.6.2",
2528
"prettier-plugin-pegjs": "^0.5.0",
29+
"remark-gfm": "^3.0.1",
30+
"remark-parse": "^10.0.1",
31+
"remark-stringify": "^10.0.2",
2632
"ts-jest": "^28.0.2",
33+
"ts-morph": "^14.0.0",
2734
"typescript": "^4.6.4"
2835
},
2936
"dependencies": {

scripts/build-docs.ts

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/**
2+
* Autogenerate documentation for source files. This assumes a very specific format.
3+
*
4+
* run with
5+
* ```
6+
* npx esr scripts/build-docs.ts
7+
* ```
8+
*/
9+
10+
import * as fs from "node:fs/promises";
11+
import * as path from "node:path";
12+
import { Project, Node } from "ts-morph";
13+
14+
const README_COMMENT: string = `<!-- DO NOT MODIFY -->
15+
<!-- This file was autogenerated by build-docs.ts -->
16+
<!-- Edit the docstring in index.ts and regenerate -->
17+
<!-- rather than editing this file directly. -->
18+
`;
19+
20+
// We can only import ESM modules via async import, so all the action happens here.
21+
(async function () {
22+
const chalk = await (await import("chalk")).default;
23+
const { unified } = await import("unified");
24+
const inject = await (await import("mdast-util-inject")).default;
25+
const b = await import("mdast-builder");
26+
const { getDataOnAllExports } = await import("./libs/get-symbols");
27+
const {
28+
gfmRemark,
29+
makeConstantsSection,
30+
makeFunctionsSection,
31+
makeTypesSection,
32+
makePluginsSection,
33+
} = await (
34+
await import("./libs/markdown-utils")
35+
).default;
36+
type MdNode = ReturnType<typeof b.text>;
37+
38+
///////////////////////////////////////////////////////////
39+
// Build the documentation for all exports
40+
///////////////////////////////////////////////////////////
41+
const project = new Project();
42+
project.addSourceFilesAtPaths("./packages/**/*.ts");
43+
44+
// Find all the files we want to process. These have th form .../unified-latex.../index.ts,
45+
// however we only want the top-level index.ts files (no any that may be in subdirectories).
46+
47+
const filesToProcess = project
48+
.getSourceFiles()
49+
.filter((file) =>
50+
file.getFilePath().match(/unified-latex[^/]*\/index\.ts/)
51+
);
52+
53+
console.log(chalk.bold("Processing"), filesToProcess.length, "files");
54+
for (const file of filesToProcess) {
55+
console.log(
56+
" ",
57+
chalk.magenta(file.getDirectory().getBaseName()),
58+
`/ ${file.getBaseName()}`
59+
);
60+
61+
const outPath = path.join(file.getDirectoryPath(), "README.md");
62+
63+
const readme = generateReadme(file.getFilePath());
64+
65+
console.log(
66+
chalk.gray(" Writing to", path.relative(__dirname, outPath))
67+
);
68+
69+
await fs.writeFile(outPath, readme, { encoding: "utf-8" });
70+
}
71+
72+
/*
73+
const filename =
74+
// "./packages/unified-latex/unified-latex-types/index.ts";
75+
"./packages/unified-latex/unified-latex-util-replace/index.ts";
76+
//"./packages/unified-latex/unified-latex-util-trim/index.ts";
77+
//"./packages/unified-latex/unified-latex-util-visit/index.ts";
78+
*/
79+
80+
function generateReadme(filename): string {
81+
const sourceFile = project.getSourceFileOrThrow(filename);
82+
83+
const accumulatedExports = getDataOnAllExports(sourceFile);
84+
const constantsMd = makeConstantsSection(accumulatedExports.constants);
85+
const typesMd = makeTypesSection(accumulatedExports.types);
86+
const funcsMd = makeFunctionsSection(accumulatedExports.funcs);
87+
const pluginMd = makePluginsSection(accumulatedExports.plugins);
88+
89+
// Grab the comments from index.ts
90+
const jsDoc = sourceFile.getLastToken().getFirstChild();
91+
let introText = "";
92+
if (Node.isJSDoc(jsDoc)) {
93+
introText = jsDoc.getCommentText();
94+
}
95+
96+
const directoryName = sourceFile.getDirectory().getBaseName();
97+
let skeletonReadme = `# ${directoryName}`;
98+
if (introText) {
99+
skeletonReadme += "\n\n" + introText;
100+
}
101+
if (accumulatedExports.plugins.length > 0) {
102+
skeletonReadme += "\n\n# Plugins";
103+
}
104+
if (accumulatedExports.funcs.length > 0) {
105+
skeletonReadme += "\n\n# Functions";
106+
}
107+
if (accumulatedExports.constants.length > 0) {
108+
skeletonReadme += "\n\n# Constants";
109+
}
110+
if (accumulatedExports.types.length > 0) {
111+
skeletonReadme += "\n\n# Types";
112+
}
113+
114+
// Assemble the dynamically-generated data
115+
const target = gfmRemark.parse(skeletonReadme);
116+
117+
if (accumulatedExports.plugins.length > 0) {
118+
inject("Plugins", target, pluginMd);
119+
}
120+
if (accumulatedExports.funcs.length > 0) {
121+
inject("Functions", target, funcsMd);
122+
}
123+
if (accumulatedExports.constants.length > 0) {
124+
inject("Constants", target, constantsMd);
125+
}
126+
if (accumulatedExports.types.length > 0) {
127+
inject("Types", target, typesMd);
128+
}
129+
130+
const renderedReadme = `${README_COMMENT}${gfmRemark.stringify(
131+
target
132+
)}`;
133+
134+
return renderedReadme;
135+
}
136+
})();

0 commit comments

Comments
 (0)