Skip to content

Commit 0667426

Browse files
committed
Adds 'notes' display on function page.
Add 'important notes' property to function schema and update notes display. Refactor style on pages
1 parent 108ff85 commit 0667426

File tree

4 files changed

+99
-28
lines changed

4 files changed

+99
-28
lines changed

schemas/function.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ $defs:
4343
description:
4444
type: string
4545
description: Describes the functionality provided by the function.
46+
important_notes:
47+
type: array
48+
description: |
49+
A list of important notes about the function.
50+
This is a list of things that are important to know about the function, but not
51+
necessarily related to the function itself.
52+
items:
53+
type: string
4654
notes:
4755
type: array
4856
description: List of noteworthy pieces of information for the function.

web/src/pages/[func].astro

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import fs from "fs";
77
import path from "path";
88
import { Code } from '@astrojs/starlight/components';
99
10+
import '@src/styles/function-page.css';
11+
1012
export async function getStaticPaths() {
1113
const functions = await getCollection('functions');
1214
return functions.map(func => ({
@@ -19,24 +21,30 @@ const { func } = Astro.props;
1921
2022
const funcInfo = getFunctionInfo(func.data);
2123
const funcType = funcInfo.type;
22-
const funcTypePretty = funcInfo.typePretty;
23-
2424
const funcPair = funcInfo.pair;
25-
const funcPath = path.dirname(func.filePath ?? "")
26-
let funcExamples = funcInfo.examples
25+
const funcPath = path.dirname(func.filePath ?? "");
26+
27+
const { description, notes: funcNotes, examples: rawExamples } = funcInfo;
2728
28-
if ( funcExamples.length > 0 ){
29-
funcExamples = funcInfo.examples.map((example: any) => {
29+
let processedExamples: any[] = [];
30+
if (Array.isArray(rawExamples) && rawExamples.length > 0) {
31+
processedExamples = rawExamples.map((example: any) => {
3032
try {
31-
const luaCode = fs.readFileSync(path.resolve(`${funcPath}`, example.path), "utf8");
33+
const exampleFilePath = path.resolve(funcPath, example.path);
34+
const luaCode = fs.readFileSync(exampleFilePath, "utf8");
3235
return { ...example, luaCode };
3336
} catch (error) {
34-
console.error(`Error reading ${example.path}:`, error);
35-
return { ...example, luaCode: "Loading example error." };
37+
console.error(`Error reading example file ${example.path} at ${path.resolve(funcPath, example.path)}:`, error);
38+
return { ...example, luaCode: `Error loading example: ${example.path}` };
3639
}
3740
});
3841
}
3942
43+
let notesContent: string[] = [];
44+
if (Array.isArray(funcNotes) && funcNotes.length > 0) {
45+
notesContent = funcNotes;
46+
}
47+
4048
---
4149

4250
<div class={"show-type-badge-" + funcType}>
@@ -45,18 +53,36 @@ if ( funcExamples.length > 0 ){
4553
title: func.id,
4654
tableOfContents: false,
4755
}}>
56+
<!-- Pair Function Ref -->
4857
{funcPair && (
4958
<p><strong>Pair:</strong> <a href={ `/${funcPair}` }>{ funcPair }</a></p>
5059
)}
5160

5261
<!-- Description -->
53-
<Fragment set:html={marked(funcInfo.description)} />
62+
{description && <Fragment set:html={marked(description)} />}
5463

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)} />
64+
<!-- Notes -->
65+
{notesContent.length > 0 && (
66+
<div class="notes-section">
67+
{notesContent.map((note) => (
68+
<div class="custom-note-box">
69+
<Fragment set:html={marked(note)} />
70+
</div>
71+
))}
5972
</div>
60-
))}
73+
)}
74+
75+
<!-- Examples -->
76+
{processedExamples.length > 0 && (
77+
<div class="examples-section">
78+
<h3>Exemplos</h3>
79+
{processedExamples.map((example: any) => (
80+
<div class="function-example">
81+
<Fragment set:html={marked(example.description)} />
82+
<Code code={example.luaCode} lang="lua" title={path.basename(example.path)} />
83+
</div>
84+
))}
85+
</div>
86+
)}
6187
</StarlightPage>
6288
</div>

web/src/styles/function-page.css

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
.function-syntax,
2+
.function-oop,
3+
.notes-section,
4+
.examples-section {
5+
margin-top: 1.5rem;
6+
margin-bottom: 1.5rem;
7+
}
8+
9+
.function-syntax h3,
10+
.function-oop h3,
11+
.examples-section h3 {
12+
margin-bottom: 0.75rem;
13+
font-size: 1.25em;
14+
border-bottom: 1px solid var(--sl-color-gray-5);
15+
padding-bottom: 0.25rem;
16+
}
17+
18+
.custom-note-box {
19+
background-color: var(--sl-color-blue-low);
20+
border-left: 4px solid var(--sl-color-blue);
21+
border-radius: 8px;
22+
padding: 1rem 1.25rem;
23+
margin-bottom: 1rem;
24+
color: var(--sl-color-text);
25+
}
26+
27+
html[data-theme="dark"] .custom-note-box {
28+
background-color: var(--sl-color-gray-5);
29+
}
30+
31+
.function-example {
32+
margin-bottom: 1.5rem;
33+
}
34+
.function-example > :first-child {
35+
margin-bottom: 0.5rem;
36+
}
37+
.examples-section .function-example:last-child {
38+
margin-bottom: 0;
39+
}

web/src/utils/functions.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ type FunctionItem = Awaited<ReturnType<typeof getCollection>>[number];
88
// Define a structure for function parameters
99
type FunctionParameter = {
1010
name: string;
11-
type: string; // Adjust type as needed (e.g., string | string[])
11+
type: string;
1212
description?: string;
1313
optional?: boolean;
1414
};
@@ -19,6 +19,7 @@ type FunctionDetails = {
1919
pair?: boolean;
2020
examples?: { code: string; description?: string }[];
2121
notes?: string[];
22+
important_notes?: string[];
2223
parameters?: FunctionParameter[];
2324
};
2425

@@ -36,7 +37,6 @@ export type FunctionData = {
3637
server?: any;
3738
};
3839

39-
// Use the specific FunctionDetails type
4040
export type TypedFunctionData = {
4141
shared?: FunctionDetails;
4242
client?: FunctionDetails;
@@ -47,42 +47,42 @@ export const functionTypePrettyName = {
4747
'client': 'Client-side',
4848
'server': 'Server-side',
4949
'shared': 'Shared',
50-
} as const; // Use 'as const' for stricter typing of keys
50+
} as const;
5151

5252
function getFunctionType(data: FunctionData): FunctionType {
5353
if (data.shared) return 'shared';
5454
if (data.client) return 'client';
5555
return 'server';
5656
}
5757
function getFunctionTypePretty(data: FunctionData): string {
58-
// No need for fallback, getFunctionType guarantees a valid FunctionType
5958
const funcType = getFunctionType(data);
6059
return functionTypePrettyName[funcType];
6160
}
6261

63-
// Define a return type for getFunctionInfo
6462
export type FunctionInfo = {
6563
description: string;
6664
type: FunctionType;
6765
typePretty: string;
6866
pair: boolean;
6967
examples: { code: string; description?: string }[];
70-
notes?: string[]; // Added notes
71-
parameters?: FunctionParameter[]; // Added parameters
68+
notes?: string[];
69+
important_notes?: string[];
70+
parameters?: FunctionParameter[];
7271
};
7372

7473
export function getFunctionInfo(data: TypedFunctionData): FunctionInfo {
7574
const type = getFunctionType(data);
76-
const details = data[type] ?? {}; // Get details based on type, default to empty object
75+
const details = data[type] ?? {};
7776

7877
return {
7978
description: details.description || '',
8079
type: type,
8180
typePretty: getFunctionTypePretty(data),
8281
pair: details.pair || false,
8382
examples: details.examples || [],
84-
notes: details.notes, // Extract notes (will be undefined if not present)
85-
parameters: details.parameters || [], // Extract parameters
83+
notes: details.notes || [],
84+
important_notes: details.important_notes || [],
85+
parameters: details.parameters || [],
8686
};
8787
}
8888

@@ -95,16 +95,14 @@ let functionsByTypeByCategory: FunctionsByTypeByCategory = {
9595
};
9696

9797
for (let func of functionsCollection) {
98-
// Assuming func.filePath exists, handle potential undefined if necessary
99-
const normalizedPath = path.normalize(func.id); // Use func.id which includes the path relative to content dir
98+
const normalizedPath = path.normalize(func.id);
10099
const folder = path.basename(path.dirname(normalizedPath));
101100
if (!functionsByCategory[folder]) {
102101
functionsByCategory[folder] = [];
103102
}
104103
functionsByCategory[folder].push(func);
105104

106105
const funcType = getFunctionType(func.data);
107-
// Ensure the folder exists for the specific type
108106
if (!functionsByTypeByCategory[funcType]?.[folder]) {
109107
functionsByTypeByCategory[funcType][folder] = [];
110108
}

0 commit comments

Comments
 (0)