-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Markdown Parser Rework #1496
Open
saberzero1
wants to merge
14
commits into
jackyzha0:v4
Choose a base branch
from
saberzero1:markdown-parser-rework-rework
base: v4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,727
−1,054
Open
Markdown Parser Rework #1496
Changes from 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
56c53e6
Markdown Parser Rework
saberzero1 51ebec6
Cleanup
saberzero1 5e50bda
Merge branch 'v4' into markdown-parser-rework-rework
saberzero1 020c0e1
Verbose
saberzero1 375b21f
Merge branch 'markdown-parser-rework-rework' of https://github.com/sa…
saberzero1 ed545a3
Merge branch 'v4' of https://github.com/jackyzha0/quartz into markdow…
saberzero1 524c43a
Fix name logging
saberzero1 4216e24
Merge branch 'v4' of https://github.com/jackyzha0/quartz into markdow…
saberzero1 5866719
Merge branch 'v4' of https://github.com/jackyzha0/quartz into markdow…
saberzero1 cb8238d
Add parser presets.
saberzero1 78512db
Merge branch 'v4' of https://github.com/jackyzha0/quartz into markdow…
saberzero1 f5ac658
Merge branch 'v4' of https://github.com/jackyzha0/quartz into markdow…
saberzero1 f64e277
Comment regex (fixes https://github.com/jackyzha0/quartz/issues/1114)
saberzero1 1f9ee47
Comment regex (fixes https://github.com/jackyzha0/quartz/issues/1114)
saberzero1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 6 additions & 11 deletions
17
quartz/plugins/transformers/gfm.ts → ...gins/transformers/html/gfmLinkHeadings.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export { Citations } from "./citations" | ||
export { CrawlLinks } from "./links" | ||
export { Description } from "./description" | ||
export { GitHubFlavoredMarkdownLinkHeadings } from "./gfmLinkHeadings" | ||
export { Latex } from "./latex" | ||
export { SyntaxHighlighting } from "./syntax" | ||
export { ObsidianFlavoredMarkdownBlockReferences } from "./ofmBlockReferences" | ||
export { ObsidianFlavoredMarkdownCheckbox } from "./ofmCheckbox" | ||
export { ObsidianFlavoredMarkdownYouTubeEmbed } from "./ofmYoutubeEmbed" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import rehypeKatex from "rehype-katex" | ||
import rehypeMathjax from "rehype-mathjax/svg" | ||
import { HtmlTransformerPlugin } from "../../types" | ||
|
||
interface Options { | ||
renderEngine: "katex" | "mathjax" | ||
customMacros: MacroType | ||
} | ||
|
||
interface MacroType { | ||
[key: string]: string | ||
} | ||
|
||
export const Latex: HtmlTransformerPlugin<Partial<Options>> = (opts) => { | ||
const engine = opts?.renderEngine ?? "katex" | ||
const macros = opts?.customMacros ?? {} | ||
return { | ||
name: "Latex", | ||
transformation() { | ||
if (engine === "katex") { | ||
return [[rehypeKatex, { output: "html", macros }]] | ||
} else { | ||
return [[rehypeMathjax, { macros }]] | ||
} | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { HtmlTransformerPlugin } from "../../types" | ||
import { Element, Literal, Root as HtmlRoot } from "hast" | ||
import rehypeRaw from "rehype-raw" | ||
import { visit } from "unist-util-visit" | ||
import { PluggableList } from "unified" | ||
|
||
const blockReferenceRegex = new RegExp(/\^([-_A-Za-z0-9]+)$/g) | ||
|
||
export const ObsidianFlavoredMarkdownBlockReferences: HtmlTransformerPlugin = () => { | ||
return { | ||
name: "ObsidianFlavoredMarkdownBlockReferences", | ||
transformation() { | ||
const plugins: PluggableList = [rehypeRaw] | ||
|
||
plugins.push(() => { | ||
const inlineTagTypes = new Set(["p", "li"]) | ||
const blockTagTypes = new Set(["blockquote"]) | ||
return (tree: HtmlRoot, file) => { | ||
file.data.blocks = {} | ||
|
||
visit(tree, "element", (node, index, parent) => { | ||
if (blockTagTypes.has(node.tagName)) { | ||
const nextChild = parent?.children.at(index! + 2) as Element | ||
if (nextChild && nextChild.tagName === "p") { | ||
const text = nextChild.children.at(0) as Literal | ||
if (text && text.value && text.type === "text") { | ||
const matches = text.value.match(blockReferenceRegex) | ||
if (matches && matches.length >= 1) { | ||
parent!.children.splice(index! + 2, 1) | ||
const block = matches[0].slice(1) | ||
|
||
if (!Object.keys(file.data.blocks!).includes(block)) { | ||
node.properties = { | ||
...node.properties, | ||
id: block, | ||
} | ||
file.data.blocks![block] = node | ||
} | ||
} | ||
} | ||
} | ||
} else if (inlineTagTypes.has(node.tagName)) { | ||
const last = node.children.at(-1) as Literal | ||
if (last && last.value && typeof last.value === "string") { | ||
const matches = last.value.match(blockReferenceRegex) | ||
if (matches && matches.length >= 1) { | ||
last.value = last.value.slice(0, -matches[0].length) | ||
const block = matches[0].slice(1) | ||
|
||
if (last.value === "") { | ||
// this is an inline block ref but the actual block | ||
// is the previous element above it | ||
let idx = (index ?? 1) - 1 | ||
while (idx >= 0) { | ||
const element = parent?.children.at(idx) | ||
if (!element) break | ||
if (element.type !== "element") { | ||
idx -= 1 | ||
} else { | ||
if (!Object.keys(file.data.blocks!).includes(block)) { | ||
element.properties = { | ||
...element.properties, | ||
id: block, | ||
} | ||
file.data.blocks![block] = element | ||
} | ||
return | ||
} | ||
} | ||
} else { | ||
// normal paragraph transclude | ||
if (!Object.keys(file.data.blocks!).includes(block)) { | ||
node.properties = { | ||
...node.properties, | ||
id: block, | ||
} | ||
file.data.blocks![block] = node | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}) | ||
|
||
file.data.htmlAst = tree | ||
} | ||
}) | ||
|
||
return plugins | ||
}, | ||
} | ||
} | ||
|
||
declare module "vfile" { | ||
interface DataMap { | ||
blocks: Record<string, Element> | ||
htmlAst: HtmlRoot | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sitting on this for a bit, I do think that this is a bit too verbose.
ig this approach is bottom-up given that the transformers are
text => markdown => html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Verbose as in function naming? That can be resolved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i mean having users too much control and customization (naming is a byproduct ig).
if we gave them a "gun" they will eventually end up shooting themselves in their foot.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see what you mean.
Having said that, most users shouldn't be touching their parsers anyway. And despite the many questions in the Discord, I have yet to get one about parsers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have given it some thought. We could create 4 (text, markdown, HTML, external resources) "parsers" that just call the default parsing order.
That would make it easy for most users, while still allowing advanced users full customization.