Skip to content

Commit 684bee4

Browse files
Fix function examples display
1 parent 2f72a06 commit 684bee4

File tree

3 files changed

+42
-56
lines changed

3 files changed

+42
-56
lines changed

web/src/pages/[func].astro

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,30 @@ const funcType = funcInfo.type;
2727
const funcPair = funcInfo.pair;
2828
const funcPath = path.dirname(func.filePath ?? "");
2929
30-
const { description, notes: funcNotes, examples: rawExamples } = funcInfo;
31-
32-
let processedExamples: any[] = [];
33-
if (Array.isArray(rawExamples) && rawExamples.length > 0) {
34-
processedExamples = rawExamples.map((example: any) => {
35-
try {
36-
const exampleFilePath = path.resolve(funcPath, example.path);
37-
const luaCode = fs.readFileSync(exampleFilePath, "utf8");
38-
return { ...example, luaCode };
39-
} catch (error) {
40-
console.error(`Error reading example file ${example.path} at ${path.resolve(funcPath, example.path)}:`, error);
41-
return { ...example, luaCode: `Error loading example: ${example.path}` };
42-
}
43-
});
30+
// join shared, client and server examples (in func.data) if any
31+
let funcExamples = [];
32+
if (func.data.shared && func.data.shared.examples) {
33+
funcExamples = [...funcExamples, ...func.data.shared.examples];
4434
}
35+
if (func.data.client && func.data.client.examples) {
36+
funcExamples = [...funcExamples, ...func.data.client.examples];
37+
}
38+
if (func.data.server && func.data.server.examples) {
39+
funcExamples = [...funcExamples, ...func.data.server.examples];
40+
}
41+
funcExamples = funcExamples.map((example: any) => {
42+
try {
43+
const luaCode = fs.readFileSync(path.resolve(`${funcPath}`, example.path), "utf8");
44+
return { ...example, luaCode };
45+
} catch (error) {
46+
console.error(`Error reading ${example.path}:`, error);
47+
return { ...example, luaCode: "Loading example error." };
48+
}
49+
});
4550
4651
let notesContent: NotesType = [];
47-
if (Array.isArray(funcNotes) && funcNotes.length > 0) {
48-
notesContent = funcNotes;
52+
if (Array.isArray(funcInfo.notes) && funcInfo.notes.length > 0) {
53+
notesContent = funcInfo.notes;
4954
}
5055
5156
let funcSyntaxes = parseFunctionSyntaxes(func.id, func.data);
@@ -63,7 +68,7 @@ let funcSyntaxes = parseFunctionSyntaxes(func.id, func.data);
6368
)}
6469

6570
<!-- Description -->
66-
{description && <Fragment set:html={marked(description)} />}
71+
{funcInfo.description && <Fragment set:html={marked(funcInfo.description)} />}
6772

6873
<!-- Notes -->
6974
{notesContent.length > 0 && (
@@ -79,7 +84,8 @@ let funcSyntaxes = parseFunctionSyntaxes(func.id, func.data);
7984
<!-- OOP Syntax -->
8085
{funcInfo.oop && (
8186
<>
82-
<h3>OOP Syntax <a class="small-text" href="/OOP_Introduction">Help! I don't understand this!</a></h3>
87+
<div class="function-oop">
88+
<h4>OOP Syntax <a class="small-text" href="/OOP_Introduction">Help! I don't understand this!</a></h4>
8389
<ul>
8490
{funcInfo.oop.method && (
8591
<li>
@@ -105,17 +111,19 @@ let funcSyntaxes = parseFunctionSyntaxes(func.id, func.data);
105111
<strong>Counterpart:</strong> <a href={ `/${funcPair}` }>{ funcPair }</a>
106112
</li>
107113
)}
114+
108115
</ul>
116+
</div>
109117
</>
110118
)}
111119

112120
<!-- Syntaxes -->
113121
{funcSyntaxes.length > 0 && funcSyntaxes.map((syntax: any) => (
114122
<div class="function-syntax">
115123
{funcType === syntax.type && (
116-
<h3>Syntax</h3>
124+
<h4>Syntax</h4>
117125
) || (
118-
<h3>{syntax.type.charAt(0).toUpperCase() + syntax.type.slice(1)} Syntax</h3>
126+
<h4>{syntax.type.charAt(0).toUpperCase() + syntax.type.slice(1)} Syntax</h4>
119127
)}
120128
<Code code={syntax.syntaxString} lang="c" />
121129
{syntax.parameters.length > 0 && (
@@ -154,7 +162,7 @@ let funcSyntaxes = parseFunctionSyntaxes(func.id, func.data);
154162
</>
155163
)}
156164
{syntax.returns && (
157-
<h5>Returns</h5>
165+
<h4>Returns</h4>
158166
<ul>
159167
{syntax.returns.values.map((ret: any) => (
160168
<li set:html={"<strong>" + ret.type + "</strong>: " + renderInlineMarkdown(ret.name)} />
@@ -166,10 +174,10 @@ let funcSyntaxes = parseFunctionSyntaxes(func.id, func.data);
166174
))}
167175

168176
<!-- Examples -->
169-
{processedExamples.length > 0 && (
177+
{funcExamples.length > 0 && (
170178
<div class="examples-section">
171-
<h3>Code Examples</h3>
172-
{processedExamples.map((example: any) => (
179+
<h4>Code Examples</h4>
180+
{funcExamples.map((example: any) => (
173181
<div class="function-example">
174182
<Fragment set:html={marked(example.description)} />
175183
<Code code={example.luaCode} lang="lua"/>

web/src/styles/function-page.css

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
.function-oop h3,
1515
.examples-section h3 {
1616
margin-bottom: 0.75rem;
17-
font-size: 1.25em;
1817
border-bottom: 1px solid var(--sl-color-gray-5);
1918
padding-bottom: 0.25rem;
2019
}

web/src/utils/functions.ts

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@ type ConstructorOOP = BaseOOP & {
2727

2828
export type OOPInfo = MethodOOP | ConstructorOOP;
2929

30-
type FunctionDetails = {
30+
export type FunctionInfo = {
31+
description: string;
32+
type: FunctionType;
33+
typePretty: string;
34+
pair?: string;
3135
oop?: OOPInfo;
32-
description?: string;
33-
pair?: boolean;
34-
examples?: { code: string; description?: string }[];
3536
notes?: NotesType;
3637
};
3738

@@ -49,12 +50,6 @@ export type FunctionData = {
4950
server?: any;
5051
};
5152

52-
export type TypedFunctionData = {
53-
shared?: FunctionDetails;
54-
client?: FunctionDetails;
55-
server?: FunctionDetails;
56-
};
57-
5853
export const functionTypePrettyName = {
5954
'client': 'Client-side',
6055
'server': 'Server-side',
@@ -96,15 +91,6 @@ type Syntax = {
9691
syntaxString: string;
9792
};
9893

99-
export type FunctionInfo = {
100-
oop?: OOPInfo;
101-
description: string;
102-
type: FunctionType;
103-
typePretty: string;
104-
pair: boolean;
105-
examples: { code: string; description?: string }[];
106-
notes?: NotesType;
107-
};
10894

10995
// return_type func_name ( param1, param2, [ optional param1 ] )
11096
// e.g. bool setCursorPosition ( int cursorX, int cursorY )
@@ -214,19 +200,12 @@ export function parseFunctionSyntaxes(funcName: string, funcData: FunctionData):
214200
return syntaxes;
215201
}
216202

217-
export function getFunctionInfo(data: TypedFunctionData): FunctionInfo {
203+
export function getFunctionInfo(data: FunctionData): FunctionInfo {
218204
const type = getFunctionType(data);
219-
const details = data[type] ?? {};
220-
221-
return {
222-
oop: details.oop || undefined,
223-
description: details.description || '',
224-
type: type,
225-
typePretty: getFunctionTypePretty(data),
226-
pair: details.pair || false,
227-
examples: details.examples || [],
228-
notes: details.notes || []
229-
};
205+
const details: FunctionInfo = data[type];
206+
details.type = type;
207+
details.typePretty = getFunctionTypePretty(data);
208+
return details;
230209
}
231210

232211
const functionsCollection = await getCollection('functions');

0 commit comments

Comments
 (0)