From 85e4083db7f78844b023fb6fa3d9705f0d77f7ee Mon Sep 17 00:00:00 2001 From: Pedro Nauck Date: Thu, 6 Sep 2018 22:39:58 -0300 Subject: [PATCH] fix(docz-core): literal value of headings for menus --- packages/docz-core/src/Entries.ts | 3 ++- packages/docz-core/src/Entry.ts | 22 +++---------------- packages/docz-core/src/utils/ast.ts | 33 +++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 20 deletions(-) create mode 100644 packages/docz-core/src/utils/ast.ts diff --git a/packages/docz-core/src/Entries.ts b/packages/docz-core/src/Entries.ts index fead762db..0221688b7 100644 --- a/packages/docz-core/src/Entries.ts +++ b/packages/docz-core/src/Entries.ts @@ -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 = [ diff --git a/packages/docz-core/src/Entry.ts b/packages/docz-core/src/Entry.ts index e33c79bcd..bf2e7311f 100644 --- a/packages/docz-core/src/Entry.ts +++ b/packages/docz-core/src/Entry.ts @@ -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 => { - 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 @@ -51,7 +35,7 @@ const getHeadings = (ast: any): Heading[] => { headings.push({ depth, slug, - value: humanize(slug), + value: valueFromHeading(node), }) }) diff --git a/packages/docz-core/src/utils/ast.ts b/packages/docz-core/src/utils/ast.ts new file mode 100644 index 000000000..5f5b4003d --- /dev/null +++ b/packages/docz-core/src/utils/ast.ts @@ -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 => { + 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) +}