Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(routing): fix Windows normalize bug #4

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"rehype-react": "^6.1.0",
"rss": "^1.2.2",
"sanitize-html": "^2.2.0",
"slash": "^3.0.0",
"unist-util-visit": "^2.0.3"
},
"devDependencies": {
Expand Down
6 changes: 4 additions & 2 deletions utils/routing.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { normalize } from 'path'
// Hot fix Windows normalize function returning backslash
import slash from 'slash'

// higher order function
const withPrefixPath = (prefixPath: string) => (path: string) => normalize(`/${prefixPath}/${path}/`)
const withPrefixPath = (prefixPath: string) => (path: string) => slash(normalize(`/${prefixPath}/${path}/`))

const trimSlash = (text: string) => text.replace(/^\//, '').replace(/\/$/, '')

Expand All @@ -22,7 +24,7 @@ interface ResolveUrlProps {

export const resolveUrl = ({ collectionPath = `/`, slug, url }: ResolveUrlProps) => {
const resolvePath = withPrefixPath(collectionPath)
if (!slug || slug.length === 0) return normalize(resolvePath(`/`))
if (!slug || slug.length === 0) return slash(normalize(resolvePath(`/`)))
if (!url || url.length === 0) return resolvePath(slug)
if (trimSlash(url) === slug) return resolvePath(slug)

Expand Down