Skip to content

Commit 36a557c

Browse files
committed
chore: rewrite DocLink's logic
1 parent 7930d4c commit 36a557c

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

src/components/DocLink.astro

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
---
2-
import { getLinkToKey } from "../lib/doc-index";
2+
import { isLinkMissing, normalizeLink } from "../lib/doc-link";
33
44
interface Props {
55
src: string;
66
section?: string;
77
}
88
99
const { src, section } = Astro.props;
10-
const rawLink = await getLinkToKey(src);
1110
12-
const missing = rawLink === undefined;
13-
const link = rawLink ? rawLink + (section ? "#" + section : "") : "";
11+
const missing = await isLinkMissing(src);
12+
const link = normalizeLink(src) + (section ? "#" + section : "");
1413
---
1514

1615
{

src/lib/doc-link.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { getEntry } from "astro:content";
2+
3+
function getIdFromLink(link: string): string {
4+
if (link.startsWith("/")) {
5+
link = link.slice(1);
6+
}
7+
if (link.endsWith("/")) {
8+
link = link.slice(0, -1);
9+
}
10+
return link;
11+
}
12+
13+
export function normalizeLink(link: string): string {
14+
if (!link.startsWith("/")) {
15+
link = "/" + link;
16+
}
17+
if (!link.endsWith("/")) {
18+
link = link + "/";
19+
}
20+
return link;
21+
}
22+
23+
export async function isLinkMissing(link: string): Promise<boolean> {
24+
const id = getIdFromLink(link);
25+
const entry = await getEntry("docs", id);
26+
return entry === undefined;
27+
}

0 commit comments

Comments
 (0)