toc
Jacky Zhao
2023-06-10 b8c011410d6bcd6837f4efd6a3948196a0f7aeea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { PluggableList } from "unified"
import matter from "gray-matter"
import remarkFrontmatter from 'remark-frontmatter'
import { QuartzTransformerPlugin } from "../types"
 
export interface Options {
  language: 'yaml' | 'toml',
  delims: string | string[]
}
 
const defaultOptions: Options = {
  language: 'yaml',
  delims: '---'
}
 
export class FrontMatter extends QuartzTransformerPlugin {
  name = "FrontMatter"
  opts: Options
 
  constructor(opts?: Partial<Options>) {
    super()
    this.opts = { ...defaultOptions, ...opts }
  }
 
  markdownPlugins(): PluggableList {
    return [
      remarkFrontmatter,
      () => {
        return (_, file) => {
          const { data } = matter(file.value, this.opts)
 
          // fill in frontmatter
          file.data.frontmatter = {
            title: file.stem ?? "Untitled",
            tags: [],
            ...data
          }
        }
      }
    ]
  }
 
  htmlPlugins(): PluggableList {
    return []
  }
}
 
declare module 'vfile' {
  interface DataMap {
    frontmatter: { [key: string]: any } & {
      title: string
      tags: string[]
    }
  }
}