@@ -3,6 +3,9 @@ import StarlightPage from '@astrojs/starlight/components/StarlightPage.astro';
3
3
import { getCollection } from ' astro:content' ;
4
4
import { getFunctionInfo } from ' @src/utils/functions' ;
5
5
import { marked } from ' marked' ;
6
+ import fs from " fs" ;
7
+ import path from " path" ;
8
+ import { Code } from ' @astrojs/starlight/components' ;
6
9
7
10
export async function getStaticPaths() {
8
11
const functions = await getCollection (' functions' );
@@ -15,11 +18,24 @@ export async function getStaticPaths() {
15
18
const { func } = Astro .props ;
16
19
17
20
const funcInfo = getFunctionInfo (func .data );
18
-
19
21
const funcType = funcInfo .type ;
20
22
const funcTypePretty = funcInfo .typePretty ;
21
23
22
24
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
+ }
23
39
---
24
40
25
41
<StarlightPage frontmatter ={ {
@@ -35,4 +51,12 @@ const funcPair = funcInfo.pair;
35
51
36
52
<!-- Description -->
37
53
<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
+
38
62
</StarlightPage >
0 commit comments