Skip to content

Commit 26d4088

Browse files
committed
Process signatures and have links between types in modules.
1 parent 0bf7964 commit 26d4088

File tree

5 files changed

+89
-9
lines changed

5 files changed

+89
-9
lines changed

docs/components/record.astro

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
import { micromark } from "micromark";
3+
import Signature from "./signature.astro";
34
const { name, items, typesInOwnModule } = Astro.props;
45
---
56

@@ -13,11 +14,10 @@ const { name, items, typesInOwnModule } = Astro.props;
1314
<div class="record_field">
1415
<h5 id={`${name}_${item.name}`}>{item.name}</h5>
1516
<div class="type">
16-
{typesInOwnModule.has(item.signature) ? (
17-
<a href={`#${item.signature}`}>{item.signature}</a>
18-
) : (
19-
item.signature
20-
)}
17+
<Signature
18+
signature={item.signature}
19+
typesInOwnModule={typesInOwnModule}
20+
/>
2121
</div>
2222
{documentation && <div class="doc" set:html={documentation} />}
2323
</div>
@@ -37,6 +37,7 @@ const { name, items, typesInOwnModule } = Astro.props;
3737
& .type {
3838
justify-self: end;
3939
color: var(--sl-color-accent-low);
40+
margin-top: 0;
4041
}
4142

4243
& .doc {

docs/components/signature.astro

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
import SignatureItem from "./signatureItem.astro";
3+
4+
5+
function tokenize(input) {
6+
// Split by special characters while keeping them
7+
const regex = /([<>,])/g;
8+
return input
9+
.split(regex)
10+
.map((token) => token.trim())
11+
.filter(Boolean);
12+
}
13+
14+
function parseTokens(tokens) {
15+
let index = 0;
16+
17+
const parseNode = () => {
18+
const ident = tokens[index++]; // Read the current token and increment index
19+
let genericArguments = [];
20+
21+
if (tokens[index] === "<") {
22+
// Check for generics
23+
index++; // Consume "<"
24+
while (tokens[index] !== ">") {
25+
genericArguments.push(parseNode());
26+
if (tokens[index] === ",") index++; // Consume ","
27+
}
28+
index++; // Consume ">"
29+
}
30+
31+
return { ident, genericArguments };
32+
};
33+
34+
return parseNode();
35+
}
36+
37+
function parse(input) {
38+
const tokens = tokenize(input);
39+
return parseTokens(tokens);
40+
}
41+
42+
43+
const { signature, typesInOwnModule } = Astro.props;
44+
const tree = parse(signature);
45+
---
46+
47+
<div class="signature">
48+
{(<SignatureItem typesInOwnModule={typesInOwnModule} item={tree} />)}
49+
</div>

docs/components/signatureItem.astro

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
import { createModuleLink } from "../pages/apidocs/utils.js";
3+
const { item, typesInOwnModule } = Astro.props;
4+
5+
let link;
6+
if (typesInOwnModule.has(item.ident)) {
7+
link = `#${item.ident}`;
8+
}
9+
10+
if (item.ident.startsWith("WebAPI.")) {
11+
const idents = item.ident.split(".");
12+
if (idents.length === 3){
13+
link = `${createModuleLink(idents[1])}#${idents[2]}`;
14+
}
15+
}
16+
---
17+
18+
{link ? <a href={link}>{item.ident}</a> : item.ident}
19+
{
20+
item.genericArguments.length > 0 && (
21+
<>
22+
{"<"}
23+
{item.genericArguments.map((subItem) => (
24+
<Astro.self typesInOwnModule={typesInOwnModule} item={subItem} />
25+
))}
26+
{">"}
27+
</>
28+
)
29+
}

docs/pages/apidocs/[API].astro

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,6 @@ const headings = [
7979
</div>
8080
))
8181
}
82-
<code>
83-
<pre>{JSON.stringify(docInfo, null, 4)}</pre>
84-
</code>
8582
</div>
8683
</StarlightPage>
8784
<style>

docs/pages/apidocs/utils.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@ function toKebabCase(input) {
1111
.toLowerCase(); // Convert to lowercase
1212
}
1313

14+
export function createModuleLink(moduleName) {
15+
return `/apidocs/${toKebabCase(moduleName)}`;
16+
}
17+
1418
function mapRescriptFile(file) {
1519
const moduleName = path
1620
.basename(file, ".res")
1721
.replace("$", "")
1822
.replace("API", " API");
19-
const link = `apidocs/${toKebabCase(moduleName)}`;
23+
const link = createModuleLink(moduleName);
2024
return {
2125
filePath: path.join(import.meta.dirname, file),
2226
moduleName,

0 commit comments

Comments
 (0)