docs + various polish
This commit is contained in:
		@@ -100,6 +100,7 @@ declare module 'vfile' {
 | 
			
		||||
  // inserted in processors.ts
 | 
			
		||||
  interface DataMap {
 | 
			
		||||
    slug: string
 | 
			
		||||
    allSlugs: string[]
 | 
			
		||||
    filePath: string
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -2,6 +2,7 @@ import matter from "gray-matter"
 | 
			
		||||
import remarkFrontmatter from 'remark-frontmatter'
 | 
			
		||||
import { QuartzTransformerPlugin } from "../types"
 | 
			
		||||
import yaml from 'js-yaml'
 | 
			
		||||
import { slug as slugAnchor } from 'github-slugger'
 | 
			
		||||
 | 
			
		||||
export interface Options {
 | 
			
		||||
  language: 'yaml' | 'toml',
 | 
			
		||||
@@ -29,10 +30,18 @@ export const FrontMatter: QuartzTransformerPlugin<Partial<Options> | undefined>
 | 
			
		||||
              }
 | 
			
		||||
            })
 | 
			
		||||
 | 
			
		||||
            // tag is an alias for tags
 | 
			
		||||
            if (data.tag) {
 | 
			
		||||
              data.tags = data.tag
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if (data.tags && !Array.isArray(data.tags)) {
 | 
			
		||||
              data.tags = data.tags.toString().split(",").map((tag: string) => tag.trim())
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // slug them all!!
 | 
			
		||||
            data.tags = data.tags?.map((tag: string) => slugAnchor(tag)) ?? []
 | 
			
		||||
 | 
			
		||||
            // fill in frontmatter
 | 
			
		||||
            file.data.frontmatter = {
 | 
			
		||||
              title: file.stem ?? "Untitled",
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,7 @@
 | 
			
		||||
export { FrontMatter } from './frontmatter'
 | 
			
		||||
export { GitHubFlavoredMarkdown } from './gfm'
 | 
			
		||||
export { CreatedModifiedDate } from './lastmod'
 | 
			
		||||
export { Katex } from './latex'
 | 
			
		||||
export { Latex } from './latex'
 | 
			
		||||
export { Description } from './description'
 | 
			
		||||
export { CrawlLinks } from './links'
 | 
			
		||||
export { ObsidianFlavoredMarkdown } from './ofm'
 | 
			
		||||
 
 | 
			
		||||
@@ -1,33 +1,43 @@
 | 
			
		||||
import remarkMath from "remark-math"
 | 
			
		||||
import rehypeKatex from 'rehype-katex'
 | 
			
		||||
import rehypeMathjax from 'rehype-mathjax/svg.js'
 | 
			
		||||
import { QuartzTransformerPlugin } from "../types"
 | 
			
		||||
 | 
			
		||||
export const Katex: QuartzTransformerPlugin = () => ({
 | 
			
		||||
  name: "Katex",
 | 
			
		||||
  markdownPlugins() {
 | 
			
		||||
    return [remarkMath]
 | 
			
		||||
  },
 | 
			
		||||
  htmlPlugins() {
 | 
			
		||||
    return [
 | 
			
		||||
      [rehypeKatex, {
 | 
			
		||||
        output: 'html',
 | 
			
		||||
      }]
 | 
			
		||||
    ]
 | 
			
		||||
  },
 | 
			
		||||
  externalResources() {
 | 
			
		||||
    return {
 | 
			
		||||
      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",
 | 
			
		||||
          contentType: 'external'
 | 
			
		||||
        }
 | 
			
		||||
interface Options {
 | 
			
		||||
  renderEngine: 'katex' | 'mathjax'
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export const Latex: QuartzTransformerPlugin<Options> = (opts?: Options) => {
 | 
			
		||||
  const engine = opts?.renderEngine ?? 'katex'
 | 
			
		||||
  return {
 | 
			
		||||
    name: "Latex",
 | 
			
		||||
    markdownPlugins() {
 | 
			
		||||
      return [remarkMath]
 | 
			
		||||
    },
 | 
			
		||||
    htmlPlugins() {
 | 
			
		||||
      return [
 | 
			
		||||
        engine === 'katex'
 | 
			
		||||
          ? [rehypeKatex, { output: 'html' }]
 | 
			
		||||
          : [rehypeMathjax]
 | 
			
		||||
      ]
 | 
			
		||||
    },
 | 
			
		||||
    externalResources() {
 | 
			
		||||
      return engine === 'katex'
 | 
			
		||||
        ? {
 | 
			
		||||
          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",
 | 
			
		||||
              contentType: 'external'
 | 
			
		||||
            }
 | 
			
		||||
          ]
 | 
			
		||||
        }
 | 
			
		||||
        : {}
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
})
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -29,13 +29,30 @@ export const CrawlLinks: QuartzTransformerPlugin<Partial<Options> | undefined> =
 | 
			
		||||
        return (tree, file) => {
 | 
			
		||||
          const curSlug = clientSideSlug(file.data.slug!)
 | 
			
		||||
          const transformLink = (target: string) => {
 | 
			
		||||
            const targetSlug = slugify(decodeURI(target).trim())
 | 
			
		||||
            const targetSlug = clientSideSlug(slugify(decodeURI(target).trim()))
 | 
			
		||||
            if (opts.markdownLinkResolution === 'relative' && !path.isAbsolute(targetSlug)) {
 | 
			
		||||
              return './' + relative(curSlug, targetSlug)
 | 
			
		||||
            } else {
 | 
			
		||||
              return './' + relativeToRoot(curSlug, targetSlug)
 | 
			
		||||
            } else if (opts.markdownLinkResolution === 'shortest') {
 | 
			
		||||
              // https://forum.obsidian.md/t/settings-new-link-format-what-is-shortest-path-when-possible/6748/5
 | 
			
		||||
              const allSlugs = file.data.allSlugs!
 | 
			
		||||
 | 
			
		||||
              // if the file name is unique, then it's just the filename
 | 
			
		||||
              const matchingFileNames = allSlugs.filter(slug => {
 | 
			
		||||
                const parts = clientSideSlug(slug).split(path.posix.sep)
 | 
			
		||||
                const fileName = parts.at(-1)
 | 
			
		||||
                return targetSlug === fileName
 | 
			
		||||
              })
 | 
			
		||||
 | 
			
		||||
              if (matchingFileNames.length === 1) {
 | 
			
		||||
                const targetSlug = clientSideSlug(matchingFileNames[0])
 | 
			
		||||
                return './' + relativeToRoot(curSlug, targetSlug)
 | 
			
		||||
              }
 | 
			
		||||
 | 
			
		||||
              // if it's not unique, then it's the absolute path from the vault root
 | 
			
		||||
              // (fall-through case)
 | 
			
		||||
            }
 | 
			
		||||
            // todo: handle shortest path
 | 
			
		||||
            // treat as absolute
 | 
			
		||||
            return './' + relativeToRoot(curSlug, targetSlug)
 | 
			
		||||
          }
 | 
			
		||||
 | 
			
		||||
          const outgoing: Set<string> = new Set()
 | 
			
		||||
@@ -53,7 +70,7 @@ export const CrawlLinks: QuartzTransformerPlugin<Partial<Options> | undefined> =
 | 
			
		||||
              if (!(isAbsoluteUrl(dest) || dest.startsWith("#"))) {
 | 
			
		||||
                node.properties.href = transformLink(dest)
 | 
			
		||||
              }
 | 
			
		||||
              
 | 
			
		||||
 | 
			
		||||
              dest = node.properties.href
 | 
			
		||||
              if (dest.startsWith(".")) {
 | 
			
		||||
                const normalizedPath = path.normalize(path.join(curSlug, dest))
 | 
			
		||||
 
 | 
			
		||||
@@ -12,6 +12,7 @@ import { JSResource } from "../../resources"
 | 
			
		||||
import calloutScript from "../../components/scripts/callout.inline.ts"
 | 
			
		||||
 | 
			
		||||
export interface Options {
 | 
			
		||||
  comments: boolean
 | 
			
		||||
  highlight: boolean
 | 
			
		||||
  wikilinks: boolean
 | 
			
		||||
  callouts: boolean
 | 
			
		||||
@@ -19,6 +20,7 @@ export interface Options {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const defaultOptions: Options = {
 | 
			
		||||
  comments: true,
 | 
			
		||||
  highlight: true,
 | 
			
		||||
  wikilinks: true,
 | 
			
		||||
  callouts: true,
 | 
			
		||||
@@ -101,11 +103,14 @@ const capitalize = (s: string): string => {
 | 
			
		||||
// ([^\[\]\|\#]+)   -> one or more non-special characters ([,],|, or #) (name)
 | 
			
		||||
// (#[^\[\]\|\#]+)? -> # then one or more non-special characters (heading link)
 | 
			
		||||
// (|[^\[\]\|\#]+)? -> | then one or more non-special characters (alias)
 | 
			
		||||
const backlinkRegex = new RegExp(/!?\[\[([^\[\]\|\#]+)(#[^\[\]\|\#]+)?(\|[^\[\]\|\#]+)?\]\]/, "g")
 | 
			
		||||
const wikilinkRegex = new RegExp(/!?\[\[([^\[\]\|\#]+)(#[^\[\]\|\#]+)?(\|[^\[\]\|\#]+)?\]\]/, "g")
 | 
			
		||||
 | 
			
		||||
// Match highlights 
 | 
			
		||||
const highlightRegex = new RegExp(/==(.+)==/, "g")
 | 
			
		||||
 | 
			
		||||
// Match comments 
 | 
			
		||||
const commentRegex = new RegExp(/%%(.+)%%/, "g")
 | 
			
		||||
 | 
			
		||||
// from https://github.com/escwxyz/remark-obsidian-callout/blob/main/src/index.ts
 | 
			
		||||
const calloutRegex = new RegExp(/^\[\!(\w+)\]([+-]?)/)
 | 
			
		||||
 | 
			
		||||
@@ -117,7 +122,7 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<Options>
 | 
			
		||||
      // pre-transform wikilinks (fix anchors to things that may contain illegal syntax e.g. codeblocks, latex)
 | 
			
		||||
      if (opts.wikilinks) {
 | 
			
		||||
        src = src.toString()
 | 
			
		||||
        return src.replaceAll(backlinkRegex, (value, ...capture) => {
 | 
			
		||||
        return src.replaceAll(wikilinkRegex, (value, ...capture) => {
 | 
			
		||||
          const [fp, rawHeader, rawAlias] = capture
 | 
			
		||||
          const anchor = rawHeader?.trim().slice(1)
 | 
			
		||||
          const displayAnchor = anchor ? `#${slugAnchor(anchor)}` : ""
 | 
			
		||||
@@ -133,7 +138,7 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<Options>
 | 
			
		||||
      if (opts.wikilinks) {
 | 
			
		||||
        plugins.push(() => {
 | 
			
		||||
          return (tree: Root, _file) => {
 | 
			
		||||
            findAndReplace(tree, backlinkRegex, (value: string, ...capture: string[]) => {
 | 
			
		||||
            findAndReplace(tree, wikilinkRegex, (value: string, ...capture: string[]) => {
 | 
			
		||||
              const [fp, rawHeader, rawAlias] = capture
 | 
			
		||||
              const anchor = rawHeader?.trim() ?? ""
 | 
			
		||||
              const alias = rawAlias?.slice(1).trim()
 | 
			
		||||
@@ -204,6 +209,19 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<Options>
 | 
			
		||||
          }
 | 
			
		||||
        })
 | 
			
		||||
      }
 | 
			
		||||
      
 | 
			
		||||
      if (opts.comments) {
 | 
			
		||||
        plugins.push(() => {
 | 
			
		||||
          return (tree: Root, _file) => {
 | 
			
		||||
            findAndReplace(tree, commentRegex, (_value: string, ..._capture: string[]) => {
 | 
			
		||||
              return {
 | 
			
		||||
                type: 'text',
 | 
			
		||||
                value: ''
 | 
			
		||||
              }
 | 
			
		||||
            })
 | 
			
		||||
          }
 | 
			
		||||
        })
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      if (opts.callouts) {
 | 
			
		||||
        plugins.push(() => {
 | 
			
		||||
 
 | 
			
		||||
@@ -6,19 +6,6 @@ export const SyntaxHighlighting: QuartzTransformerPlugin = () => ({
 | 
			
		||||
  htmlPlugins() {
 | 
			
		||||
    return [[rehypePrettyCode, {
 | 
			
		||||
      theme: 'css-variables',
 | 
			
		||||
      onVisitLine(node) {
 | 
			
		||||
        if (node.children.length === 0) {
 | 
			
		||||
          node.children = [{ type: 'text', value: ' ' }]
 | 
			
		||||
        }
 | 
			
		||||
      },
 | 
			
		||||
      onVisitHighlightedLine(node) {
 | 
			
		||||
        node.properties.className ??= []
 | 
			
		||||
        node.properties.className.push('highlighted')
 | 
			
		||||
      },
 | 
			
		||||
      onVisitHighlightedWord(node) {
 | 
			
		||||
        node.properties.className ??= []
 | 
			
		||||
        node.properties.className.push('word')
 | 
			
		||||
      },
 | 
			
		||||
    } satisfies Partial<CodeOptions>]]
 | 
			
		||||
  }
 | 
			
		||||
})
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user