Skip to content

Commit 5c8a41e

Browse files
authored
refactor(utils): extract URL normalization to a shared utility (#178)
* refactor(utils): extract URL normalization to a shared utility * fix(urls): ensure the toPublicLink function return an absolute link
1 parent 4960d84 commit 5c8a41e

7 files changed

Lines changed: 21 additions & 26 deletions

File tree

components/NavBar.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import SearchBox from '@node-core/doc-kit/src/generators/web/ui/components/Searc
77
import { useTheme } from '@node-core/doc-kit/src/generators/web/ui/hooks/useTheme.mjs';
88
import { navbar } from '#theme/site';
99
import { baseURL } from '#theme/config';
10-
import { toPublicLink } from '../plugins/shared/urls.mjs';
10+
import { toPublicLink } from '../utils/helpers/urls.mjs';
1111
import Logo from '#theme/Logo';
1212

1313
const versionBase = new URL(baseURL).pathname;

components/SideBar.jsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import SideBar from '@node-core/ui-components/Containers/Sidebar';
33
import { major } from 'semver';
44
import { sidebar } from '#theme/local/site';
55
import { version } from '#theme/config';
6+
import { toPublicLink } from '../utils/helpers/urls.mjs';
67
import versions from '../versions.json' with { type: 'json' };
78

89
/** @param {string} url */
@@ -16,11 +17,6 @@ const isVersioned = Array.isArray(sidebar);
1617
// Versioned pages expose a version relative path, we prefix it so it matches the absolute sidebar links.
1718
const versionBase = isVersioned ? `/docs/api/v${version.major}.x` : '';
1819

19-
const pathnameFor = path => {
20-
const clean = path.replace(/\/index$/, '').replace(/^\//, '');
21-
return (clean ? `${versionBase}/${clean}` : versionBase) || '/';
22-
};
23-
2420
const groupsFor = path => {
2521
if (isVersioned) return sidebar;
2622

@@ -39,7 +35,7 @@ const currentVersion = isVersioned ? versionBase : undefined;
3935
/** Docs sidebar, plus a webpack version picker on the versioned API docs. */
4036
export default ({ metadata }) => (
4137
<SideBar
42-
pathname={pathnameFor(metadata.path)}
38+
pathname={toPublicLink(metadata.path, versionBase)}
4339
groups={groupsFor(metadata.path)}
4440
onSelect={redirect}
4541
as={PrefetchLink}

plugins/processor/router.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { categoryForReflection } from '../shared/categories.mjs';
55
import { getConstructorTitle, getMemberTitle } from '../shared/titles.mjs';
66
import { getSourceMetadata, isTypePage } from './metadata.mjs';
77
import { createTypePages, TYPE_PAGE_HEADING_KINDS } from './synthetic.mjs';
8-
import { normalizeLink } from '../shared/urls.mjs';
8+
import { normalizeLink } from '../../utils/helpers/urls.mjs';
99

1010
/**
1111
* The router owns the public Markdown shape. It keeps one class per file,

plugins/processor/site.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ReflectionKind } from 'typedoc';
22
import { CATEGORY_RULES } from '../shared/categories.mjs';
3-
import { toPublicLink } from '../shared/urls.mjs';
3+
import { toPublicLink } from '../../utils/helpers/urls.mjs';
44

55
const SIDEBAR_KINDS =
66
ReflectionKind.Project | ReflectionKind.Namespace | ReflectionKind.Class;

plugins/processor/typeMap.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ReflectionKind } from 'typedoc';
22
import { isTypePage } from './metadata.mjs';
3-
import { normalizeLink } from '../shared/urls.mjs';
3+
import { normalizeLink } from '../../utils/helpers/urls.mjs';
44

55
const TYPE_MAP_KINDS =
66
ReflectionKind.Class |

plugins/shared/urls.mjs

Lines changed: 0 additions & 16 deletions
This file was deleted.

utils/helpers/urls.mjs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export const normalizeLink = url => {
2+
if (!url) return '';
3+
4+
return url
5+
.replace(/\.(md|html)$/, '')
6+
.replace(/\/index$/, '')
7+
.replace(/^\//, '');
8+
};
9+
10+
export const toPublicLink = (url, publicPath = '') => {
11+
const path = normalizeLink(url);
12+
const prefix = publicPath.replace(/^\/+|\/+$/g, '');
13+
const combined = [prefix, path].filter(Boolean).join('/');
14+
return `/${combined}`;
15+
};

0 commit comments

Comments
 (0)