Skip to content

Commit dd9353b

Browse files
authored
fix: cross-package links normalization (#1113)
1 parent fe633fe commit dd9353b

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

docs/astro.config.mjs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { versionedSidebarPlugin } from '@dfinity/starlight/versioned-sidebar';
99

1010
const BASE_DOCS_PATH = '/core';
1111
const docsVersion = process.env.DOCS_VERSION ?? 'local';
12+
const packagesDir = '../packages';
1213

1314
const UPGRADE_BANNER_CONTENT =
1415
'Still using <code>@dfinity/agent</code>? Migrate to <a href="/core/latest/upgrading/v4">@icp-sdk/core</a>!';
@@ -26,12 +27,17 @@ export default defineConfig({
2627
social: [{ icon: 'github', label: 'GitHub', href: 'https://github.com/dfinity/agent-js' }],
2728
plugins: [
2829
dfinityStarlightTheme(),
29-
markdownUrlsPlugin(),
30+
markdownUrlsPlugin({
31+
packagesDir,
32+
}),
3033
libsPlugin({
3134
clean: true,
32-
baseDir: '../packages',
35+
baseDir: packagesDir,
3336
typeDoc: {
34-
exclude: ['../packages/core', '../packages/migrate'],
37+
exclude: [
38+
`${packagesDir}/core`,
39+
`${packagesDir}/migrate`,
40+
],
3541
},
3642
frontmatter: {
3743
editUrl: false,

docs/plugins/src/markdown-urls.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@ import { visit } from "unist-util-visit";
33
import path from "node:path";
44
import { type StarlightPlugin } from "@astrojs/starlight/types";
55
import { DOCS_DIR } from "./utils/constants.ts";
6+
import { readdirSync } from "node:fs";
67

78
type RemarkPlugin = RemarkPlugins[number];
89

910
const markdownUrlsRemarkPlugin: RemarkPlugin = (
10-
[logger, docsDir, site, baseUrl]: [
11+
[logger, docsDir, site, baseUrl, crossPackageUrlRegex]: [
1112
AstroIntegrationLogger,
1213
string,
1314
string,
1415
string,
16+
RegExp,
1517
],
1618
) =>
1719
(tree, file) => {
@@ -38,25 +40,36 @@ const markdownUrlsRemarkPlugin: RemarkPlugin = (
3840
return;
3941
}
4042

43+
// if the url is a cross-package link, go back one level and add the api directory
44+
const normalizedUrl = url.replace(crossPackageUrlRegex, '../../$1/api');
45+
const absoluteLinkedFilePath = path.resolve(currentFileDir, normalizedUrl);
46+
4147
// normalize all other relative URLs to the docs directory
42-
const absoluteLinkedFilePath = path.resolve(currentFileDir, url);
4348
const relativeToDocs = path.relative(docsDir, absoluteLinkedFilePath);
44-
const normalizedUrl = `${baseUrl}${
49+
const nodeUrl = `${baseUrl}${
4550
relativeToDocs.replace(/(index)?\.mdx?(#.*)?$/, "$2").toLowerCase()
4651
}`;
47-
logger.debug(`Normalizing URL: ${url} -> ${normalizedUrl}`);
52+
logger.debug(`Normalizing URL: ${url} -> ${nodeUrl}`);
4853

49-
node.url = normalizedUrl;
54+
node.url = nodeUrl;
5055
});
5156
};
5257

53-
export function markdownUrlsPlugin(): StarlightPlugin {
58+
interface MarkdownUrlsPluginOptions {
59+
packagesDir: string;
60+
}
61+
62+
export function markdownUrlsPlugin({ packagesDir }: MarkdownUrlsPluginOptions): StarlightPlugin {
5463
return {
5564
name: "@dfinity/starlight/markdown-urls",
5665
hooks: {
5766
"config:setup": (ctx) => {
5867
const site = ctx.astroConfig.site;
5968

69+
// get all the packages in the packagesDir
70+
const packages = readdirSync(packagesDir);
71+
const crossPackageUrlRegex = new RegExp(`\\.\\.\\/(${packages.join('|')})`);
72+
6073
ctx.addIntegration({
6174
name: "libs-astro-plugin",
6275
hooks: {
@@ -70,6 +83,7 @@ export function markdownUrlsPlugin(): StarlightPlugin {
7083
DOCS_DIR,
7184
site,
7285
ctx.astroConfig.base,
86+
crossPackageUrlRegex,
7387
]],
7488
],
7589
},

0 commit comments

Comments
 (0)