Skip to content

Commit 3c74fe8

Browse files
committed
Very rough generation of API documentation
1 parent a2dbeeb commit 3c74fe8

File tree

9 files changed

+207
-54
lines changed

9 files changed

+207
-54
lines changed

.prettierrc.mjs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/** @type {import("prettier").Config} */
2+
export default {
3+
plugins: ["prettier-plugin-astro"],
4+
overrides: [
5+
{
6+
files: "*.astro",
7+
options: {
8+
parser: "astro",
9+
},
10+
},
11+
],
12+
endOfLine: "lf",
13+
};

astro.config.mjs

+25-35
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,17 @@
11
import { readFileSync } from "node:fs";
22
import { defineConfig } from "astro/config";
33
import starlight from "@astrojs/starlight";
4-
5-
const starlightPlugin = {
6-
name: "RescriptAPIDocs",
7-
hooks: {
8-
setup: ({ config, updateConfig, addIntegration }) => {
9-
console.log(`Setup yow!`)
10-
addIntegration({
11-
name: "RescriptAPIDocs",
12-
hooks: {
13-
"astro:config:setup": ({ injectRoute }) => {
14-
injectRoute({
15-
pattern: "meh",
16-
entrypoint: "docs/apidocs/meh.astro"
17-
})
18-
}
19-
}
20-
})
21-
22-
updateConfig({
23-
sidebar: [
24-
...config.sidebar,
25-
{ label: "meh", link: "meh" }
26-
]
27-
});
28-
}
29-
}
30-
}
4+
import { apiModules } from "./docs/pages/apidocs/utils";
315

326
const rescriptTM = JSON.parse(
337
readFileSync("./docs/assets/rescript.tmLanguage.json", "utf-8"),
348
);
359

10+
const apiSidebarItems = apiModules.map(({ moduleName, link }) => ({
11+
label: moduleName,
12+
link,
13+
}));
14+
3615
export default defineConfig({
3716
srcDir: "docs",
3817
publicDir: "docs/public",
@@ -45,24 +24,36 @@ export default defineConfig({
4524
src: "./docs/assets/rescript-logo.svg",
4625
},
4726
social: {
48-
github: 'https://github.com/rescript-lang/experimental-rescript-webapi',
27+
github: "https://github.com/rescript-lang/experimental-rescript-webapi",
4928
},
5029
editLink: {
51-
baseUrl: 'https://github.com/rescript-lang/experimental-rescript-webapi/edit/main/',
30+
baseUrl:
31+
"https://github.com/rescript-lang/experimental-rescript-webapi/edit/main/",
5232
},
5333
sidebar: [
5434
{
55-
slug: '',
35+
slug: "",
36+
},
37+
{
38+
slug: "design-philosophy",
5639
},
5740
{
58-
slug: 'design-philosophy',
41+
slug: "project-status",
5942
},
6043
{
61-
slug: 'project-status',
44+
label: "Contributing",
45+
autogenerate: { directory: "contributing" },
6246
},
6347
{
64-
label: 'Contributing',
65-
autogenerate: { directory: 'contributing' },
48+
label: "API Documentation",
49+
collapsed: true,
50+
items: [
51+
{
52+
label: "Overview",
53+
link: "apidocs",
54+
},
55+
...apiSidebarItems,
56+
],
6657
},
6758
],
6859
customCss: ["./docs/styles/fonts.css", "./docs/styles/theme.css"],
@@ -71,7 +62,6 @@ export default defineConfig({
7162
langs: [rescriptTM],
7263
},
7364
},
74-
plugins: [starlightPlugin],
7565
}),
7666
],
7767
});

docs/apidocs/meh.astro

-11
This file was deleted.

docs/pages/apidocs/[API].astro

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
import { apiModules, getDoc } from "./utils";
3+
import StarlightPage from "@astrojs/starlight/components/StarlightPage.astro";
4+
5+
export async function getStaticPaths() {
6+
return apiModules.map((apiModule) => {
7+
return {
8+
params: {
9+
API: apiModule.apiRoute,
10+
},
11+
props: apiModule,
12+
};
13+
});
14+
}
15+
16+
const { moduleName, filePath } = Astro.props;
17+
18+
const frontmatter = {
19+
title: moduleName,
20+
};
21+
22+
const headings = [
23+
// {
24+
// depth: 2,
25+
// slug: "web-apis",
26+
// text: "Web APIs",
27+
// },
28+
];
29+
30+
const docInfo = await getDoc(filePath);
31+
---
32+
33+
<StarlightPage frontmatter={frontmatter} headings={headings}>
34+
<h2>{moduleName} overview</h2>
35+
<code>
36+
<pre>{JSON.stringify(docInfo, null, 4)}</pre>
37+
</code>
38+
</StarlightPage>

docs/pages/apidocs/index.astro

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
import StarlightPage from "@astrojs/starlight/components/StarlightPage.astro";
3+
import { apiModules } from "./utils";
4+
5+
const frontmatter = {
6+
title: "API Documentation",
7+
};
8+
9+
const headings = [
10+
{
11+
depth: 2,
12+
slug: "web-apis",
13+
text: "Web APIs",
14+
},
15+
];
16+
17+
const resFiles = apiModules;
18+
---
19+
20+
<StarlightPage frontmatter={frontmatter} headings={headings}>
21+
<h2 id="web-apis">Web APIs</h2>
22+
<ul>
23+
{
24+
resFiles.map(({ moduleName, link }) => (
25+
<li>
26+
<a href={link}>{moduleName}</a>
27+
</li>
28+
))
29+
}
30+
</ul>
31+
</StarlightPage>

docs/pages/apidocs/utils.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import * as path from "node:path";
2+
import { exec } from "node:child_process";
3+
import { promisify } from "node:util";
4+
5+
const execAsync = promisify(exec);
6+
7+
function toKebabCase(input) {
8+
return input
9+
.replace(/([a-z])([A-Z])/g, "$1-$2") // Insert dash between lowercase and uppercase
10+
.replace(/[\s_]+/g, "-") // Replace spaces or underscores with dash
11+
.toLowerCase(); // Convert to lowercase
12+
}
13+
14+
function mapRescriptFile(file) {
15+
const moduleName = path
16+
.basename(file, ".res")
17+
.replace("$", "")
18+
.replace("API", " API");
19+
const link = `apidocs/${toKebabCase(moduleName)}`;
20+
return {
21+
filePath: path.join(import.meta.dirname, file),
22+
moduleName,
23+
link,
24+
apiRoute: toKebabCase(moduleName),
25+
};
26+
}
27+
28+
export const apiModules = Object.keys(
29+
import.meta.glob("../../../src/*.res"),
30+
).map(mapRescriptFile);
31+
32+
export const fileModules = Object.keys(
33+
import.meta.glob("../../../src/**/*.res"),
34+
).map(mapRescriptFile);
35+
36+
export async function getDoc(absoluteFilePath) {
37+
const { stdout, stderr } = await execAsync(
38+
`rescript-tools doc ${absoluteFilePath}`,
39+
{
40+
maxBuffer: 1024 * 1024 * 10, // Increase buffer to 10 MB
41+
},
42+
);
43+
if (stderr) {
44+
throw new Error(stderr);
45+
}
46+
return JSON.parse(stdout);
47+
}

package-lock.json

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

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,19 @@
2929
"scripts": {
3030
"test": "node tests/index.js",
3131
"build": "rewatch",
32-
"format": "rescript format -all && prettier --write ./tests/index.js ./package.json",
32+
"format": "rescript format -all && prettier --write ./tests/index.js ./package.json ./docs/pages",
3333
"docs": "astro dev",
3434
"build:docs": "astro build"
3535
},
3636
"license": "MIT",
3737
"dependencies": {
38-
"rescript": "^12.0.0-alpha.4"
38+
"rescript": "^12.0.0-alpha.5"
3939
},
4040
"devDependencies": {
4141
"@astrojs/starlight": "^0.29.0",
4242
"astro": "^4.16.10",
4343
"prettier": "^3.3.3",
44+
"prettier-plugin-astro": "^0.14.1",
4445
"sharp": "^0.33.5"
4546
}
4647
}

src/DOMAPI/Window.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)