mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-29 03:53:40 -05:00
[visual] Provide command definitions to MathJax (#13761)
GitOrigin-RevId: 845921df363f61d2333c1305b0b3edb86501c296
This commit is contained in:
parent
08c82a24a9
commit
a9177abe5a
2 changed files with 40 additions and 3 deletions
|
@ -150,6 +150,8 @@ export const atomicDecorations = (options: Options) => {
|
|||
['proof', 'Proof'],
|
||||
])
|
||||
|
||||
let commandDefinitions = ''
|
||||
|
||||
const preamble: {
|
||||
from: number
|
||||
to: number
|
||||
|
@ -570,6 +572,22 @@ export const atomicDecorations = (options: Options) => {
|
|||
}
|
||||
|
||||
return false // no markup in verbatim content
|
||||
} else if (
|
||||
nodeRef.type.is('NewCommand') ||
|
||||
nodeRef.type.is('RenewCommand')
|
||||
) {
|
||||
const argumentNode = nodeRef.node.getChild('LiteralArgContent')
|
||||
if (argumentNode) {
|
||||
const argument = state
|
||||
.sliceDoc(argumentNode.from, argumentNode.to)
|
||||
.trim()
|
||||
if (/^\\\w+/.test(argument)) {
|
||||
const content = state.sliceDoc(nodeRef.from, nodeRef.to)
|
||||
if (content) {
|
||||
commandDefinitions += `${content}\n`
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (nodeRef.type.is('Cite')) {
|
||||
// \cite command with a bibkey argument
|
||||
if (shouldDecorate(state, nodeRef)) {
|
||||
|
@ -715,7 +733,11 @@ export const atomicDecorations = (options: Options) => {
|
|||
|
||||
decorations.push(
|
||||
Decoration.replace({
|
||||
widget: new MathWidget(content, displayMode),
|
||||
widget: new MathWidget(
|
||||
content,
|
||||
displayMode,
|
||||
commandDefinitions
|
||||
),
|
||||
block: displayMode,
|
||||
}).range(ancestorNode.from, ancestorNode.to)
|
||||
)
|
||||
|
|
|
@ -5,7 +5,11 @@ import { placeSelectionInsideBlock } from '../selection'
|
|||
export class MathWidget extends WidgetType {
|
||||
destroyed = false
|
||||
|
||||
constructor(public math: string, public displayMode: boolean) {
|
||||
constructor(
|
||||
public math: string,
|
||||
public displayMode: boolean,
|
||||
public preamble?: string
|
||||
) {
|
||||
super()
|
||||
}
|
||||
|
||||
|
@ -26,7 +30,11 @@ export class MathWidget extends WidgetType {
|
|||
}
|
||||
|
||||
eq(widget: MathWidget) {
|
||||
return widget.math === this.math && widget.displayMode === this.displayMode
|
||||
return (
|
||||
widget.math === this.math &&
|
||||
widget.displayMode === this.displayMode &&
|
||||
widget.preamble === this.preamble
|
||||
)
|
||||
}
|
||||
|
||||
updateDOM(element: HTMLElement, view: EditorView) {
|
||||
|
@ -62,6 +70,13 @@ export class MathWidget extends WidgetType {
|
|||
|
||||
if (!this.destroyed) {
|
||||
MathJax.texReset([0]) // equation numbering is disabled, but this is still needed
|
||||
if (this.preamble) {
|
||||
try {
|
||||
await MathJax.tex2svgPromise(this.preamble)
|
||||
} catch {
|
||||
// ignore errors thrown during parsing command definitions
|
||||
}
|
||||
}
|
||||
const math = await MathJax.tex2svgPromise(this.math, {
|
||||
...MathJax.getMetricsFor(element),
|
||||
display: this.displayMode,
|
||||
|
|
Loading…
Reference in a new issue