-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from EricRovell/render-math
render-math
- Loading branch information
Showing
7 changed files
with
95 additions
and
46 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
svelte.config.js | ||
svelte.config.js | ||
mdsvex.config.js |
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,48 @@ | ||
/** | ||
* Setup for including Math in MDsvex files. | ||
* | ||
* Issue: https://github.com/pngwn/MDsveX/issues/302 | ||
* Example: https://github.com/pngwn/mdsvex-math | ||
*/ | ||
|
||
import math from "remark-math"; | ||
import rehype_katex from "rehype-katex"; | ||
import katex from "katex"; | ||
import visit from "unist-util-visit"; | ||
|
||
const correct_hast_tree = () => (tree) => { | ||
visit(tree, "text", (node) => { | ||
if (node.value.trim().startsWith('<')) { | ||
node.type = "raw"; | ||
} | ||
}); | ||
}; | ||
|
||
const katex_blocks = () => (tree) => { | ||
visit(tree, "code", (node) => { | ||
if (node.lang === "math") { | ||
const str = katex.renderToString(node.value, { | ||
displayMode: true, | ||
leqno: false, | ||
fleqn: false, | ||
throwOnError: true, | ||
errorColor: "#cc0000", | ||
strict: "warn", | ||
output: "htmlAndMathml", | ||
trust: false, | ||
macros: { "\\f": "#1f(#2)" } | ||
}); | ||
|
||
node.type = "raw"; | ||
node.value = '{@html `' + str + '`}'; | ||
} | ||
}); | ||
}; | ||
|
||
export const mdsvexConfig = { | ||
remarkPlugins: [ math, katex_blocks ], | ||
rehypePlugins: [ correct_hast_tree, rehype_katex ], | ||
layout: { | ||
blogpost: "./src/lib/layout/Blogpost.svelte" | ||
} | ||
}; |
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