Skip to content

Commit ecb6680

Browse files
authored
chore(core): expose routePathToMdPath utility in runtime (#2967)
1 parent 4a63cd1 commit ecb6680

File tree

4 files changed

+49
-9
lines changed

4 files changed

+49
-9
lines changed

packages/core/src/runtime/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,6 @@ export {
3737
normalizeImagePath,
3838
removeBase,
3939
removeTrailingSlash,
40+
routePathToMdPath,
4041
withBase,
4142
} from './utils';
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { describe, expect, it, rs } from '@rstest/core';
2+
import { routePathToMdPath } from './utils';
3+
4+
rs.mock('virtual-site-data', () => {
5+
return {
6+
default: {
7+
base: '/',
8+
},
9+
};
10+
});
11+
12+
describe('routePathToMdPath', () => {
13+
it('should match snapshot for various paths', () => {
14+
const cases = [
15+
'/foo/bar.html',
16+
'/foo/bar.html#hash',
17+
'/foo/bar.html?q=1',
18+
'/foo/bar.html?q=1#hash',
19+
'/foo.html',
20+
'/simple.html#my-heading',
21+
'/query.html?foo=bar&baz=qux',
22+
'/complex.html?id=123#section-2',
23+
];
24+
25+
const results = cases.map(path => routePathToMdPath(path));
26+
expect(results).toMatchInlineSnapshot(`
27+
[
28+
"/foo/bar.md",
29+
"/foo/bar.md#hash",
30+
"/foo/bar.md?q=1",
31+
"/foo/bar.md?q=1#hash",
32+
"/foo.md",
33+
"/simple.md#my-heading",
34+
"/query.md?foo=bar&baz=qux",
35+
"/complex.md?id=123#section-2",
36+
]
37+
`);
38+
});
39+
});

packages/core/src/runtime/utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ function normalizeImagePath(imagePath: string) {
5353
return withBase(imagePath);
5454
}
5555

56+
function routePathToMdPath(routePath: string): string {
57+
let url: string = routePath;
58+
url = normalizeHref(url, false);
59+
url = url.replace(/\.html(?=#|\?|$)/, '.md');
60+
return withBase(url);
61+
}
62+
5663
function isAbsoluteUrl(path: string) {
5764
return isExternalUrl(path) || isDataUrl(path) || path.startsWith('//');
5865
}
@@ -67,4 +74,5 @@ export {
6774
withBase,
6875
isEqualPath,
6976
normalizeHrefInRuntime,
77+
routePathToMdPath,
7078
};

packages/core/src/theme/components/Overview/index.tsx

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ import type {
66
} from '@rspress/core';
77
import {
88
isEqualPath,
9-
normalizeHref,
9+
routePathToMdPath,
1010
useI18n,
1111
usePageData,
1212
useSidebar,
13-
withBase,
1413
} from '@rspress/core/runtime';
1514
import {
1615
FallbackHeading,
@@ -27,13 +26,6 @@ import {
2726
import './index.scss';
2827
import { findItemByRoutePath } from './utils';
2928

30-
function routePathToMdPath(routePath: string): string {
31-
let url: string = routePath;
32-
url = normalizeHref(url, false);
33-
url = url.replace(/\.html$/, '.md');
34-
return withBase(url);
35-
}
36-
3729
function OverviewMarkdown({
3830
title,
3931
groups,

0 commit comments

Comments
 (0)