Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions schemas/function.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ $defs:
description: |
The default value for this parameter, if none was given in the call to the function.
This property automatically implicitly marks this parameter as optional.
optional:
type: boolean
default: false
description: If set to true, this parameter is optional.

returns:
type: object
Expand Down
2 changes: 1 addition & 1 deletion web/src/pages/[func].astro
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ if ( funcExamples.length > 0 ){
tableOfContents: false,
}}>
{funcPair && (
<p><strong>Pair:</strong> <a href={ funcPair }>{ funcPair }</a></p>
<p><strong>Pair:</strong> <a href={ `/${funcPair}` }>{ funcPair }</a></p>
)}

<!-- Description -->
Expand Down
66 changes: 54 additions & 12 deletions web/src/utils/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,84 @@ import type { FunctionType } from './types';

type FunctionItem = Awaited<ReturnType<typeof getCollection>>[number];

// Define a structure for function parameters
type FunctionParameter = {
name: string;
type: string; // Adjust type as needed (e.g., string | string[])
description?: string;
optional?: boolean;
};

// Define a structure for the details expected within shared/client/server
type FunctionDetails = {
description?: string;
pair?: boolean;
examples?: { code: string; description?: string }[];
notes?: string[];
parameters?: FunctionParameter[];
};

type FunctionsByCategory = {
[folder: string]: FunctionItem[];
};
type FunctionsByTypeByCategory = {
shared: FunctionsByCategory;
client: FunctionsByCategory;
server: FunctionsByCategory;
[key in FunctionType]: FunctionsByCategory;
};


export type FunctionData = {
shared?: any;
client?: any;
server?: any;
};

// Use the specific FunctionDetails type
export type TypedFunctionData = {
shared?: FunctionDetails;
client?: FunctionDetails;
server?: FunctionDetails;
};

export const functionTypePrettyName = {
'client': 'Client-side',
'server': 'Server-side',
'shared': 'Shared',
};
} as const; // Use 'as const' for stricter typing of keys

function getFunctionType(data: FunctionData): FunctionType {
if (data.shared) return 'shared';
if (data.client) return 'client';
return 'server';
}
function getFunctionTypePretty(data: FunctionData): string {
// No need for fallback, getFunctionType guarantees a valid FunctionType
const funcType = getFunctionType(data);
return functionTypePrettyName[funcType] ?? 'Server-side';
return functionTypePrettyName[funcType];
}

export function getFunctionInfo(data: FunctionData): any {
// Define a return type for getFunctionInfo
export type FunctionInfo = {
description: string;
type: FunctionType;
typePretty: string;
pair: boolean;
examples: { code: string; description?: string }[];
notes?: string[]; // Added notes
parameters?: FunctionParameter[]; // Added parameters
};

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

return {
description: data.shared?.description || data.client?.description || data.server?.description || '',
type: getFunctionType(data),
description: details.description || '',
type: type,
typePretty: getFunctionTypePretty(data),
pair: data.shared?.pair || data.client?.pair || data.server?.pair || false,
examples: data.shared?.examples || data.client?.examples || data.server?.examples || [ ],
pair: details.pair || false,
examples: details.examples || [],
notes: details.notes, // Extract notes (will be undefined if not present)
parameters: details.parameters || [], // Extract parameters
};
}

Expand All @@ -55,15 +95,17 @@ let functionsByTypeByCategory: FunctionsByTypeByCategory = {
};

for (let func of functionsCollection) {
const normalizedPath = path.normalize(func.filePath || '');
// Assuming func.filePath exists, handle potential undefined if necessary
const normalizedPath = path.normalize(func.id); // Use func.id which includes the path relative to content dir
const folder = path.basename(path.dirname(normalizedPath));
if (!functionsByCategory[folder]) {
functionsByCategory[folder] = [];
}
functionsByCategory[folder].push(func);

const funcType = getFunctionType(func.data);
if (!functionsByTypeByCategory[funcType][folder]) {
// Ensure the folder exists for the specific type
if (!functionsByTypeByCategory[funcType]?.[folder]) {
functionsByTypeByCategory[funcType][folder] = [];
}
functionsByTypeByCategory[funcType][folder].push(func);
Expand Down