plugin integration round 2
This commit is contained in:
		
							
								
								
									
										55
									
								
								quartz/plugins/transformers/frontmatter.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								quartz/plugins/transformers/frontmatter.ts
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,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?: 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[]
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user