Skip to content

Commit

Permalink
chore: split function
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed Apr 17, 2024
1 parent 62cec2e commit d94d9c5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
1 change: 1 addition & 0 deletions packages/shared/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from './isLinkExternal.js'
export * from './isLinkHttp.js'
export * from './isLinkWithProtocol.js'
export * from './isPlainObject.js'
export * from './inferRoutePath.js'
export * from './normalizeRoutePath.js'
export * from './omit.js'
export * from './removeEndingSlash.js'
Expand Down
23 changes: 23 additions & 0 deletions packages/shared/src/utils/inferRoutePath.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export const inferRoutePath = (path: string): string => {
// if the pathname is empty or ends with `/`, return as is
if (!path || path.endsWith('/')) return path

// convert README.md to index.html
let routePath = path.replace(/(^|\/)README.md$/i, '$1index.html')

// convert /foo/bar.md to /foo/bar.html
if (routePath.endsWith('.md')) {
routePath = routePath.substring(0, routePath.length - 3) + '.html'
}
// convert /foo/bar to /foo/bar.html
else if (!routePath.endsWith('.html')) {
routePath = routePath + '.html'
}

// convert /foo/index.html to /foo/
if (routePath.endsWith('/index.html')) {
routePath = routePath.substring(0, routePath.length - 10)
}

return routePath
}
26 changes: 2 additions & 24 deletions packages/shared/src/utils/normalizeRoutePath.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,6 @@
const FAKE_HOST = 'http://.'

export const inferRoutePath = (path: string): string => {
// if the pathname is empty or ends with `/`, return as is
if (!path || path.endsWith('/')) return path

// convert README.md to index.html
let routePath = path.replace(/(^|\/)README.md$/i, '$1index.html')

// convert /foo/bar.md to /foo/bar.html
if (routePath.endsWith('.md')) {
routePath = routePath.substring(0, routePath.length - 3) + '.html'
}
// convert /foo/bar to /foo/bar.html
else if (!routePath.endsWith('.html')) {
routePath = routePath + '.html'
}

// convert /foo/index.html to /foo/
if (routePath.endsWith('/index.html')) {
routePath = routePath.substring(0, routePath.length - 10)
}
import { inferRoutePath } from './inferRoutePath.js'

return routePath
}
const FAKE_HOST = 'http://.'

/**
* Normalize the given path to the final route path
Expand Down

0 comments on commit d94d9c5

Please sign in to comment.