-
-
Notifications
You must be signed in to change notification settings - Fork 135
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
Add mathjax support #165
Closed
Closed
Add mathjax support #165
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
import marpitPlugin from '@marp-team/marpit/plugin' | ||
|
||
import { mathjax } from 'mathjax-full/js/mathjax' | ||
import { TeX } from 'mathjax-full/js/input/tex' | ||
import { SVG } from 'mathjax-full/js/output/svg' | ||
import { liteAdaptor } from 'mathjax-full/js/adaptors/liteAdaptor' | ||
import { RegisterHTMLHandler } from 'mathjax-full/js/handlers/html' | ||
import { AllPackages } from 'mathjax-full/js/input/tex/AllPackages' | ||
|
||
const adaptor = liteAdaptor() | ||
RegisterHTMLHandler(adaptor) | ||
const tex = new TeX({ packages: AllPackages }) | ||
const svg = new SVG({ fontCache: 'none' }) | ||
const mathDocument = mathjax.document('', { InputJax: tex, OutputJax: svg }) | ||
|
||
interface MathOptionsInterface { | ||
katexOption?: object | ||
} | ||
|
||
export type MathOptions = boolean | MathOptionsInterface | ||
|
||
/** | ||
* marp-core math plugin | ||
* | ||
* It is implemented based on markdown-it-katex plugin. However, that is no | ||
* longer maintained by author. So we have ported math typesetting parser. | ||
* | ||
* @see https://github.com/waylonflinn/markdown-it-katex | ||
*/ | ||
export const markdown = marpitPlugin( | ||
(md, updateState: (rendered: boolean) => void = () => {}) => { | ||
const genOpts = (display: boolean) => { | ||
const math: MathOptions = md.marpit.options.math | ||
|
||
return { | ||
...(typeof math === 'object' && typeof math.katexOption === 'object' | ||
? math.katexOption | ||
: {}), | ||
display, | ||
} | ||
} | ||
|
||
md.core.ruler.before('block', 'marp_math_initialize', (state) => { | ||
if (state.inlineMode) return | ||
|
||
updateState(false) | ||
|
||
if (md.marpit.options.math) { | ||
md.block.ruler.enable('marp_math_block') | ||
md.inline.ruler.enable('marp_math_inline') | ||
} else { | ||
md.block.ruler.disable('marp_math_block') | ||
md.inline.ruler.disable('marp_math_inline') | ||
} | ||
}) | ||
|
||
// Inline | ||
md.inline.ruler.after('escape', 'marp_math_inline', (state, silent) => { | ||
if (parseInlineMath(state, silent)) { | ||
updateState(true) | ||
return true | ||
} | ||
return false | ||
}) | ||
|
||
md.renderer.rules.marp_math_inline = (tokens, idx) => { | ||
const { content } = tokens[idx] | ||
|
||
try { | ||
return adaptor.outerHTML(mathDocument.convert(content, genOpts(false))) | ||
} catch (e) { | ||
console.warn(e) | ||
return content | ||
} | ||
} | ||
|
||
// Block | ||
md.block.ruler.after( | ||
'blockquote', | ||
'marp_math_block', | ||
(state, start, end, silent) => { | ||
if (parseMathBlock(state, start, end, silent)) { | ||
updateState(true) | ||
return true | ||
} | ||
return false | ||
}, | ||
{ alt: ['paragraph', 'reference', 'blockquote', 'list'] } | ||
) | ||
|
||
md.renderer.rules.marp_math_block = (tokens, idx) => { | ||
const { content } = tokens[idx] | ||
|
||
try { | ||
return `<p>${adaptor.outerHTML( | ||
mathDocument.convert(content, genOpts(true)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By comparing to KaTeX rendering, the rendered math block is not centering. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Solved, check it. const { Marp } = require("./lib/marp.js")
const marp = new Marp({ math: true, mathjax: false})
const md = `
# Elementary Math
$$
1+1 = 2
$$
`
const r = marp.render(md)
console.log(r.html + `<style>${r.css}</style>`) |
||
)}</p>` | ||
} catch (e) { | ||
console.warn(e) | ||
return `<p>${content}</p>` | ||
} | ||
} | ||
} | ||
) | ||
|
||
export function css(): string { | ||
return adaptor.textContent(svg.styleSheet(mathDocument) as any) | ||
} | ||
|
||
function isValidDelim(state, pos = state.pos) { | ||
const ret = { openable: true, closable: true } | ||
const { posMax, src } = state | ||
const prev = pos > 0 ? src.charCodeAt(pos - 1) : -1 | ||
const next = pos + 1 <= posMax ? src.charCodeAt(pos + 1) : -1 | ||
|
||
if (next === 0x20 || next === 0x09) ret.openable = false | ||
if (prev === 0x20 || prev === 0x09 || (next >= 0x30 && next <= 0x39)) { | ||
ret.closable = false | ||
} | ||
|
||
return ret | ||
} | ||
|
||
function parseInlineMath(state, silent) { | ||
const { src, pos } = state | ||
if (src[pos] !== '$') return false | ||
|
||
const addPending = (stt: string) => (state.pending += stt) | ||
const found = (manipulation: () => void, newPos: number) => { | ||
if (!silent) manipulation() | ||
state.pos = newPos | ||
return true | ||
} | ||
|
||
const start = pos + 1 | ||
if (!isValidDelim(state).openable) return found(() => addPending('$'), start) | ||
|
||
let match = start | ||
while ((match = src.indexOf('$', match)) !== -1) { | ||
let dollarPos = match - 1 | ||
while (src[dollarPos] === '\\') dollarPos -= 1 | ||
|
||
if ((match - dollarPos) % 2 === 1) break | ||
match += 1 | ||
} | ||
|
||
if (match === -1) return found(() => addPending('$'), start) | ||
if (match - start === 0) return found(() => addPending('$$'), start + 1) | ||
if (!isValidDelim(state, match).closable) { | ||
return found(() => addPending('$'), start) | ||
} | ||
|
||
return found(() => { | ||
const token = state.push('marp_math_inline', 'math', 0) | ||
token.markup = '$' | ||
token.content = src.slice(start, match) | ||
}, match + 1) | ||
} | ||
|
||
function parseMathBlock(state, start, end, silent) { | ||
const { blkIndent, bMarks, eMarks, src, tShift } = state | ||
let pos = bMarks[start] + tShift[start] | ||
let max = eMarks[start] | ||
|
||
if (pos + 2 > max || src.slice(pos, pos + 2) !== '$$') return false | ||
if (silent) return true | ||
|
||
pos += 2 | ||
|
||
let firstLine = src.slice(pos, max) | ||
let lastLine | ||
let found = firstLine.trim().slice(-2) === '$$' | ||
|
||
if (found) firstLine = firstLine.trim().slice(0, -2) | ||
|
||
let next = start | ||
for (; !found; ) { | ||
next += 1 | ||
if (next >= end) break | ||
|
||
pos = bMarks[next] + tShift[next] | ||
max = eMarks[next] | ||
if (pos < max && tShift[next] < blkIndent) break | ||
|
||
const target = src.slice(pos, max).trim() | ||
|
||
if (target.slice(-2) === '$$') { | ||
found = true | ||
lastLine = src.slice(pos, src.slice(0, max).lastIndexOf('$$')) | ||
} | ||
} | ||
|
||
state.line = next + 1 | ||
|
||
const token = state.push('marp_math_block', 'math', 0) | ||
token.block = true | ||
token.content = '' | ||
token.map = [start, state.line] | ||
token.markup = '$$' | ||
|
||
if (firstLine?.trim()) token.content += `${firstLine}\n` | ||
token.content += state.getLines(start + 1, next, tShift[start], true) | ||
if (lastLine?.trim()) token.content += lastLine | ||
|
||
return true | ||
} |
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
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.
I think that interface of constructor option is not intuitive.
math: 'mathjax'
would be better than an individual option.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.
Nice idea. Let me confirm you.
Do you want to do like this?