mirror of
https://github.com/alrayyes/wiki.git
synced 2025-05-01 14:38:14 +00:00
plugin integration round 2
This commit is contained in:
parent
a757521313
commit
ad6ce0d73f
29 changed files with 3863 additions and 100 deletions
quartz/plugins/transformers
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"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue