obsidian flavored markdown support
This commit is contained in:
		
							parent
							
								
									3636c052eb
								
							
						
					
					
						commit
						c1c46ad67e
					
				
					 6 changed files with 348 additions and 2301 deletions
				
			
		| 
						 | 
				
			
			@ -1,6 +1,5 @@
 | 
			
		|||
import { PluggableList } from "unified"
 | 
			
		||||
import { QuartzTransformerPlugin } from "../types"
 | 
			
		||||
import { remarkWikiLink } from "@flowershow/remark-wiki-link"
 | 
			
		||||
import { relative, relativeToRoot, slugify } from "../../path"
 | 
			
		||||
import path from "path"
 | 
			
		||||
import { visit } from 'unist-util-visit'
 | 
			
		||||
| 
						 | 
				
			
			@ -18,7 +17,7 @@ const defaultOptions: Options = {
 | 
			
		|||
  prettyLinks: true
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export class LinkProcessing extends QuartzTransformerPlugin {
 | 
			
		||||
export class ResolveLinks extends QuartzTransformerPlugin {
 | 
			
		||||
  name = "LinkProcessing"
 | 
			
		||||
  opts: Options
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -28,9 +27,7 @@ export class LinkProcessing extends QuartzTransformerPlugin {
 | 
			
		|||
  }
 | 
			
		||||
 | 
			
		||||
  markdownPlugins(): PluggableList {
 | 
			
		||||
    return [[remarkWikiLink, {
 | 
			
		||||
      pathFormat: this.opts.markdownLinkResolution === "absolute" ? 'obsidian-absolute' : 'raw',
 | 
			
		||||
    }]]
 | 
			
		||||
    return []
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  htmlPlugins(): PluggableList {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										81
									
								
								quartz/plugins/transformers/ofm.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										81
									
								
								quartz/plugins/transformers/ofm.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,81 @@
 | 
			
		|||
import { PluggableList } from "unified"
 | 
			
		||||
import { QuartzTransformerPlugin } from "../types"
 | 
			
		||||
import { Root } from 'mdast'
 | 
			
		||||
import { findAndReplace } from "mdast-util-find-and-replace"
 | 
			
		||||
import { slugify } from "../../path"
 | 
			
		||||
import rehypeRaw from "rehype-raw"
 | 
			
		||||
 | 
			
		||||
export interface Options {
 | 
			
		||||
  highlight: boolean
 | 
			
		||||
  wikilinks: boolean
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const defaultOptions: Options = {
 | 
			
		||||
  highlight: true,
 | 
			
		||||
  wikilinks: true
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export class ObsidianFlavoredMarkdown extends QuartzTransformerPlugin {
 | 
			
		||||
  name = "ObsidianFlavoredMarkdown"
 | 
			
		||||
  opts: Options
 | 
			
		||||
 | 
			
		||||
  constructor(opts?: Options) {
 | 
			
		||||
    super()
 | 
			
		||||
    this.opts = { ...defaultOptions, ...opts }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  markdownPlugins(): PluggableList {
 | 
			
		||||
    const plugins: PluggableList = []
 | 
			
		||||
 | 
			
		||||
    if (this.opts.wikilinks) {
 | 
			
		||||
      plugins.push(() => {
 | 
			
		||||
        // Match wikilinks 
 | 
			
		||||
        // !?               -> optional embedding
 | 
			
		||||
        // \[\[             -> open brace
 | 
			
		||||
        // ([^\[\]\|\#]+)   -> 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")
 | 
			
		||||
        return (tree: Root, _file) => {
 | 
			
		||||
          findAndReplace(tree, backlinkRegex, (value: string, ...capture: string[]) => {
 | 
			
		||||
            const [path, rawHeader, rawAlias] = capture
 | 
			
		||||
            const header = rawHeader?.slice(1).trim() ?? ""
 | 
			
		||||
            const alias = rawAlias?.slice(1).trim() ?? value 
 | 
			
		||||
            const url = slugify(path.trim() + header)
 | 
			
		||||
            return {
 | 
			
		||||
              type: 'link',
 | 
			
		||||
              url,
 | 
			
		||||
              children: [{
 | 
			
		||||
                type: 'text',
 | 
			
		||||
                value: alias
 | 
			
		||||
              }]
 | 
			
		||||
            }
 | 
			
		||||
          })
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
      )
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (this.opts.highlight) {
 | 
			
		||||
      plugins.push(() => {
 | 
			
		||||
        // Match highlights 
 | 
			
		||||
        const highlightRegex = new RegExp(/==(.+)==/, "g")
 | 
			
		||||
        return (tree: Root, _file) => {
 | 
			
		||||
          findAndReplace(tree, highlightRegex, (_value: string, ...capture: string[]) => {
 | 
			
		||||
            const [inner] = capture
 | 
			
		||||
            return {
 | 
			
		||||
              type: 'html',
 | 
			
		||||
              value: `<span class="text-highlight">${inner}</span>`
 | 
			
		||||
            }
 | 
			
		||||
          })
 | 
			
		||||
        }
 | 
			
		||||
      })
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return plugins
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  htmlPlugins(): PluggableList {
 | 
			
		||||
    return [rehypeRaw]
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue