Jacky Zhao
2023-12-09 2c69b0c97d40f76363d20478cb799ce4b7518e56
quartz/plugins/transformers/frontmatter.ts
@@ -8,11 +8,13 @@
export interface Options {
  delims: string | string[]
  language: "yaml" | "toml"
  oneLineTagDelim: string
}
const defaultOptions: Options = {
  delims: "---",
  language: "yaml",
  oneLineTagDelim: ",",
}
export const FrontMatter: QuartzTransformerPlugin<Partial<Options> | undefined> = (userOpts) => {
@@ -20,8 +22,10 @@
  return {
    name: "FrontMatter",
    markdownPlugins() {
      const { oneLineTagDelim } = opts
      return [
        remarkFrontmatter,
        [remarkFrontmatter, ["yaml", "toml"]],
        () => {
          return (_, file) => {
            const { data } = matter(file.value, {
@@ -33,14 +37,19 @@
            })
            // tag is an alias for tags
            if (data.tag) {
              data.tags = data.tag
            if (data.tag !== null) {
              data.tags = data.tag.toString()
            }
            if (data.tags && !Array.isArray(data.tags)) {
            // coerce title to string
            if (data.title !== null) {
              data.title = data.title.toString()
            }
            if (data.tags !== null && !Array.isArray(data.tags)) {
              data.tags = data.tags
                .toString()
                .split(",")
                .split(oneLineTagDelim)
                .map((tag: string) => tag.trim())
            }