plugin integration round 2
This commit is contained in:
		
							parent
							
								
									a757521313
								
							
						
					
					
						commit
						ad6ce0d73f
					
				
					 29 changed files with 3863 additions and 100 deletions
				
			
		
							
								
								
									
										26
									
								
								quartz/plugins/emitters/contentPage.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								quartz/plugins/emitters/contentPage.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,26 @@
 | 
			
		|||
import { resolveToRoot } from "../../path"
 | 
			
		||||
import { EmitCallback, QuartzEmitterPlugin } from "../types"
 | 
			
		||||
import { ProcessedContent } from "../vfile"
 | 
			
		||||
 | 
			
		||||
export class ContentPage extends QuartzEmitterPlugin {
 | 
			
		||||
  name = "ContentPage"
 | 
			
		||||
  async emit(content: ProcessedContent[], emit: EmitCallback): Promise<string[]> {
 | 
			
		||||
    const fps: string[] = []
 | 
			
		||||
    for (const [tree, file] of content) {
 | 
			
		||||
      const pathToRoot = resolveToRoot(file.data.slug!)
 | 
			
		||||
 | 
			
		||||
      const fp = file.data.slug + ".html"
 | 
			
		||||
      await emit({
 | 
			
		||||
        title: file.data.frontmatter?.title ?? "Untitled",
 | 
			
		||||
        description: file.data.description ?? "",
 | 
			
		||||
        slug: file.data.slug!,
 | 
			
		||||
        ext: ".html",
 | 
			
		||||
      })
 | 
			
		||||
 | 
			
		||||
      // TODO: process aliases
 | 
			
		||||
 | 
			
		||||
      fps.push(fp)
 | 
			
		||||
    }
 | 
			
		||||
    return fps
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										1
									
								
								quartz/plugins/emitters/index.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								quartz/plugins/emitters/index.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1 @@
 | 
			
		|||
export { ContentPage } from './contentPage'
 | 
			
		||||
							
								
								
									
										10
									
								
								quartz/plugins/filters/draft.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								quartz/plugins/filters/draft.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,10 @@
 | 
			
		|||
import { QuartzFilterPlugin } from "../types"
 | 
			
		||||
import { ProcessedContent } from "../vfile"
 | 
			
		||||
 | 
			
		||||
export class RemoveDrafts extends QuartzFilterPlugin {
 | 
			
		||||
  name = "RemoveDrafts"
 | 
			
		||||
  shouldPublish([_tree, vfile]: ProcessedContent): boolean {
 | 
			
		||||
    const draftFlag: boolean = vfile.data?.frontmatter?.draft ?? false
 | 
			
		||||
    return !draftFlag
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										10
									
								
								quartz/plugins/filters/explicit.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								quartz/plugins/filters/explicit.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,10 @@
 | 
			
		|||
import { QuartzFilterPlugin } from "../types"
 | 
			
		||||
import { ProcessedContent } from "../vfile"
 | 
			
		||||
 | 
			
		||||
export class ExplicitPublish extends QuartzFilterPlugin {
 | 
			
		||||
  name = "ExplicitPublish"
 | 
			
		||||
  shouldPublish([_tree, vfile]: ProcessedContent): boolean {
 | 
			
		||||
    const publishFlag: boolean = vfile.data?.frontmatter?.publish ?? false
 | 
			
		||||
    return publishFlag
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										2
									
								
								quartz/plugins/filters/index.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								quartz/plugins/filters/index.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,2 @@
 | 
			
		|||
export { RemoveDrafts } from './draft'
 | 
			
		||||
export { ExplicitPublish } from './explicit'
 | 
			
		||||
							
								
								
									
										33
									
								
								quartz/plugins/index.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								quartz/plugins/index.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,33 @@
 | 
			
		|||
import { StaticResources } from '../resources'
 | 
			
		||||
import { PluginTypes } from './types'
 | 
			
		||||
 | 
			
		||||
export function getStaticResourcesFromPlugins(plugins: PluginTypes) {
 | 
			
		||||
  const staticResources: StaticResources = {
 | 
			
		||||
    css: [],
 | 
			
		||||
    js: [],
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  for (const plugin of plugins.transformers) {
 | 
			
		||||
    const res = plugin.externalResources
 | 
			
		||||
    if (res?.js) {
 | 
			
		||||
      staticResources.js = staticResources.js.concat(res.js)
 | 
			
		||||
    }
 | 
			
		||||
    if (res?.css) {
 | 
			
		||||
      staticResources.css = staticResources.css.concat(res.css)
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return staticResources
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export * from './transformers'
 | 
			
		||||
export * from './filters'
 | 
			
		||||
export * from './emitters'
 | 
			
		||||
 | 
			
		||||
declare module 'vfile' {
 | 
			
		||||
  // inserted in processors.ts
 | 
			
		||||
  interface DataMap {
 | 
			
		||||
    slug: string
 | 
			
		||||
    filePath: string
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										54
									
								
								quartz/plugins/transformers/description.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								quartz/plugins/transformers/description.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,54 @@
 | 
			
		|||
import { PluggableList } from "unified"
 | 
			
		||||
import { Root as HTMLRoot } from 'hast'
 | 
			
		||||
import { toString } from "hast-util-to-string"
 | 
			
		||||
import { QuartzTransformerPlugin } from "../types"
 | 
			
		||||
 | 
			
		||||
export interface Options {
 | 
			
		||||
  descriptionLength: number
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const defaultOptions: Options = {
 | 
			
		||||
  descriptionLength: 150
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export class Description extends QuartzTransformerPlugin {
 | 
			
		||||
  name = "Description"
 | 
			
		||||
  opts: Options
 | 
			
		||||
 | 
			
		||||
  constructor(opts?: Options) {
 | 
			
		||||
    super()
 | 
			
		||||
    this.opts = { ...defaultOptions, ...opts }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  markdownPlugins(): PluggableList {
 | 
			
		||||
    return []
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  htmlPlugins(): PluggableList {
 | 
			
		||||
    return [
 | 
			
		||||
      () => {
 | 
			
		||||
        return async (tree: HTMLRoot, file) => {
 | 
			
		||||
          const frontMatterDescription = file.data.frontmatter?.description
 | 
			
		||||
          const desc = frontMatterDescription ?? toString(tree)
 | 
			
		||||
          const sentences = desc.replace(/\s+/g, ' ').split('.')
 | 
			
		||||
          let finalDesc = ""
 | 
			
		||||
          let sentenceIdx = 0
 | 
			
		||||
          const len = this.opts.descriptionLength
 | 
			
		||||
          while (finalDesc.length < len) {
 | 
			
		||||
            finalDesc += sentences[sentenceIdx] + '.'
 | 
			
		||||
            sentenceIdx++
 | 
			
		||||
          }
 | 
			
		||||
 | 
			
		||||
          file.data.description = finalDesc
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
    ]
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
declare module 'vfile' {
 | 
			
		||||
  interface DataMap {
 | 
			
		||||
    description: string
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										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[]
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										30
									
								
								quartz/plugins/transformers/gfm.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								quartz/plugins/transformers/gfm.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,30 @@
 | 
			
		|||
import { PluggableList } from "unified"
 | 
			
		||||
import remarkGfm from "remark-gfm"
 | 
			
		||||
import smartypants from 'remark-smartypants'
 | 
			
		||||
import { QuartzTransformerPlugin } from "../types"
 | 
			
		||||
 | 
			
		||||
export interface Options {
 | 
			
		||||
  enableSmartyPants: boolean
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const defaultOptions: Options = {
 | 
			
		||||
  enableSmartyPants: true
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export class GitHubFlavoredMarkdown extends QuartzTransformerPlugin {
 | 
			
		||||
  name = "GitHubFlavoredMarkdown"
 | 
			
		||||
  opts: Options
 | 
			
		||||
 | 
			
		||||
  constructor(opts?: Options) {
 | 
			
		||||
    super()
 | 
			
		||||
    this.opts = { ...defaultOptions, ...opts }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  markdownPlugins(): PluggableList {
 | 
			
		||||
    return this.opts.enableSmartyPants ? [remarkGfm] : [remarkGfm, smartypants]
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  htmlPlugins(): PluggableList {
 | 
			
		||||
    return []
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										5
									
								
								quartz/plugins/transformers/index.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								quartz/plugins/transformers/index.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,5 @@
 | 
			
		|||
export { FrontMatter } from './frontmatter'
 | 
			
		||||
export { GitHubFlavoredMarkdown } from './gfm'
 | 
			
		||||
export { CreatedModifiedDate } from './lastmod'
 | 
			
		||||
export { Katex } from './latex'
 | 
			
		||||
export { Description } from './description'
 | 
			
		||||
							
								
								
									
										80
									
								
								quartz/plugins/transformers/lastmod.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										80
									
								
								quartz/plugins/transformers/lastmod.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,80 @@
 | 
			
		|||
import { PluggableList } from "unified"
 | 
			
		||||
import fs from "fs"
 | 
			
		||||
import path from 'path'
 | 
			
		||||
import { Repository } from "@napi-rs/simple-git"
 | 
			
		||||
import { QuartzTransformerPlugin } from "../types"
 | 
			
		||||
 | 
			
		||||
export interface Options {
 | 
			
		||||
  priority: ('frontmatter' | 'git' | 'filesystem')[],
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const defaultOptions: Options = {
 | 
			
		||||
  priority: ['frontmatter', 'git', 'filesystem']
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export class CreatedModifiedDate extends QuartzTransformerPlugin {
 | 
			
		||||
  name = "CreatedModifiedDate"
 | 
			
		||||
  opts: Options
 | 
			
		||||
 | 
			
		||||
  constructor(opts?: Options) {
 | 
			
		||||
    super()
 | 
			
		||||
    this.opts = {
 | 
			
		||||
      ...defaultOptions,
 | 
			
		||||
      ...opts,
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  markdownPlugins(): PluggableList {
 | 
			
		||||
    return [
 | 
			
		||||
      () => {
 | 
			
		||||
        let repo: Repository | undefined = undefined
 | 
			
		||||
        return async (_tree, file) => {
 | 
			
		||||
          let created: undefined | Date = undefined
 | 
			
		||||
          let modified: undefined | Date = undefined
 | 
			
		||||
          let published: undefined | Date = undefined
 | 
			
		||||
 | 
			
		||||
          const fp = path.join(file.cwd, file.data.filePath as string)
 | 
			
		||||
          for (const source of this.opts.priority) {
 | 
			
		||||
            if (source === "filesystem") {
 | 
			
		||||
              const st = await fs.promises.stat(fp)
 | 
			
		||||
              created ||= new Date(st.birthtimeMs)
 | 
			
		||||
              modified ||= new Date(st.mtimeMs)
 | 
			
		||||
            } else if (source === "frontmatter" && file.data.frontmatter) {
 | 
			
		||||
              created ||= file.data.frontmatter.date
 | 
			
		||||
              modified ||= file.data.frontmatter.lastmod
 | 
			
		||||
              modified ||= file.data.frontmatter["last-modified"]
 | 
			
		||||
              published ||= file.data.frontmatter.publishDate
 | 
			
		||||
            } else if (source === "git") {
 | 
			
		||||
              console.log(file)
 | 
			
		||||
              if (!repo) {
 | 
			
		||||
                repo = new Repository(file.cwd)
 | 
			
		||||
              }
 | 
			
		||||
 | 
			
		||||
              modified ||= new Date(await repo.getFileLatestModifiedDateAsync(fp))
 | 
			
		||||
            }
 | 
			
		||||
          }
 | 
			
		||||
 | 
			
		||||
          file.data.dates = {
 | 
			
		||||
            created: created ?? new Date(),
 | 
			
		||||
            modified: modified ?? new Date(),
 | 
			
		||||
            published: published ?? new Date()
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
    ]
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  htmlPlugins(): PluggableList {
 | 
			
		||||
    return []
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
declare module 'vfile' {
 | 
			
		||||
  interface DataMap {
 | 
			
		||||
    dates: {
 | 
			
		||||
      created: Date
 | 
			
		||||
      modified: Date
 | 
			
		||||
      published: Date
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										34
									
								
								quartz/plugins/transformers/latex.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								quartz/plugins/transformers/latex.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,34 @@
 | 
			
		|||
import { PluggableList } from "unified"
 | 
			
		||||
import remarkMath from "remark-math"
 | 
			
		||||
import rehypeKatex from 'rehype-katex'
 | 
			
		||||
import { StaticResources } from "../../resources"
 | 
			
		||||
import { QuartzTransformerPlugin } from "../types"
 | 
			
		||||
 | 
			
		||||
export class Katex extends QuartzTransformerPlugin {
 | 
			
		||||
  name = "Katex"
 | 
			
		||||
  markdownPlugins(): PluggableList {
 | 
			
		||||
    return [remarkMath]
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  htmlPlugins(): PluggableList {
 | 
			
		||||
    return [
 | 
			
		||||
      [rehypeKatex, {
 | 
			
		||||
        output: 'html',
 | 
			
		||||
      }]
 | 
			
		||||
    ]
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  externalResources: Partial<StaticResources> = {
 | 
			
		||||
    css: [
 | 
			
		||||
      // base css
 | 
			
		||||
      "https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/katex.min.css",
 | 
			
		||||
    ],
 | 
			
		||||
    js: [
 | 
			
		||||
      {
 | 
			
		||||
        // fix copy behaviour: https://github.com/KaTeX/KaTeX/blob/main/contrib/copy-tex/README.md
 | 
			
		||||
        src: "https://cdn.jsdelivr.net/npm/katex@0.16.7/dist/contrib/copy-tex.min.js",
 | 
			
		||||
        loadTime: "afterDOMReady"
 | 
			
		||||
      }
 | 
			
		||||
    ]
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										38
									
								
								quartz/plugins/types.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								quartz/plugins/types.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,38 @@
 | 
			
		|||
import { PluggableList } from "unified"
 | 
			
		||||
import { StaticResources } from "../resources"
 | 
			
		||||
import { ProcessedContent } from "./vfile"
 | 
			
		||||
 | 
			
		||||
export abstract class QuartzTransformerPlugin {
 | 
			
		||||
  abstract name: string
 | 
			
		||||
  abstract markdownPlugins(): PluggableList
 | 
			
		||||
  abstract htmlPlugins(): PluggableList
 | 
			
		||||
  externalResources?: Partial<StaticResources>
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export abstract class QuartzFilterPlugin {
 | 
			
		||||
  abstract name: string
 | 
			
		||||
  abstract shouldPublish(content: ProcessedContent): boolean
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export interface EmitOptions {
 | 
			
		||||
  // meta
 | 
			
		||||
  title: string
 | 
			
		||||
  description: string
 | 
			
		||||
  slug: string
 | 
			
		||||
  ext: `.${string}`
 | 
			
		||||
  
 | 
			
		||||
  // rendering related 
 | 
			
		||||
  content: string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export type EmitCallback = (data: EmitOptions) => Promise<void>
 | 
			
		||||
export abstract class QuartzEmitterPlugin {
 | 
			
		||||
  abstract name: string
 | 
			
		||||
  abstract emit(content: ProcessedContent[], emitCallback: EmitCallback): Promise<string[]>
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export interface PluginTypes {
 | 
			
		||||
  transformers: QuartzTransformerPlugin[],
 | 
			
		||||
  filters: QuartzFilterPlugin[],
 | 
			
		||||
  emitters: QuartzEmitterPlugin[],
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										5
									
								
								quartz/plugins/vfile.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										5
									
								
								quartz/plugins/vfile.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,5 @@
 | 
			
		|||
import { Node } from 'hast'
 | 
			
		||||
import { Data, VFile } from 'vfile/lib'
 | 
			
		||||
 | 
			
		||||
export type QuartzPluginData = Data
 | 
			
		||||
export type ProcessedContent = [Node<QuartzPluginData>, VFile]
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue