Skip to content

Commit 53416a8

Browse files
committed
Add examples
1 parent f036950 commit 53416a8

File tree

3 files changed

+92
-2
lines changed

3 files changed

+92
-2
lines changed

astro.config.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ export default defineConfig({
5151
{
5252
slug: "design-philosophy",
5353
},
54+
{
55+
label: "Examples",
56+
link: "examples",
57+
},
5458
{
5559
slug: "project-status",
5660
},

docs/pages/examples.astro

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
---
2+
import StarlightPage from "@astrojs/starlight/components/StarlightPage.astro";
3+
import { Tabs, TabItem, Code } from "@astrojs/starlight/components";
4+
import { testFiles } from "../utils";
5+
6+
const frontmatter = {
7+
title: "Examples",
8+
};
9+
10+
const headings = testFiles.map(({ name }) => ({
11+
depth: 2,
12+
slug: name,
13+
text: name,
14+
}));
15+
---
16+
17+
<StarlightPage frontmatter={frontmatter} headings={headings}>
18+
<p>
19+
The best example of usage of these bindings are the <a href="./06-testing"
20+
>tests</a
21+
>. <br />
22+
These typically contain tweaks made to the generated bindings to make them more
23+
ergonomic.
24+
</p>
25+
{
26+
testFiles.map(({ source, output, name }) => {
27+
return (
28+
<div class="example" id={name}>
29+
<h2>{name}</h2>
30+
<Tabs>
31+
<TabItem label="ReScript">
32+
<Code code={source} lang="ReScript" frame="none" />
33+
</TabItem>
34+
<TabItem label="JS Output">
35+
<Code code={output} lang="js" frame="none" />
36+
</TabItem>
37+
</Tabs>
38+
</div>
39+
);
40+
})
41+
}
42+
</StarlightPage>
43+
<style>
44+
.example {
45+
margin-block: 4rem;
46+
47+
&:first-of-type {
48+
margin-top: 4rem;
49+
}
50+
51+
& h2 {
52+
margin-bottom: 1rem;
53+
font-size: var(--sl-text-2xl);
54+
}
55+
}
56+
</style>

docs/utils.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as path from "node:path";
22
import { exec } from "node:child_process";
33
import { promisify } from "node:util";
4-
import { readdirSync, existsSync } from "fs";
4+
import { readdirSync, existsSync, readFileSync } from "fs";
55
import { micromark } from "micromark";
66

77
const execAsync = promisify(exec);
@@ -140,4 +140,34 @@ export async function getDoc(absoluteFilePath) {
140140
values,
141141
valueHeadings,
142142
};
143-
}
143+
}
144+
145+
function trimRescriptOutput(output) {
146+
return output
147+
.replace("// Generated by ReScript, PLEASE EDIT WITH CARE", "")
148+
.replace("/* response Not a pure module */", "")
149+
.trim();
150+
}
151+
152+
const testDir = path.resolve(process.cwd(), "tests");
153+
export const testFiles =
154+
readdirSync(testDir, { recursive: true })
155+
.filter(f => f.endsWith(".res"))
156+
.map(tf => {
157+
const sourcePath = path.join(testDir, tf);
158+
const source = readFileSync(sourcePath, "utf-8");
159+
const outputPath = sourcePath.replace(".res", ".js");
160+
const output = readFileSync(outputPath, "utf-8");
161+
162+
const parts = tf.split(path.sep);
163+
const name = parts[parts.length - 1].replace("__tests.res", "");
164+
165+
return {
166+
sourcePath: sourcePath.replace(testDir,""),
167+
source,
168+
output: trimRescriptOutput(output),
169+
outputPath: outputPath.replace(testDir,""),
170+
name,
171+
};
172+
})
173+
.sort((a, b) => a.name.localeCompare(b.name));

0 commit comments

Comments
 (0)