Skip to content

Commit

Permalink
feat: dotNesting option to disable dot special handling in filenames
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Apr 27, 2023
1 parent 951064c commit d803831
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/core/tree.ts
Expand Up @@ -34,7 +34,11 @@ export class TreeNode {
constructor(options: ResolvedOptions, filePath: string, parent?: TreeNode) {
this.options = options
this.parent = parent
this.value = createTreeNodeValue(filePath, parent?.value)
this.value = createTreeNodeValue(
filePath,
parent?.value,
options.pathParser
)
}

/**
Expand Down
29 changes: 24 additions & 5 deletions src/core/treeNodeValue.ts
Expand Up @@ -184,13 +184,17 @@ export type TreeNodeValue = TreeNodeValueStatic | TreeNodeValueParam

export function createTreeNodeValue(
segment: string,
parent?: TreeNodeValue
parent?: TreeNodeValue,
parseSegmentOptions?: ParseSegmentOptions
): TreeNodeValue {
if (!segment || segment === 'index') {
return new TreeNodeValueStatic(segment, parent, '')
}

const [pathSegment, params, subSegments] = parseSegment(segment)
const [pathSegment, params, subSegments] = parseSegment(
segment,
parseSegmentOptions
)

if (params.length) {
return new TreeNodeValueParam(
Expand All @@ -212,14 +216,28 @@ const enum ParseSegmentState {
modifier, // after the ]
}

/**
* Options passed to `parseSegment()`to control how a segment of a file path is parsed. e.g. in `/users/[id]`, `users`
* and `[id]` are segments.
*/
export interface ParseSegmentOptions {
/**
* Should we allow dot nesting in the param name. e.g. `users.[id]` will be parsed as `users/[id]` if this is `true`,
* nesting
* @default true
*/
dotNesting?: boolean
}

/**
* Parses a segment into the route path segment and the extracted params.
*
* @param segment - segment to parse without the extension
* @returns - the pathSegment and the params
*/
function parseSegment(
segment: string
segment: string,
{ dotNesting = true }: ParseSegmentOptions = {}
): [string, TreeRouteParam[], SubSegment[]] {
let buffer = ''
let state: ParseSegmentState = ParseSegmentState.static
Expand Down Expand Up @@ -262,8 +280,9 @@ function parseSegment(
// check if it's an optional param or not
state = ParseSegmentState.paramOptional
} else {
// allows for nested paths without nesting the views
buffer += c === '.' ? '/' : c
// append the char to the buffer or if the dotNesting option
// is enabled (by default it is), transform into a slash
buffer += dotNesting && c === '.' ? '/' : c
}
} else if (state === ParseSegmentState.paramOptional) {
if (c === '[') {
Expand Down
9 changes: 9 additions & 0 deletions src/options.ts
Expand Up @@ -3,6 +3,7 @@ import { Awaitable, getFileBasedRouteName, isArray, warn } from './core/utils'
import type { TreeNode } from './core/tree'
import { resolve } from 'pathe'
import { EditableTreeNode } from './core/extendRoutes'
import { ParseSegmentOptions } from './core/treeNodeValue'

export interface RoutesFolderOption {
/**
Expand Down Expand Up @@ -124,6 +125,11 @@ export interface ResolvedOptions {
* Activates debug logs.
*/
logs: boolean

/**
* @inheritDoc ParseSegmentOptions
*/
pathParser: ParseSegmentOptions
}

/**
Expand Down Expand Up @@ -152,6 +158,9 @@ export const DEFAULT_OPTIONS: ResolvedOptions = {
dts: isPackageExists('typescript'),
logs: false,
_inspect: false,
pathParser: {
dotNesting: true,
},
}

export interface ServerContext {
Expand Down

0 comments on commit d803831

Please sign in to comment.