Skip to content

Commit

Permalink
fix: uppercase in path (#2170)
Browse files Browse the repository at this point in the history
Co-authored-by: nobkd <[email protected]>
  • Loading branch information
Jannchie and nobkd authored Jul 17, 2023
1 parent dcc2e8b commit 9bd31d6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
7 changes: 7 additions & 0 deletions docs/content/4.api/3.configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -410,3 +410,10 @@ Toggles the document-driven mode.
| `layoutFallbacks` | `string[]` | A list of `globals` key to use to find a layout fallback. |
| `injectPage` | `boolean` | Inject `[...slug].vue` pre-configured page |
| `trailingSlash` | `boolean` | Add a slash at the end of `canonical` and `og:url` |

## `respectPathCase`

- Type: `boolean`{lang=ts}
- Default: `false`{lang=ts}

Whether to respect the case of the file path when generating the route. Defaults to `false`, which means the route will be generated in lowercase. For example, `content/en-US/foo.md` will resolve to the `en-us/foo` path. This may not be what you expect.If set to `true`, the route will be generated with the same case as the file path. `content/en-US/foo.md` will resolve to the `en-US/foo` path.
9 changes: 9 additions & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,12 @@ export interface ModuleOptions {
injectPage?: boolean
trailingSlash?: boolean
},
/**
* Enable to keep uppercase characters in the generated routes.
*
* @default false
*/
respectPathCase: boolean
experimental: {
clientDB: boolean
stripQueryParameters: boolean
Expand Down Expand Up @@ -280,6 +286,7 @@ export default defineNuxtModule<ModuleOptions>({
fields: []
},
documentDriven: false,
respectPathCase: false,
experimental: {
clientDB: false,
stripQueryParameters: false
Expand Down Expand Up @@ -604,6 +611,7 @@ export default defineNuxtModule<ModuleOptions>({
stripQueryParameters: options.experimental.stripQueryParameters,
clientDB: options.experimental.clientDB && nuxt.options.ssr === false
},
respectPathCase: options.respectPathCase ?? false,
api: {
baseURL: options.api.baseURL
},
Expand Down Expand Up @@ -728,6 +736,7 @@ interface ModulePublicRuntimeConfig {
stripQueryParameters: boolean
clientDB: boolean
}
respectPathCase: boolean

defaultLocale: ModuleOptions['defaultLocale']

Expand Down
4 changes: 3 additions & 1 deletion src/runtime/server/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ interface ParseContentOptions {
pathMeta?: {
locales?: ModuleOptions['locales']
defaultLocale?: ModuleOptions['defaultLocale']
respectPathCase?: ModuleOptions['respectPathCase']
}
// Allow passing options for custom transformers
[key: string]: any
Expand Down Expand Up @@ -173,7 +174,8 @@ export const parseContent = async (id: string, content: StorageValue, opts: Pars
transformers: customTransformers,
pathMeta: {
defaultLocale: contentConfig.defaultLocale,
locales: contentConfig.locales
locales: contentConfig.locales,
respectPathCase: contentConfig.respectPathCase
}
}
)
Expand Down
10 changes: 4 additions & 6 deletions src/runtime/transformers/path-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ export default defineTransformer({
name: 'path-meta',
extensions: ['.*'],
transform (content, options: any = {}) {
const { locales = [], defaultLocale = 'en' } = options
const { locales = [], defaultLocale = 'en', respectPathCase = false } = options
const { _source, _file, _path, _extension } = describeId(content._id)
const parts = _path.split('/')

// Check first part for locale name
const _locale = locales.includes(parts[0]) ? parts.shift() : defaultLocale

const filePath = generatePath(parts.join('/'))
const filePath = generatePath(parts.join('/'), { respectPathCase })

return <ParsedContent> {
_path: filePath,
Expand Down Expand Up @@ -66,8 +64,8 @@ const isPartial = (path: string): boolean => path.split(/[:/]/).some(part => par
* @param path file full path
* @returns generated slug
*/
export const generatePath = (path: string, { forceLeadingSlash = true } = {}): string => {
path = path.split('/').map(part => slugify(refineUrlPart(part), { lower: true })).join('/')
export const generatePath = (path: string, { forceLeadingSlash = true, respectPathCase = false } = {}): string => {
path = path.split('/').map(part => slugify(refineUrlPart(part), { lower: !respectPathCase })).join('/')
return forceLeadingSlash ? withLeadingSlash(withoutTrailingSlash(path)) : path
}

Expand Down

0 comments on commit 9bd31d6

Please sign in to comment.