Added GitHub parsing

This commit is contained in:
Emile Bangma 2024-09-19 14:45:18 +00:00
parent dfd6abe6a6
commit 97613c2c09
5 changed files with 138 additions and 3 deletions

View File

@ -0,0 +1,2 @@
export { GitHubSmartypants } from "./smartypants"
export { GitHubLinkheadings } from "./linkheadings"

View File

@ -0,0 +1,87 @@
import { QuartzParser } from "../../types"
import { JSResource } from "../../../util/resources"
import { Pluggable } from "unified"
import rehypeSlug from "rehype-slug"
import rehypeAutolinkHeadings from "rehype-autolink-headings"
interface Options {
enabled: Boolean
}
const defaultOptions: Options = {
enabled: true,
}
export const GitHubLinkheadings: QuartzParser<Partial<Options>> = (userOpts) => {
const opts: Options = { ...defaultOptions, ...userOpts }
return {
name: "GitHubLinkheadings",
textTransform(_ctx, src: string | Buffer) {
if (src instanceof Buffer) {
src = src.toString()
}
return src
},
markdownPlugins(_ctx) {
const plug: Pluggable = () => {}
return plug
},
htmlPlugins() {
if (opts.enabled) {
return [
rehypeSlug,
[
rehypeAutolinkHeadings,
{
behavior: "append",
properties: {
role: "anchor",
ariaHidden: true,
tabIndex: -1,
"data-no-popover": true,
},
content: {
type: "element",
tagName: "svg",
properties: {
width: 18,
height: 18,
viewBox: "0 0 24 24",
fill: "none",
stroke: "currentColor",
"stroke-width": "2",
"stroke-linecap": "round",
"stroke-linejoin": "round",
},
children: [
{
type: "element",
tagName: "path",
properties: {
d: "M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71",
},
children: [],
},
{
type: "element",
tagName: "path",
properties: {
d: "M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71",
},
children: [],
},
],
},
},
],
] as Pluggable[]
} else {
return [] as Pluggable[]
}
},
externalResources() {
const js = {} as JSResource
return js
},
}
}

View File

@ -0,0 +1,40 @@
import { QuartzParser } from "../../types"
import { JSResource } from "../../../util/resources"
import { Pluggable } from "unified"
import remarkGfm from "remark-gfm"
import smartypants from "remark-smartypants"
interface Options {
enabled: Boolean
}
const defaultOptions: Options = {
enabled: true,
}
export const GitHubSmartypants: QuartzParser<Partial<Options>> = (userOpts) => {
const opts: Options = { ...defaultOptions, ...userOpts }
return {
name: "GitHubSmartypants",
textTransform(_ctx, src: string | Buffer) {
if (src instanceof Buffer) {
src = src.toString()
}
return src
},
markdownPlugins(_ctx) {
if (opts.enabled) {
return [remarkGfm, smartypants]
}
return [remarkGfm]
},
htmlPlugins() {
const plug: Pluggable = () => {}
return plug
},
externalResources() {
const js = {} as JSResource
return js
},
}
}

View File

@ -26,7 +26,7 @@ import { toHast } from "mdast-util-to-hast"
import { toHtml } from "hast-util-to-html"
import { PhrasingContent } from "mdast-util-find-and-replace/lib"
import { capitalize } from "../../util/lang"
import { PluggableList } from "unified"
import { Pluggable, PluggableList } from "unified"
import { Node } from "unist"
import { VFile } from "vfile"
import { BuildVisitor } from "unist-util-visit"
@ -35,6 +35,8 @@ import smartypants from "remark-smartypants"
import rehypeSlug from "rehype-slug"
import rehypeAutolinkHeadings from "rehype-autolink-headings"
import { GitHubLinkheadings, GitHubSmartypants } from "../parsers/github"
import {
ObsidianArrow,
ObsidianCallouts,
@ -213,11 +215,15 @@ export const GitHubFlavoredMarkdown: QuartzTransformerPlugin<Partial<GitHubOptio
markdownPlugins(ctx) {
const plugins: PluggableList = []
plugins.push(GitHubSmartypants({ enabled: opts.enableSmartyPants }).markdownPlugins(ctx))
return plugins
},
htmlPlugins() {
const plugins: PluggableList = []
plugins.push.apply(GitHubLinkheadings({ enabled: opts.linkHeadings }).htmlPlugins())
return plugins
},
externalResources() {
@ -269,7 +275,7 @@ export const ObsidianFlavoredMarkdown: QuartzTransformerPlugin<Partial<ObsidianO
htmlPlugins() {
const plugins: PluggableList = [rehypeRaw]
plugins.push(ObsidianCheckboxes({ enabled: opts.enableCheckbox }).htmlPlugins())
plugins.push.apply(ObsidianCheckboxes({ enabled: opts.enableCheckbox }).htmlPlugins())
return plugins
},

View File

@ -57,6 +57,6 @@ export type QuartzParserInstance = {
name: string
textTransform: (ctx: BuildCtx, src: string | Buffer) => string | Buffer
markdownPlugins: (ctx: BuildCtx) => Pluggable
htmlPlugins: () => Pluggable
htmlPlugins: () => Pluggable | Pluggable[]
externalResources: () => JSResource | string
}