Skip to content

Commit

Permalink
Fix sidebar highlighting when path contains non-ASCII characters (#1069)
Browse files Browse the repository at this point in the history
Co-authored-by: HiDeoo <[email protected]>
Co-authored-by: Chris Swithinbank <[email protected]>
  • Loading branch information
3 people authored Nov 17, 2023
1 parent c27495d commit b86f360
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/healthy-rabbits-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/starlight': patch
---

Fix sidebar highlighting and navigation buttons for pages with path containing non-ASCII characters
117 changes: 117 additions & 0 deletions packages/starlight/__tests__/sidebar/navigation-unicode.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { describe, expect, test, vi } from 'vitest';
import { getSidebar } from '../../utils/navigation';

vi.mock('astro:content', async () =>
(await import('../test-utils')).mockedAstroContent({
docs: [
['index.mdx', { title: 'Home Page' }],
['environmental-impact.md', { title: 'Eco-friendly docs' }],
['reference/configuration.mdx', { title: 'Config Reference' }],
['reference/frontmatter.md', { title: 'Frontmatter Reference' }],
// @ts-expect-error — Using a slug not present in Starlight docs site
['api/v1/用户.md', { title: 'Path with non-ASCII characters' }],
['guides/components.mdx', { title: 'Components' }],
],
})
);

describe('getSidebar', () => {
test('matches current page when path contains non-ascii characters', () => {
expect(getSidebar('/api/v1/%E7%94%A8%E6%88%B7', undefined)).toMatchInlineSnapshot(`
[
{
"attrs": {},
"badge": undefined,
"href": "/",
"isCurrent": false,
"label": "Home",
"type": "link",
},
{
"badge": undefined,
"collapsed": false,
"entries": [
{
"attrs": {},
"badge": {
"text": "New",
"variant": "success",
},
"href": "/intro/",
"isCurrent": false,
"label": "Introduction",
"type": "link",
},
{
"attrs": {},
"badge": {
"text": "Deprecated",
"variant": "default",
},
"href": "/next-steps/",
"isCurrent": false,
"label": "Next Steps",
"type": "link",
},
{
"attrs": {
"class": "showcase-link",
"target": "_blank",
},
"badge": undefined,
"href": "/showcase/",
"isCurrent": false,
"label": "Showcase",
"type": "link",
},
],
"label": "Start Here",
"type": "group",
},
{
"badge": {
"text": "Experimental",
"variant": "default",
},
"collapsed": false,
"entries": [
{
"attrs": {},
"badge": undefined,
"href": "/reference/configuration/",
"isCurrent": false,
"label": "Config Reference",
"type": "link",
},
{
"attrs": {},
"badge": undefined,
"href": "/reference/frontmatter/",
"isCurrent": false,
"label": "Frontmatter Reference",
"type": "link",
},
],
"label": "Reference",
"type": "group",
},
{
"badge": undefined,
"collapsed": false,
"entries": [
{
"attrs": {},
"badge": undefined,
"href": "/api/v1/用户/",
"isCurrent": true,
"label": "Path with non-ASCII characters",
"type": "link",
},
],
"label": "API v1",
"type": "group",
},
]
`);
});
});
2 changes: 1 addition & 1 deletion packages/starlight/utils/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function makeLink(
attrs?: LinkHTMLAttributes
): Link {
if (!isAbsolute(href)) href = pathWithBase(href);
const isCurrent = href === ensureTrailingSlash(currentPathname);
const isCurrent = encodeURI(href) === ensureTrailingSlash(currentPathname);
return { type: 'link', label, href, isCurrent, badge, attrs: attrs ?? {} };
}

Expand Down

0 comments on commit b86f360

Please sign in to comment.