Skip to content

Commit ade3442

Browse files
committed
code visualization
1 parent 46c6b19 commit ade3442

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

web/src/pages/[func].astro

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import StarlightPage from '@astrojs/starlight/components/StarlightPage.astro';
33
import { getCollection } from 'astro:content';
44
import { getFunctionInfo } from '@src/utils/functions';
55
import { marked } from 'marked';
6+
import fs from "fs";
7+
import path from "path";
8+
import { Code } from '@astrojs/starlight/components';
69
710
export async function getStaticPaths() {
811
const functions = await getCollection('functions');
@@ -15,11 +18,24 @@ export async function getStaticPaths() {
1518
const { func } = Astro.props;
1619
1720
const funcInfo = getFunctionInfo(func.data);
18-
1921
const funcType = funcInfo.type;
2022
const funcTypePretty = funcInfo.typePretty;
2123
2224
const funcPair = funcInfo.pair;
25+
const funcPath = path.dirname(func.filePath ?? "")
26+
let funcExamples = funcInfo.examples
27+
28+
if ( funcExamples.length > 0 ){
29+
funcExamples = funcInfo.examples.map((example: any) => {
30+
try {
31+
const luaCode = fs.readFileSync(path.resolve(`${funcPath}`, example.path), "utf8");
32+
return { ...example, luaCode };
33+
} catch (error) {
34+
console.error(`Error reading ${example.path}:`, error);
35+
return { ...example, luaCode: "Loading example error." };
36+
}
37+
});
38+
}
2339
---
2440

2541
<StarlightPage frontmatter={{
@@ -35,4 +51,12 @@ const funcPair = funcInfo.pair;
3551

3652
<!-- Description -->
3753
<Fragment set:html={marked(funcInfo.description)} />
54+
55+
{funcExamples.length > 0 && funcExamples.map((example: any) => (
56+
<div>
57+
<p set:html={marked(example.description)}></p>
58+
<Code code={example.luaCode} lang="lua" title={path.basename(example.path)} />
59+
</div>
60+
))}
61+
3862
</StarlightPage>

web/src/utils/functions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export function getFunctionInfo(data: FunctionData): any {
3636
type: getFunctionType(data),
3737
typePretty: getFunctionTypePretty(data),
3838
pair: data.shared?.pair || data.client?.pair || data.server?.pair || false,
39+
examples: data.shared?.examples || data.client?.examples || data.server?.examples || [ ],
3940
};
4041
}
4142

0 commit comments

Comments
 (0)