Skip to content

Commit c43a3fa

Browse files
fix: Correct anchor IDs of functions and their parameters (#232)
Co-authored-by: Copilot <[email protected]>
1 parent fb3a736 commit c43a3fa

File tree

7 files changed

+79
-26
lines changed

7 files changed

+79
-26
lines changed

website/src/components/templates/FuncTemplate.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export const FuncTemplate: FC<FuncTemplateProps> = ({
5757
</h2>
5858

5959
<div class="mb-6">
60-
<FunctionDefinition func={content} prefix={content.name} />
60+
<FunctionDefinition func={content} />
6161
</div>
6262

6363
{content.example && (
@@ -67,7 +67,7 @@ export const FuncTemplate: FC<FuncTemplateProps> = ({
6767
)}
6868

6969
<div class="my-6">
70-
<FunctionParameters func={content} prefix={content.name} />
70+
<FunctionParameters func={content} />
7171
</div>
7272

7373
{content.scope.length > 0 && (

website/src/components/templates/GroupTemplate.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export const GroupTemplate: FC<GroupTemplateProps> = ({
4141

4242
{content.functions.map((method, index) => (
4343
<div key={method.name}>
44-
<h3 id={`definitions-${method.name}`} class="method-head">
44+
<h3 id={`functions-${method.name}`} class="method-head">
4545
<code class="text-base font-medium">{method.name}</code>
4646
<div class="flex flex-wrap items-center gap-2 text-sm">
4747
{method.element && <Tooltip kind="element" />}
@@ -50,7 +50,7 @@ export const GroupTemplate: FC<GroupTemplateProps> = ({
5050
</h3>
5151
<FunctionDisplay
5252
func={method}
53-
prefix={`definitions-${method.name}`}
53+
prefix={`functions-${method.name}`}
5454
isExampleFolding={false}
5555
/>
5656
</div>

website/src/components/ui/FunctionDefinition.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,21 @@ import type { FC } from "hono/jsx";
22
import type { Func } from "../../types/model";
33
import { TypeIcon } from "./TypeIcon";
44
import { genPath } from "./genPath";
5-
import { type2href } from "./type2href";
5+
import { buildParamId, type2href } from "./type2href";
66

77
type FunctionDefinitionProps = {
88
func: Func;
9-
prefix?: string;
9+
/**
10+
* The prefix for parameter IDs
11+
*
12+
* See `buildParamId`.
13+
*/
14+
prefix?: string | undefined;
1015
};
1116

1217
export const FunctionDefinition: FC<FunctionDefinitionProps> = ({
1318
func,
14-
prefix = "",
19+
prefix = undefined,
1520
}) => {
1621
return (
1722
<div class="bg-gray-50 p-4 rounded-md border border-gray-100 overflow-x-auto">
@@ -28,7 +33,7 @@ export const FunctionDefinition: FC<FunctionDefinitionProps> = ({
2833
{!param.positional && (
2934
<div class="flex-shrink-0">
3035
<a
31-
href={`#${prefix}-${func.name}-parameters-${param.name}`}
36+
href={`#${buildParamId(param.name, prefix)}`}
3237
class="text-gray-800 hover:text-blue-500 transition-colors mr-1"
3338
>
3439
<span class="font-medium">{param.name}</span>

website/src/components/ui/FunctionDisplay.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@ import { HtmlContent } from "./HtmlContent";
77

88
type FunctionDisplayProps = {
99
func: Func;
10-
prefix?: string;
10+
/**
11+
* The prefix for IDs of function parameters
12+
*
13+
* See `buildParamId`.
14+
*/
15+
prefix?: string | undefined;
1116
isExampleFolding?: boolean;
1217
};
1318

1419
export const FunctionDisplay: FC<FunctionDisplayProps> = ({
1520
func,
16-
prefix = "",
21+
prefix = undefined,
1722
isExampleFolding = true,
1823
}) => {
1924
return (

website/src/components/ui/FunctionParameters.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,21 @@ import { ChevronRightIcon } from "../icons";
44
import { HtmlContent } from "./HtmlContent";
55
import { Tooltip } from "./Tooltip";
66
import { TypeIcon } from "./TypeIcon";
7-
import { type2href } from "./type2href";
7+
import { buildParamId, type2href } from "./type2href";
88

99
type FunctionParametersProps = {
1010
func: Func;
11-
prefix?: string;
11+
/**
12+
* The prefix for IDs
13+
*
14+
* See `buildParamId`.
15+
*/
16+
prefix?: string | undefined;
1217
};
1318

1419
export const FunctionParameters: FC<FunctionParametersProps> = ({
1520
func,
16-
prefix = "",
21+
prefix = undefined,
1722
}) => {
1823
return (
1924
<div class="space-y-6">
@@ -23,7 +28,7 @@ export const FunctionParameters: FC<FunctionParametersProps> = ({
2328
class="bg-gray-50 rounded-md p-4 border border-gray-100"
2429
>
2530
<h4
26-
id={`${prefix}-${func.name}-parameters-${param.name}`}
31+
id={buildParamId(param.name, prefix)}
2732
class="flex flex-wrap items-center gap-2 mb-3"
2833
>
2934
<code class="text-base font-medium">{param.name}</code>

website/src/components/ui/common/TableOfContents.tsx

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,30 @@ export type TableOfContentsProps = {
44
outline: OutlineItem[];
55
};
66

7+
const PlainTableOfContents = ({
8+
outline,
9+
topLevel = false,
10+
}: TableOfContentsProps & { topLevel?: boolean }) => {
11+
return (
12+
// Indent for succeeding levels
13+
<ol class={`space-y-1 ${!topLevel && "pl-4"} text-sm text-neutral-700`}>
14+
{outline.map((item) => (
15+
<li key={item.id} data-assoc={item.id}>
16+
<a
17+
href={`#${item.id}`}
18+
class="block px-2 py-1 rounded hover:bg-neutral-100 transition-colors"
19+
>
20+
{item.name}
21+
</a>
22+
{item.children.length > 0 && (
23+
<PlainTableOfContents outline={item.children} />
24+
)}
25+
</li>
26+
))}
27+
</ol>
28+
);
29+
};
30+
731
export const TableOfContents = ({ outline }: TableOfContentsProps) => {
832
if (outline.length === 0) {
933
return null;
@@ -17,18 +41,7 @@ export const TableOfContents = ({ outline }: TableOfContentsProps) => {
1741
<strong class="block mb-2 text-sm text-neutral-500 font-semibold tracking-wide">
1842
目次
1943
</strong>
20-
<ol class="space-y-1 text-sm text-neutral-700">
21-
{outline.map((item) => (
22-
<li key={item.id} data-assoc={item.id}>
23-
<a
24-
href={`#${item.id}`}
25-
class="block px-2 py-1 rounded hover:bg-neutral-100 transition-colors"
26-
>
27-
{item.name}
28-
</a>
29-
</li>
30-
))}
31-
</ol>
44+
<PlainTableOfContents outline={outline} topLevel={true} />
3245
</nav>
3346
);
3447
};

website/src/components/ui/type2href.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,28 @@ export const type2href = (parameterType: string): string | null => {
5858
}
5959
return null;
6060
};
61+
62+
/**
63+
* Build the ID of a parameter with prefix
64+
*
65+
* If the parameter belongs to a top-level function on a page, leave `prefix` empty;
66+
* Otherwise, set it to an appropriate prefix.
67+
*
68+
* ## Example values of `prefix`
69+
*
70+
* | Page (kind) | Function | Parameter | `prefix` |
71+
* | ------------------- | ---------------- | ---------- | ----------------------- |
72+
* | `figure` (function) | `figure` | `body` | `undefined` |
73+
* | `figure` (function) | `figure.caption` | `body` | `"definitions-caption"` |
74+
* | `calc` (group) | `calc.abs` | `value` | `"functions-abs"` |
75+
* | `array` (type) | `array.at` | `index` | `"definitions-at"` |
76+
* | `array` (type) | Constructor | `value` | `"constructor"` |
77+
*/
78+
export function buildParamId(
79+
parameterName: string,
80+
prefix: string | undefined,
81+
): string {
82+
return prefix === undefined
83+
? `parameters-${parameterName}`
84+
: `${prefix}-${parameterName}`;
85+
}

0 commit comments

Comments
 (0)