Skip to content

Commit

Permalink
fix(docz-core): add a fallback name on Entry
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Jun 30, 2018
1 parent 1750136 commit ec38139
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions packages/docz-core/src/Entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ export const parseMdx = (file: string): Promise<string> => {
return parser.run(parser.parse(raw))
}

const getParsedData = (ast: any) => {
interface ParsedData {
[key: string]: any
}

const getParsedData = (ast: any): ParsedData => {
const node = find(ast, (node: any) => is('yaml', node))
return get(node, `data.parsedValue`)
return get(node, `data.parsedValue`) || {}
}

export interface Heading {
Expand Down Expand Up @@ -90,18 +94,15 @@ export class Entry {
constructor(ast: any, file: string, src: string) {
const filepath = this.getFilepath(file, src)
const parsed = getParsedData(ast)

if (!parsed) {
throw new Error(`${filepath} does not contain a name`)
}
const name = this.getName(filepath, parsed)

this.id = ulid()
this.filepath = filepath
this.slug = this.slugify(filepath)
this.route = this.getRoute(parsed)
this.name = parsed.name
this.name = name
this.order = parsed.order || 0
this.menu = parsed.menu
this.menu = parsed.menu || null
this.headings = getHeadings(ast)
this.settings = parsed
}
Expand All @@ -117,6 +118,11 @@ export class Entry {
return relativePath
}

private getName(filepath: string, parsed: ParsedData): string {
const filename = humanize(path.parse(filepath).name)
return parsed && parsed.name ? parsed.name : filename
}

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

0 comments on commit ec38139

Please sign in to comment.