-
Notifications
You must be signed in to change notification settings - Fork 3.4k
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
About mathematical formulas #1812
Comments
After I changed it, the math formula can be rendered, but input: import DOMPurify from 'dompurify'
import marked from 'marked'
import katex from 'katex'
import h from 'highlight.js'
import 'highlight.js/scss/github-gist.scss'
const renderer = new marked.Renderer()
function mathsExpression(expr) {
if (expr.match(/^\$\$[\s\S]*\$\$$/)) {
expr = expr.substr(2, expr.length - 4)
return katex.renderToString(expr, { displayMode: true })
} else if (expr.match(/^\$[\s\S]*\$$/)) {
expr = expr.substr(1, expr.length - 2)
return katex.renderToString(expr, { isplayMode: false })
}
}
const rendererCode = renderer.code
renderer.code = function (code, lang, escaped) {
if (!lang) {
const math = mathsExpression(code)
if (math) {
return math
}
}
return rendererCode(code, lang, escaped)
}
const rendererCodespan = renderer.codespan
renderer.codespan = function (text) {
const math = mathsExpression(text)
if (math) {
return math
}
return rendererCodespan(text)
}
marked.setOptions({
renderer: renderer,
highlight: function (code, language) {
const validLanguage = h.getLanguage(language) ? language : 'plaintext'
return h.highlight(validLanguage, code).value
},
pedantic: false,
gfm: true,
breaks: false,
sanitize: false,
smartLists: true,
smartypants: false,
xhtml: false,
headerIds: false,
katex: katex,
})
function _marked(text) {
let dirty = marked(text.toString())
let clean = DOMPurify.sanitize(dirty)
return clean
}
export default _marked |
try this: -const rendererCode = renderer.code
+const rendererCode = renderer.prototype.code |
or this: #1538 (comment) |
actually a better way to write a renderer is to use function mathsExpression(expr) {
if (expr.match(/^\$\$[\s\S]*\$\$$/)) {
expr = expr.substr(2, expr.length - 4)
return katex.renderToString(expr, { displayMode: true })
} else if (expr.match(/^\$[\s\S]*\$$/)) {
expr = expr.substr(1, expr.length - 2)
return katex.renderToString(expr, { isplayMode: false })
}
}
const renderer = {
code(code, lang, escaped) {
if (!lang) {
const math = mathsExpression(code)
if (math) {
return math
}
}
// use original code renderer by returning false
return false
},
codespan(text) {
const math = mathsExpression(text)
if (math) {
return math
}
// use original codespan renderer by returning false
return false
}
}
marked.use({renderer}) |
yea, I chose to use #1538 (comment) |
thx |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does marked support rendering mathematical formulas?
I have a need to render mathematical formulas, but I did not find your solution here. Is there a lack of functionality or is there another method? Please tell me, thanks
For example: $$ \frac{a-1}{b-1} \quad or \quad {a+1 \over b+1} $$
I read #1538, but I don’t quite understand how I should rewrite this, please advise, thank you!
The text was updated successfully, but these errors were encountered: