-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(useBreadcrumbItems): avoid infinite recursion
Fixes #148
- Loading branch information
Showing
3 changed files
with
71 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { hasTrailingSlash, parseURL, stringifyParsedURL, withTrailingSlash } from 'ufo' | ||
|
||
export function pathBreadcrumbSegments(path: string, rootNode: string = '/'): string[] { | ||
const startNode = parseURL(path) | ||
const appendsTrailingSlash = hasTrailingSlash(startNode.pathname) | ||
|
||
const stepNode = (node: ReturnType<typeof parseURL>, nodes: string[] = []) => { | ||
const fullPath = stringifyParsedURL(node) | ||
// the pathname will always be without the trailing slash | ||
const currentPathName = node.pathname || '/' | ||
// when we hit the root the path will be an empty string; we swap it out for a slash | ||
nodes.push(fullPath || '/') | ||
if (currentPathName !== rootNode && currentPathName !== '/') { | ||
// strip the last path segment (/my/cool/path -> /my/cool) | ||
node.pathname = currentPathName.substring(0, currentPathName.lastIndexOf('/')) | ||
// if the input was provided with a trailing slash we need to honour that | ||
if (appendsTrailingSlash) | ||
node.pathname = withTrailingSlash(node.pathname.substring(0, node.pathname.lastIndexOf('/'))) | ||
stepNode(node, nodes) | ||
} | ||
return nodes | ||
} | ||
return stepNode(startNode) | ||
.reverse() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { pathBreadcrumbSegments } from '../../module/src/runtime/pure/breadcrumbs' | ||
|
||
describe('breadcrumbs', () => { | ||
it('basic', async () => { | ||
const items = pathBreadcrumbSegments('/x/y', '/') | ||
expect(items).toMatchInlineSnapshot(` | ||
[ | ||
"/", | ||
"/x", | ||
"/x/y", | ||
] | ||
`) | ||
}) | ||
it('empty root node', async () => { | ||
const items = pathBreadcrumbSegments('/x/y', '') | ||
expect(items).toMatchInlineSnapshot(` | ||
[ | ||
"/", | ||
"/x", | ||
"/x/y", | ||
] | ||
`) | ||
}) | ||
it('custom root node', async () => { | ||
const items = pathBreadcrumbSegments('/x/y', '/x') | ||
expect(items).toMatchInlineSnapshot(` | ||
[ | ||
"/x", | ||
"/x/y", | ||
] | ||
`) | ||
}) | ||
it('broken root node', async () => { | ||
const items = pathBreadcrumbSegments('/x/y', '/foo') | ||
expect(items).toMatchInlineSnapshot(` | ||
[ | ||
"/", | ||
"/x", | ||
"/x/y", | ||
] | ||
`) | ||
}) | ||
}) |