diff --git a/.changeset/smooth-donkeys-begin.md b/.changeset/smooth-donkeys-begin.md new file mode 100644 --- /dev/null +++ b/.changeset/smooth-donkeys-begin.md @@ -0,0 +1,5 @@ +--- +"@astrojs/starlight": patch +--- + +Fixes the Astro `trailingSlash` and `build.format` options not being applied correctly to sidebar links that include a query string or hash diff --git a/packages/starlight/__tests__/basics/format-path.test.ts b/packages/starlight/__tests__/basics/format-path.test.ts --- a/packages/starlight/__tests__/basics/format-path.test.ts +++ b/packages/starlight/__tests__/basics/format-path.test.ts @@ -4,61 +4,42 @@ type FormatPathOptions = Parameters[0]; const formatPath = (href: string, opts: FormatPathOptions) => createPathFormatter(opts)(href); +/** + * Tests for formatters using `format: file`. These should always produce the same expected output + * regardless of the value of the `trailingSlash` option. + */ +const fileFormatTests = [ + // index page + { path: '/', expected: '/index.html' }, + // index page with base + { path: '/', base: '/base/', expected: '/base/index.html' }, + // with trailing slash + { path: '/reference/configuration/', expected: '/reference/configuration.html' }, + // without trailing slash + { path: '/api/v1/users', expected: '/api/v1/users.html' }, + // with file extension + { path: '/guides/project-structure.html', expected: '/guides/project-structure.html' }, + // with file extension and trailing slash + { path: '/guides/project-structure.html/', expected: '/guides/project-structure.html' }, + // with hash + { path: '/reference/configuration/#tabs', expected: '/reference/configuration.html#tabs' }, + // index page with hash + { path: '/#_top', expected: '/index.html#_top' }, + // index page with hash and base + { path: '/#_top', base: '/base/', expected: '/base/index.html#_top' }, + // with query string + { path: '/search?q=astro', expected: '/search.html?q=astro' }, + // with file extension and hash + { path: '/guides/faq.html#tabs', expected: '/guides/faq.html#tabs' }, +]; + describe.each<{ options: FormatPathOptions; tests: Array<{ path: string; base?: string; expected: string }>; }>([ - { - options: { format: 'file', trailingSlash: 'ignore' }, - tests: [ - // index page - { path: '/', expected: '/index.html' }, - // index page with base - { path: '/', base: '/base/', expected: '/base/index.html' }, - // with trailing slash - { path: '/reference/configuration/', expected: '/reference/configuration.html' }, - // without trailing slash - { path: '/api/v1/users', expected: '/api/v1/users.html' }, - // with file extension - { path: '/guides/project-structure.html', expected: '/guides/project-structure.html' }, - // with file extension and trailing slash - { path: '/guides/project-structure.html/', expected: '/guides/project-structure.html' }, - ], - }, - { - options: { format: 'file', trailingSlash: 'always' }, - tests: [ - // index page - { path: '/', expected: '/index.html' }, - // index page with base - { path: '/', base: '/base/', expected: '/base/index.html' }, - // with trailing slash - { path: '/reference/configuration/', expected: '/reference/configuration.html' }, - // without trailing slash - { path: '/api/v1/users', expected: '/api/v1/users.html' }, - // with file extension - { path: '/guides/project-structure.html', expected: '/guides/project-structure.html' }, - // with file extension and trailing slash - { path: '/guides/project-structure.html/', expected: '/guides/project-structure.html' }, - ], - }, - { - options: { format: 'file', trailingSlash: 'never' }, - tests: [ - // index page - { path: '/', expected: '/index.html' }, - // index page with base - { path: '/', base: '/base/', expected: '/base/index.html' }, - // with trailing slash - { path: '/reference/configuration/', expected: '/reference/configuration.html' }, - // without trailing slash - { path: '/api/v1/users', expected: '/api/v1/users.html' }, - // with file extension - { path: '/guides/project-structure.html', expected: '/guides/project-structure.html' }, - // with file extension and trailing slash - { path: '/guides/project-structure.html/', expected: '/guides/project-structure.html' }, - ], - }, + { options: { format: 'file', trailingSlash: 'ignore' }, tests: fileFormatTests }, + { options: { format: 'file', trailingSlash: 'always' }, tests: fileFormatTests }, + { options: { format: 'file', trailingSlash: 'never' }, tests: fileFormatTests }, { options: { format: 'directory', trailingSlash: 'always' }, tests: [ @@ -74,6 +55,16 @@ { path: '/guides/project-structure.html', expected: '/guides/project-structure/' }, // with file extension and trailing slash { path: '/guides/project-structure.html/', expected: '/guides/project-structure/' }, + // with hash + { path: '/reference/configuration/#tabs', expected: '/reference/configuration/#tabs' }, + // index page with hash + { path: '/#_top', expected: '/#_top' }, + // index page with hash and base + { path: '/#_top', base: '/base/', expected: '/base/#_top' }, + // with query string + { path: '/search?q=astro', expected: '/search/?q=astro' }, + // with file extension and hash + { path: '/guides/faq.html#tabs', expected: '/guides/faq/#tabs' }, ], }, { @@ -91,6 +82,16 @@ { path: '/guides/project-structure.html', expected: '/guides/project-structure' }, // with file extension and trailing slash { path: '/guides/project-structure.html/', expected: '/guides/project-structure' }, + // with hash + { path: '/reference/configuration/#tabs', expected: '/reference/configuration#tabs' }, + // index page with hash + { path: '/#_top', expected: '/#_top' }, + // index page with hash and base + { path: '/#_top', base: '/base/', expected: '/base#_top' }, + // with query string + { path: '/search?q=astro', expected: '/search?q=astro' }, + // with file extension and hash + { path: '/guides/faq.html#tabs', expected: '/guides/faq#tabs' }, ], }, { @@ -108,6 +109,16 @@ { path: '/guides/project-structure.html', expected: '/guides/project-structure' }, // with file extension and trailing slash { path: '/guides/project-structure.html/', expected: '/guides/project-structure' }, + // with hash + { path: '/reference/configuration/#tabs', expected: '/reference/configuration/#tabs' }, + // index page with hash + { path: '/#_top', expected: '/#_top' }, + // index page with hash and base + { path: '/#_top', base: '/base/', expected: '/base/#_top' }, + // with query string + { path: '/search?q=astro', expected: '/search?q=astro' }, + // with file extension and hash + { path: '/guides/faq.html#tabs', expected: '/guides/faq#tabs' }, ], }, ])( diff --git a/packages/starlight/src/utils/createPathFormatter.ts b/packages/starlight/src/utils/createPathFormatter.ts --- a/packages/starlight/src/utils/createPathFormatter.ts +++ b/packages/starlight/src/utils/createPathFormatter.ts @@ -40,6 +40,14 @@ const formatStrategy = formatStrategies[format]; const trailingSlashStrategy = trailingSlashStrategies[trailingSlash]; + // Separate any query string or hash so we only format the pathname. + let queryOrHash = ''; + const queryOrHashIndex = href.search(/[?#]/); + if (queryOrHashIndex !== -1) { + queryOrHash = href.slice(queryOrHashIndex); + href = href.slice(0, queryOrHashIndex); + } + // Handle extension href = formatStrategy.handleExtension(href); @@ -47,12 +55,12 @@ href = formatStrategy.addBase(href); // Skip trailing slash handling for `build.format: 'file'` - if (format === 'file') return href; + if (format === 'file') return href + queryOrHash; // Handle trailing slash href = href === '/' ? href : trailingSlashStrategy(href); - return href; + return href + queryOrHash; } export function createPathFormatter(opts: FormatPathOptions) {