Skip to content

Commit

Permalink
Unify API doc pages
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Nov 25, 2024
1 parent 367af85 commit 5db3fc6
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 176 deletions.
59 changes: 59 additions & 0 deletions docs/components/apidoc.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
---
import { apiModules, getDoc } from "../utils";
import StarlightPage from "@astrojs/starlight/components/StarlightPage.astro";
import Value from "./value.astro";
import Type from "./type.astro";
const { moduleName, filePath, link } = Astro.props;
const { types, typesInOwnModule, typeHeadings, values, valueHeadings } =
await getDoc(filePath);
const frontmatter = {
title: moduleName,
};
const headings = [];
if (types.length > 0) {
headings.push({
depth: 2,
slug: "types",
text: "Types",
});
headings.push(...typeHeadings);
}
if (values.length > 0) {
headings.push({
depth: 2,
slug: "values",
text: "Values",
});
headings.push(...valueHeadings);
}
---
<StarlightPage frontmatter={frontmatter} headings={headings}>
{
types.length > 0 && (
<>
<h2 id="types">Types</h2>
{types.map((type) => (
<Type
type={type}
typesInOwnModule={typesInOwnModule}
filePath={filePath}
link={link}
/>
))}
</>
)
}
{
values.length > 0 && (
<>
<h2 id="values">Values</h2>
{values.map((value) => <Value value={value} />)}
</>
)
}
</StarlightPage>
1 change: 1 addition & 0 deletions docs/components/record.astro
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const { name, items, typesInOwnModule } = Astro.props;
}

& .doc {
margin-top: 0;
padding-bottom: 0.5rem;
grid-column: 1 / -1;
grid-row: 2;
Expand Down
67 changes: 67 additions & 0 deletions docs/components/type.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
import * as path from "node:path";
import { existsSync } from "node:fs";
import { Code } from "@astrojs/starlight/components";
import Record from "./record.astro";
import { createTypeModuleLink } from "../utils";
const { type, typesInOwnModule, filePath, link } = Astro.props;
function showRecord(details) {
return details && details.kind === "record" && details.items.length > 0;
}
function getModuleFileName(typeName) {
return `${typeName[0].toUpperCase()}${typeName.slice(1)}`;
}
/**
* Check if there is a file representing the module for the type.
* The location to be checked is a sub directory with the same name as the current file type.
* @param {string} typeName
* @param {string} filePath
* @returns {boolean}
*/
function showModule(typeName, filePath) {
const moduleFileName = `${getModuleFileName(typeName)}.res`;
const potentialPath = path.join(filePath.replace(".res", ""), moduleFileName);
return existsSync(potentialPath);
}
---

<div class="rescript_type">
<h3 id={type.name}>{type.name}</h3>
<div set:html={type.documentation} />
<Code lang="ReScript" code={type.signature} />
{
showRecord(type.detail) ? (
<Record
name={type.name}
typesInOwnModule={typesInOwnModule}
{...type.detail}
/>
) : null
}
{
showModule(type.name, filePath) && (
<>
<h4>Module</h4>
<p>
There are methods and helpers defined in{" "}
<a
href={`${import.meta.env.BASE_URL}/${createTypeModuleLink(link, type.name)}`}
>
{getModuleFileName(type.name)}
</a>
.
</p>
</>
)
}
</div>
<style>
.rescript_type {
margin-block: 2rem;
}
</style>

33 changes: 24 additions & 9 deletions docs/components/value.astro
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
---
import { Code } from "@astrojs/starlight/components";
import SignatureItem from "./signatureItem.astro";
const { parameters, returnType } = Astro.props;
const { value } = Astro.props;
const details = value.detail?.details || null;
function showValue(detail) {
return detail?.kind === "signature" && detail.details?.parameters?.length > 0;
}
---

<div class="value">
<h4>Parameters</h4>
<div class="rescript_value">
<h3 id={value.name}>{value.name}</h3>
<div set:html={value.documentation} />
<Code lang="ReScript" code={value.signature} />
{
parameters.map((p) => (
<SignatureItem item={p} />
))
showValue(value.detail) && (
<div class="value_detail">
<h4>Parameters</h4>
{details.parameters.map((p) => (
<SignatureItem item={p} />
))}
<h4>Return type</h4>
<SignatureItem item={details.returnType} />
</div>
)
}
<h4>Return type</h4>
<SignatureItem item={returnType} />
</div>

<style>
.value {
.value_detail {
display: flex;
flex-direction: column;
padding-bottom: 1rem;
Expand Down
101 changes: 3 additions & 98 deletions docs/pages/apidocs/[API].astro
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
---
import * as path from "node:path";
import { existsSync } from "fs";
import { apiModules, getDoc, createTypeModuleLink } from "../../utils";
import StarlightPage from "@astrojs/starlight/components/StarlightPage.astro";
import { Code } from "@astrojs/starlight/components";
import { micromark } from "micromark";
import Record from "../../components/record.astro";
import { apiModules } from "../../utils";
import APIDoc from "../../components/apidoc.astro";
export async function getStaticPaths() {
return apiModules.map((apiModule) => {
Expand All @@ -18,97 +13,7 @@ export async function getStaticPaths() {
});
}
function showRecord(details) {
return details && details.kind === "record" && details.items.length > 0;
}
function getModuleFileName(typeName) {
return `${typeName[0].toUpperCase()}${typeName.slice(1)}`;
}
function showModule(typeName, filePath) {
const moduleFileName = `${getModuleFileName(typeName)}.res`;
const potentialPath = path.join(filePath.replace(".res", ""), moduleFileName);
return existsSync(potentialPath);
}
const { moduleName, filePath, link } = Astro.props;
const docInfo = await getDoc(filePath);
const types = docInfo.items
.filter((item) => item.kind === "type")
.sort((a, b) => a.name.localeCompare(b.name))
.map((type) => {
const documentation =
type.docstrings && micromark(type.docstrings.join("\n"));
return {
name: type.name,
documentation,
signature: type.signature,
detail: type.detail,
};
});
const typesInOwnModule = new Set(types.map((t) => t.name));
const typeHeadings = types.map((type) => ({
depth: 3,
slug: type.name,
text: type.name,
}));
const frontmatter = {
title: moduleName,
};
const headings = [
{
depth: 2,
slug: "types",
text: "Types",
},
...typeHeadings,
];
---

<StarlightPage frontmatter={frontmatter} headings={headings}>
<div id="apidocs">
<h2 id="types">Types</h2>
{
types.map((type) => (
<div class="rescript_type">
<h3 id={type.name}>{type.name}</h3>
<div set:html={type.documentation} />
<Code lang="ReScript" code={type.signature} />
{showRecord(type.detail) ? (
<Record
name={type.name}
typesInOwnModule={typesInOwnModule}
{...type.detail}
/>
) : null}
{showModule(type.name, filePath) && (
<>
<h4>Module</h4>
<p>
There are methods and helpers defined in{" "}
<a
href={`${import.meta.env.BASE_URL}/${createTypeModuleLink(link, type.name)}`}
>
{getModuleFileName(type.name)}
</a>
.
</p>
</>
)}
</div>
))
}
</div>
</StarlightPage>
<style>
#apidocs .rescript_type {
margin-block: 2rem;
}
</style>
<APIDoc moduleName={moduleName} filePath={filePath} link={link} />
70 changes: 3 additions & 67 deletions docs/pages/apidocs/[API]/[Module].astro
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
---
import { apiModules, getDoc } from "../../../utils";
import StarlightPage from "@astrojs/starlight/components/StarlightPage.astro";
import { Code } from "@astrojs/starlight/components";
import { micromark } from "micromark";
import Value from "../../../components/value.astro";
import APIDoc from "../../../components/apidoc.astro";
export async function getStaticPaths() {
return apiModules.flatMap((apiModule) => {
Expand All @@ -19,68 +16,7 @@ export async function getStaticPaths() {
});
}
function showValue(detail) {
return (
detail?.kind === "signature" && detail?.details?.parameters?.length > 0
);
}
const { filePath, moduleName } = Astro.props.currentModule;
const docInfo = await getDoc(filePath);
const values = docInfo.items
.filter((item) => item.kind === "value")
.sort((a, b) => a.name.localeCompare(b.name))
.map((value) => {
const documentation =
value.docstrings && micromark(value.docstrings.join("\n"));
return {
name: value.name,
documentation,
signature: value.signature,
detail: value.detail,
};
});
const valueHeadings = values.map((value) => ({
depth: 3,
slug: value.name,
text: value.name,
}));
const frontmatter = {
title: moduleName,
};
const headings = [
{
depth: 2,
slug: "values",
text: "Values",
},
...valueHeadings,
];
const { filePath, moduleName, link } = Astro.props.currentModule;
---

<StarlightPage frontmatter={frontmatter} headings={headings}>
<h2 id="values">Values</h2>
{
values.map((value) => (
<div class="rescript_value">
<h3 id={value.name}>{value.name}</h3>
<div set:html={value.documentation} />
<Code lang="ReScript" code={value.signature} />
{showValue(value.detail) && <Value {...value.detail.details} />}
</div>
))
}
<pre>
{JSON.stringify(docInfo, null, 4)}
</pre>
</StarlightPage>
<style>
pre {
max-width: 100%;
overflow-x: auto;
}
</style>
<APIDoc moduleName={moduleName} filePath={filePath} link={link} />
2 changes: 2 additions & 0 deletions docs/styles/theme.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
::backdrop {
--sl-font: "Inter", "Roboto Mono";
--sl-sidebar-width: 23rem;
--sl-text-3xl: 1.5rem;
--sl-text-2xl: 1.3rem;
}

/*
Expand Down
Loading

0 comments on commit 5db3fc6

Please sign in to comment.