1 import remarkFrontmatter from "remark-frontmatter"
2 import remarkParse from "remark-parse"
3 import remarkStringify from "remark-stringify"
4 import { unified } from "unified"
5 import { read, readSync } from "to-vfile"
6 import { matter } from "vfile-matter"
7 import { FrontMatter } from "types"
8
9 export async function getFrontMatter(filePath: string): Promise<FrontMatter> {
10 return (
11 await unified()
12 .use(remarkParse)
13 .use(remarkStringify)
14 .use(remarkFrontmatter, ["yaml"])
15 .use(() => {
16 return (tree, file) => {
17 matter(file)
18 }
19 })
20 .process(await read(filePath))
21 ).data.matter as FrontMatter
22 }
23
24 export function getFrontMatterSync(filePath: string): FrontMatter {
25 const content = readSync(filePath)
26
27 matter(content)
28
29 return content.data.matter as FrontMatter
30 }
31
32 export async function getFrontMatterFromString(
33 fileContent: string
34 ): Promise<FrontMatter> {
35 return (
36 await unified()
37 .use(remarkParse)
38 .use(remarkStringify)
39 .use(remarkFrontmatter, ["yaml"])
40 .use(() => {
41 return (tree, file) => {
42 matter(file)
43 }
44 })
45 .process(fileContent)
46 ).data.matter as FrontMatter
47 }