Skip to content

Commit 5bb7239

Browse files
Fix(Nav): change nav link to use the frontmatter id (#76)
1 parent 4cf3354 commit 5bb7239

File tree

11 files changed

+302
-66
lines changed

11 files changed

+302
-66
lines changed

src/components/NavEntry.tsx

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { NavItem } from '@patternfly/react-core'
2-
import { kebabCase } from 'change-case'
2+
import { kebabCase } from '../utils/case'
33

44
export interface TextContentEntry {
55
id: string
@@ -17,20 +17,18 @@ interface NavEntryProps {
1717
}
1818

1919
export const NavEntry = ({ entry, isActive }: NavEntryProps) => {
20-
const { id } = entry
20+
const { id: contentLocationId } = entry
2121
const { id: entryTitle, section } = entry.data
2222

23-
const _id =
24-
section === 'components' || section === 'layouts'
25-
? kebabCase(entryTitle)
26-
: id
23+
const kebabTitle = kebabCase(entryTitle)
24+
const kebabSection = kebabCase(section)
2725

2826
return (
2927
<NavItem
30-
itemId={_id}
31-
to={`/${section}/${_id}`}
28+
itemId={contentLocationId}
29+
to={`/${kebabSection}/${kebabTitle}`}
3230
isActive={isActive}
33-
id={`nav-entry-${_id}`}
31+
id={`nav-entry-${contentLocationId}`}
3432
>
3533
{entryTitle}
3634
</NavItem>

src/components/NavSection.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { NavExpandable } from '@patternfly/react-core'
2-
import { sentenceCase } from 'change-case'
2+
import { sentenceCase, kebabCase } from '../utils/case'
33
import { NavEntry, type TextContentEntry } from './NavEntry'
4-
import { kebabCase } from 'change-case'
54

65
interface NavSectionProps {
76
entries: TextContentEntry[]

src/components/__tests__/NavEntry.test.tsx

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,42 @@ import { NavEntry, TextContentEntry } from '../NavEntry'
33

44
const mockEntry: TextContentEntry = {
55
id: 'entry1',
6-
data: { id: 'Entry1', section: 'section1' },
6+
data: { id: 'Entry 1 Title', section: 'section1' },
77
}
88

99
describe('NavEntry', () => {
1010
it('renders without crashing', () => {
1111
render(<NavEntry entry={mockEntry} isActive={false} />)
12-
expect(screen.getByText('Entry1')).toBeInTheDocument()
12+
expect(screen.getByText('Entry 1 Title')).toBeInTheDocument()
1313
})
1414

15-
it('renders the correct link', () => {
15+
it('renders the correct link with kebab-cased title', () => {
1616
render(<NavEntry entry={mockEntry} isActive={false} />)
17-
expect(screen.getByRole('link')).toHaveAttribute('href', '/section1/entry1')
17+
expect(screen.getByRole('link')).toHaveAttribute(
18+
'href',
19+
'/section1/entry-1-title',
20+
)
21+
})
22+
23+
it('properly transforms PatternFly text in URLs', () => {
24+
const patternflyEntry: TextContentEntry = {
25+
id: 'pf1',
26+
data: { id: 'PatternFly Components', section: 'PatternFly' },
27+
}
28+
render(<NavEntry entry={patternflyEntry} isActive={false} />)
29+
expect(screen.getByRole('link')).toHaveAttribute(
30+
'href',
31+
'/patternfly/patternfly-components',
32+
)
33+
})
34+
35+
it('preserves PatternFly text in display', () => {
36+
const patternflyEntry: TextContentEntry = {
37+
id: 'pf1',
38+
data: { id: 'PatternFly Components', section: 'PatternFly' },
39+
}
40+
render(<NavEntry entry={patternflyEntry} isActive={false} />)
41+
expect(screen.getByText('PatternFly Components')).toBeInTheDocument()
1842
})
1943

2044
it('marks the entry as active if isActive is true', () => {

0 commit comments

Comments
 (0)