Skip to content

Commit ed5cc48

Browse files
iamchaniiokineadev
authored andcommitted
fix: use base path for relative path resolution in TOC generation (#74)
With base: '/docs/', TOC items failed to match sidebar entries. For example, sidebar data could not be retrieved at all. (See commit 47410cf) With this fix, TOC items are now correctly resolved against sidebar entries even when base is set. A test case has been added to cover this scenario. ::: changelog Previously, the plugin was hardcoding a `/` prefix without considering the `base` configuration option, which caused incorrect path matching when VitePress sites are deployed with a base path (e.g., `/docs/`). :::
1 parent 8652cd8 commit ed5cc48

3 files changed

Lines changed: 21 additions & 1 deletion

File tree

src/generator/toc.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ async function processSidebarSection(
118118
(item.base ?? section.base ?? base ?? '') + item.link,
119119
)
120120
const matchingFile = preparedFiles.find((file) => {
121-
const relativePath = `/${transformToPosixPath(stripExtPosix(file.path))}`
121+
const basePrefix = base.endsWith('/') ? base : `${base}/`
122+
const relativePath = `${basePrefix}${transformToPosixPath(stripExtPosix(file.path))}`
122123
return isPathMatch(relativePath, normalizedItemLink)
123124
})
124125

tests/generator/__snapshots__/toc.test.ts.snap

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ exports[`generateTOC resolves paths with common prefixes correctly 1`] = `
6363
"
6464
`;
6565

66+
exports[`generateTOC resolves paths with base options 1`] = `
67+
"### Blog Started
68+
69+
- [First version](/docs/blog/v1.md): Blazing fast frontend tool
70+
- [New features!](/docs/blog/v1.1.md): Instructions on how to get started with the tool
71+
72+
"
73+
`;
74+
6675
exports[`generateTOC with directoryFilter should include all files when directoryFilter is "." (root) 1`] = `
6776
"- [Some cool tool](/index.md): Blazing fast frontend tool
6877
- [Getting started](/test/getting-started.md): Instructions on how to get started with the tool

tests/generator/toc.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,16 @@ describe('generateTOC', () => {
167167

168168
expect(toc).toMatchSnapshot()
169169
})
170+
171+
it('resolves paths with base options', async () => {
172+
const toc = await generateTOC(preparedFilesWithCommonPrefixSample, {
173+
outDir,
174+
base: '/docs',
175+
sidebarConfig: sampleObjectVitePressSidebarWithCommonPrefix,
176+
})
177+
178+
expect(toc).toMatchSnapshot()
179+
})
170180
})
171181

172182
describe('generateTOCLink', () => {

0 commit comments

Comments
 (0)