Skip to content

Commit 8af7442

Browse files
committed
Using proper anchors for API linking, reverting resolving broken permalinks
This reverts commit 28c28f2.
1 parent 257ba83 commit 8af7442

6 files changed

Lines changed: 66 additions & 86 deletions

File tree

api/docusaurus-plugin/src/components/AnchorLink.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ import Link from '@docusaurus/Link';
22
import useBrokenLinks from '@docusaurus/useBrokenLinks';
33

44
export interface AnchorLinkProps {
5+
parent?: string;
56
id: string;
67
}
78

8-
export function AnchorLink({ id }: AnchorLinkProps) {
9-
useBrokenLinks().collectAnchor(id);
9+
export function AnchorLink({ parent, id }: AnchorLinkProps) {
10+
const anchor = parent ? parent + '.' + id : id;
11+
useBrokenLinks().collectAnchor(anchor);
1012

1113
return (
12-
<Link className="tsd-anchor" href={`#${id}`}>
13-
<span className="tsd-anchor-id" id={id} />
14+
<Link className="tsd-anchor" href={`#${anchor}`}>
15+
<span className="tsd-anchor-id" id={anchor} />
1416
<i className="codicon codicon-symbol-numeric" />
1517
</Link>
1618
);

api/docusaurus-plugin/src/components/Index.tsx

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,22 @@ import type { TSDDeclarationReflection } from '../types';
55
import { AnchorLink } from './AnchorLink';
66
import { Icon } from './Icon';
77
import { Name } from './Name';
8-
import { resolveAnchorLink } from '../utils/links';
98

109
export interface IndexChildProps {
1110
id: number;
1211
}
1312

1413
function IndexChild({ id }: IndexChildProps) {
15-
const reflection = useRequiredReflection(id);
14+
const reflection = useRequiredReflection(id);
1615

17-
return (
18-
<li>
19-
<Link
20-
className="tsd-kind-icon"
21-
to={reflection.permalink ? resolveAnchorLink(reflection.permalink) : resolveAnchorLink('', reflection.name)}
22-
>
23-
<Icon reflection={reflection} />
24-
<Name reflection={reflection} colorful={true} />
25-
</Link>
26-
</li>
27-
);
16+
return (
17+
<li>
18+
<Link className="tsd-kind-icon" to={reflection.permalink ?? `#${reflection.name}`}>
19+
<Icon reflection={reflection} />
20+
<Name reflection={reflection} colorful={true} />
21+
</Link>
22+
</li>
23+
);
2824
}
2925

3026
export interface IndexProps {

api/docusaurus-plugin/src/components/Member.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ import { Name } from './Name';
1818

1919
export interface MemberProps {
2020
id: number;
21+
parent?: string;
2122
}
2223

23-
export function Member({ id }: MemberProps) {
24+
export function Member({ id, parent }: MemberProps) {
2425
const reflection = useRequiredReflection(id);
2526
const apiOptions = useContext(ApiOptionsContext);
2627

@@ -46,10 +47,11 @@ export function Member({ id }: MemberProps) {
4647
}
4748

4849
const reflections = useReflectionMap();
50+
const parentAnchor = parent ? parent + '.' + reflection.name : reflection.name;
4951
return (
5052
<section className="tsd-panel tsd-member">
5153
<h3 className="tsd-panel-header">
52-
<AnchorLink id={reflection.name} />
54+
<AnchorLink parent={parent} id={reflection.name} />
5355
<SourceLink sources={reflection.sources} />
5456
<Flags flags={reflection.flags} />
5557
<Name reflection={reflection} />
@@ -61,7 +63,7 @@ export function Member({ id }: MemberProps) {
6163
{reflection.groups?.map((group) => (
6264
<Fragment key={group.title}>
6365
{group.children?.map((child) =>
64-
hasOwnDocument(child, reflections) ? null : <Member key={child} id={child} />,
66+
hasOwnDocument(child, reflections) ? null : <Member key={child} parent={parentAnchor} id={child} />,
6567
)}
6668
</Fragment>
6769
))}

api/docusaurus-plugin/src/plugin/data.ts

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import type {
1414
} from '../types';
1515
import { migrateToVersion0230 } from './structure/0.23';
1616
import { getKindSlug, getPackageSlug, joinUrl } from './url';
17-
import { resolveAnchorLink } from '../utils/links';
1817

1918
function shouldEmit(projectRoot: string, tsconfigPath: string) {
2019
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
@@ -177,43 +176,42 @@ export function loadPackageJsonAndDocs(
177176
}
178177

179178
export function addMetadataToReflections(
180-
project: JSONOutput.DeclarationReflection,
181-
packageSlug: string,
182-
urlPrefix: string,
179+
project: JSONOutput.DeclarationReflection,
180+
packageSlug: string,
181+
urlPrefix: string,
183182
): TSDDeclarationReflection {
184-
const permalink = `/${joinUrl(urlPrefix, packageSlug)}`;
185-
186-
if (project.children) {
187-
// eslint-disable-next-line no-param-reassign
188-
project.children = project.children.map((child) => {
189-
migrateToVersion0230(child);
190-
191-
const kindSlugPart = getKindSlug(child);
192-
const childPermalink = kindSlugPart
193-
? `${permalink}/${kindSlugPart}/${child.name}`
194-
: resolveAnchorLink(permalink, child.name);
195-
196-
// We need to go another level deeper and only use fragments
197-
if (child.kind === ReflectionKind.Namespace && child.children) {
198-
// eslint-disable-next-line no-param-reassign
199-
child.children = child.children.map((grandChild) => ({
200-
...grandChild,
201-
permalink: normalizeUrl([resolveAnchorLink(childPermalink, grandChild.name)]),
202-
}));
203-
}
204-
205-
return {
206-
...child,
207-
permalink: normalizeUrl([childPermalink]),
208-
};
209-
});
210-
}
211-
212-
// @ts-expect-error Not sure why this fails
213-
return {
214-
...project,
215-
permalink: normalizeUrl([permalink]),
216-
};
183+
const permalink = `/${joinUrl(urlPrefix, packageSlug)}`;
184+
185+
if (project.children) {
186+
// eslint-disable-next-line no-param-reassign
187+
project.children = project.children.map((child) => {
188+
migrateToVersion0230(child);
189+
190+
const kindSlugPart = getKindSlug(child);
191+
const childSlug = kindSlugPart ? `/${kindSlugPart}/${child.name}` : `#${child.name}`;
192+
const childPermalink = permalink + childSlug;
193+
194+
// We need to go another level deeper and only use fragments
195+
if (child.kind === ReflectionKind.Namespace && child.children) {
196+
// eslint-disable-next-line no-param-reassign
197+
child.children = child.children.map((grandChild) => ({
198+
...grandChild,
199+
permalink: normalizeUrl([`${childPermalink}#${grandChild.name}`]),
200+
}));
201+
}
202+
203+
return {
204+
...child,
205+
permalink: normalizeUrl([childPermalink]),
206+
};
207+
});
208+
}
209+
210+
// @ts-expect-error Not sure why this fails
211+
return {
212+
...project,
213+
permalink: normalizeUrl([permalink]),
214+
};
217215
}
218216

219217
function mergeReflections(base: TSDDeclarationReflection, next: TSDDeclarationReflection) {

api/docusaurus-plugin/src/utils/links.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,3 @@ export function removeScopes(text: string, scopes: string[]): string {
88
text,
99
);
1010
}
11-
12-
export function resolveAnchorLink(base: string, anchor?: string): string {
13-
let fullPath = base || '';
14-
if (anchor) {
15-
fullPath = fullPath ? `${fullPath}#${anchor}` : `#${anchor}`;
16-
}
17-
18-
const parts = fullPath.split('#');
19-
if (parts.length <= 2) {
20-
return fullPath;
21-
}
22-
23-
const urlPath = parts[0];
24-
const combinedAnchors = parts.slice(1).join('.');
25-
26-
return `${urlPath}#${combinedAnchors}`;
27-
}

api/docusaurus-plugin/src/utils/markdown.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import type { PropVersionMetadata } from '@docusaurus/plugin-content-docs';
22
import type { TSDDeclarationReflectionMap } from '../types';
3-
import { resolveAnchorLink } from './links';
43

54
function splitLinkText(text: string): { caption: string; target: string } {
65
let splitIndex = text.indexOf('|');
@@ -40,20 +39,20 @@ function findReflectionWithMatchingTarget(
4039
}
4140

4241
function replaceApiLinks(
43-
reflections: TSDDeclarationReflectionMap,
42+
reflections: TSDDeclarationReflectionMap,
4443
): (match: string, tagName: string, content: string) => string {
45-
return (match: string, tagName: string, content: string) => {
46-
const { caption, target } = splitLinkText(content);
47-
const [symbol, member] = target.split('.');
48-
const reflection = findReflectionWithMatchingTarget(reflections, symbol, member);
49-
const label = tagName === 'linkcode' ? `\`${caption}\`` : caption;
44+
return (match: string, tagName: string, content: string) => {
45+
const { caption, target } = splitLinkText(content);
46+
const [symbol, member] = target.split('.');
47+
const reflection = findReflectionWithMatchingTarget(reflections, symbol, member);
48+
const label = tagName === 'linkcode' ? `\`${caption}\`` : caption;
5049

51-
if (!reflection?.permalink) {
52-
return label;
53-
}
50+
if (!reflection?.permalink) {
51+
return label;
52+
}
5453

55-
return `[${label}](${resolveAnchorLink(reflection.permalink, member)})`;
56-
};
54+
return `[${label}](${reflection.permalink}${member ? `#${member}` : ''})`;
55+
};
5756
}
5857

5958
function replaceDocLinks(

0 commit comments

Comments
 (0)