-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugin-pages): implement logic for glob expansion
Related to #132
- Loading branch information
Showing
11 changed files
with
319 additions
and
40 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
36 changes: 36 additions & 0 deletions
36
packages/plugin-pages/src/converter/page-tree/expand-context.ts
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,36 @@ | ||
import { basename, dirname, relative } from 'path/posix'; | ||
|
||
import { LoDashStatic, cloneDeep, isArray, isPlainObject, isString, template } from 'lodash'; | ||
|
||
export interface IExpandContext { | ||
from: string; | ||
match: string; | ||
fullPath: string; | ||
prev: IExpandContext[]; | ||
} | ||
|
||
export interface IExpandContextImports{ | ||
_: LoDashStatic; | ||
path: Pick<typeof import( 'path' ), 'dirname' | 'basename' | 'relative'>; | ||
} | ||
const imports: Omit<IExpandContextImports, '_'> = { | ||
path: { | ||
dirname, | ||
basename, | ||
relative, | ||
}, | ||
}; | ||
|
||
export const expandNode = <T>( node: T, context: IExpandContext ): T => { | ||
const nodeClone = cloneDeep( node ) as any; | ||
Object.entries( nodeClone ).forEach( ( [ k, v ] ) => { | ||
if( isString( v ) ){ | ||
nodeClone[k] = template( v, { variable: 'context', imports } )( context ); | ||
} else if( isArray( v ) ){ | ||
nodeClone[k] = v.map( vv => expandNode( vv, context ) ); | ||
} else if( isPlainObject( v ) ){ | ||
nodeClone[k] = expandNode( v, context ); | ||
} | ||
} ); | ||
return nodeClone; | ||
}; |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './page-tree-builder'; | ||
export * from './utils'; | ||
export { IExpandContextImports as ExpandContextImports, IExpandContext } from './expand-context'; |
Oops, something went wrong.