Skip to content

Commit

Permalink
fix(docz-core): literal value of headings for menus
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Sep 7, 2018
1 parent 5fad743 commit 85e4083
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
3 changes: 2 additions & 1 deletion packages/docz-core/src/Entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import * as paths from './config/paths'
import { touch, compiled } from './utils/fs'
import { mapToObj } from './utils/helpers'

import { Entry, EntryObj, parseMdx } from './Entry'
import { Entry, EntryObj } from './Entry'
import { Plugin } from './Plugin'
import { Config } from './commands/args'
import { parseMdx } from './utils/ast'
import { getRepoEditUrl } from './utils/repo-info'

const DEFAULT_IGNORE = [
Expand Down
22 changes: 3 additions & 19 deletions packages/docz-core/src/Entry.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,14 @@
import * as path from 'path'
import * as crypto from 'crypto'
import vfile from 'to-vfile'
import unified from 'unified'
import remark from 'remark-parse'
import matter from 'remark-frontmatter'
import slug from 'remark-slug'
import parseFrontmatter from 'remark-parse-yaml'
import slugify from '@sindresorhus/slugify'
import humanize from 'humanize-string'
import find from 'unist-util-find'
import is from 'unist-util-is'
import visit from 'unist-util-visit'
import get from 'lodash.get'
import humanize from 'humanize-string'

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

export const parseMdx = (file: string): Promise<string> => {
const raw = vfile.readSync(file, 'utf-8')
const parser = unified()
.use(remark, { type: 'yaml', marker: '-' })
.use(matter)
.use(parseFrontmatter)
.use(slug)

return parser.run(parser.parse(raw))
}
import { valueFromHeading } from './utils/ast'

interface ParsedData {
[key: string]: any
Expand All @@ -51,7 +35,7 @@ const getHeadings = (ast: any): Heading[] => {
headings.push({
depth,
slug,
value: humanize(slug),
value: valueFromHeading(node),
})
})

Expand Down
33 changes: 33 additions & 0 deletions packages/docz-core/src/utils/ast.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import vfile from 'to-vfile'
import unified from 'unified'
import remark from 'remark-parse'
import matter from 'remark-frontmatter'
import slug from 'remark-slug'
import parseFrontmatter from 'remark-parse-yaml'
import humanize from 'humanize-string'
import get from 'lodash.get'

export const parseMdx = (file: string): Promise<string> => {
const raw = vfile.readSync(file, 'utf-8')
const parser = unified()
.use(remark, { type: 'yaml', marker: '-' })
.use(matter)
.use(parseFrontmatter)
.use(slug)

return parser.run(parser.parse(raw))
}

export const valueFromHeading = (node: any): string => {
const children = get(node, 'children')
const slug = get(node, 'data.id')

if (Array.isArray(children)) {
return children
.map((child: any) => child.value)
.filter(Boolean)
.join(' ')
}

return humanize(slug)
}

0 comments on commit 85e4083

Please sign in to comment.