-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(docz-core): literal value of headings for menus
- Loading branch information
1 parent
5fad743
commit 85e4083
Showing
3 changed files
with
38 additions
and
20 deletions.
There are no files selected for viewing
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
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,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) | ||
} |