Skip to content

Commit

Permalink
feat(docz-core): support for configuring the separator during slugifying
Browse files Browse the repository at this point in the history
  • Loading branch information
aleen42 authored and pedronauck committed Jan 21, 2019
1 parent e5565cf commit 10bb134
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion core/docz-core/src/lib/Entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export class Entries {
const createEntry = async (file: string) => {
try {
const ast = await parseMdx(file)
const entry = new Entry(ast, file, src)
const entry = new Entry(ast, file, src, config)

if (this.repoEditUrl) entry.setLink(this.repoEditUrl)
const { settings, ...rest } = entry
Expand Down
10 changes: 6 additions & 4 deletions core/docz-core/src/lib/Entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {

import * as paths from '../config/paths'

import { Config } from './commands/args'

const createId = (file: string) =>
crypto
.createHash('md5')
Expand Down Expand Up @@ -46,15 +48,15 @@ export class Entry {
[key: string]: any
}

constructor(ast: any, file: string, src: string) {
constructor(ast: any, file: string, src: string, config: Config) {
const filepath = this.getFilepath(file, src)
const parsed = getParsedData(ast)
const name = this.getName(filepath, parsed)

this.id = createId(file)
this.filepath = filepath
this.link = null
this.slug = this.slugify(filepath)
this.slug = this.slugify(filepath, config.separator)
this.route = this.getRoute(parsed)
this.name = name
this.order = parsed.order || 0
Expand Down Expand Up @@ -85,11 +87,11 @@ export class Entry {
return parsed && parsed.name ? parsed.name : filename
}

private slugify(filepath: string): string {
private slugify(filepath: string, separator: string): string {
const ext = path.extname(filepath)
const fileWithoutExt = filepath.replace(ext, '')

return slugify(fileWithoutExt)
return slugify(fileWithoutExt, { separator })
}

private getRoute(parsed: any): string {
Expand Down
2 changes: 2 additions & 0 deletions core/docz-core/src/states/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface Payload {
native: boolean
codeSandbox: boolean
themeConfig: ThemeConfig
separator: string
}

const getInitialConfig = (config: Config): Payload => {
Expand All @@ -34,6 +35,7 @@ const getInitialConfig = (config: Config): Payload => {
native: config.native,
codeSandbox: config.codeSandbox,
themeConfig: config.themeConfig,
separator: config.separator,
}
}

Expand Down
6 changes: 6 additions & 0 deletions packages/docz-core/src/commands/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ export interface Argv {
hashRouter: boolean
wrapper?: string
indexHtml?: string
/* slugify separator */
separator: string
}

export interface Config extends Argv {
Expand Down Expand Up @@ -202,4 +204,8 @@ export const args = (env: Env) => (yargs: any) => {
type: 'boolean',
default: getEnv('docz.sourcemaps') || true,
})
yargs.positional('separator', {
type: 'string',
default: getEnv('docz.separator', '-'),
})
}

0 comments on commit 10bb134

Please sign in to comment.