mirror of
https://github.com/docmost/docmost.git
synced 2025-11-13 15:22:40 +10:00
fix: markdown math import (#529)
* fix: markdown math block import * fix: block and inline math import * cleanup
This commit is contained in:
@ -117,7 +117,7 @@ function mathBlock(turndownService: TurndownService) {
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
replacement: function (content: any, node: HTMLInputElement) {
|
replacement: function (content: any, node: HTMLInputElement) {
|
||||||
return `\n$$${content}$$\n`;
|
return `\n$$\n${content}\n$$\n`;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
import { marked } from 'marked';
|
import { marked } from 'marked';
|
||||||
import { calloutExtension } from './callout.marked';
|
import { calloutExtension } from './callout.marked';
|
||||||
|
import { mathBlockExtension } from './math-block.marked';
|
||||||
|
import { mathInlineExtension } from "./math-inline.marked";
|
||||||
|
|
||||||
marked.use({
|
marked.use({
|
||||||
renderer: {
|
renderer: {
|
||||||
@ -26,7 +28,7 @@ marked.use({
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
marked.use({ extensions: [calloutExtension] });
|
marked.use({ extensions: [calloutExtension, mathBlockExtension, mathInlineExtension] });
|
||||||
|
|
||||||
export async function markdownToHtml(markdownInput: string): Promise<string> {
|
export async function markdownToHtml(markdownInput: string): Promise<string> {
|
||||||
const YAML_FONT_MATTER_REGEX = /^\s*---[\s\S]*?---\s*/;
|
const YAML_FONT_MATTER_REGEX = /^\s*---[\s\S]*?---\s*/;
|
||||||
|
|||||||
@ -0,0 +1,37 @@
|
|||||||
|
import { Token, marked } from 'marked';
|
||||||
|
|
||||||
|
interface MathBlockToken {
|
||||||
|
type: 'mathBlock';
|
||||||
|
text: string;
|
||||||
|
raw: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const mathBlockExtension = {
|
||||||
|
name: 'mathBlock',
|
||||||
|
level: 'block',
|
||||||
|
start(src: string) {
|
||||||
|
return src.match(/\$\$/)?.index ?? -1;
|
||||||
|
},
|
||||||
|
tokenizer(src: string): MathBlockToken | undefined {
|
||||||
|
const rule = /^\$\$(?!(\$))([\s\S]+?)\$\$/;
|
||||||
|
const match = rule.exec(src);
|
||||||
|
|
||||||
|
if (match) {
|
||||||
|
return {
|
||||||
|
type: 'mathBlock',
|
||||||
|
raw: match[0],
|
||||||
|
text: match[2]?.trim(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
},
|
||||||
|
renderer(token: Token) {
|
||||||
|
const mathBlockToken = token as MathBlockToken;
|
||||||
|
// parse to prevent escaping slashes
|
||||||
|
const latex = marked
|
||||||
|
.parse(mathBlockToken.text)
|
||||||
|
.toString()
|
||||||
|
.replace(/<(\/)?p>/g, '');
|
||||||
|
|
||||||
|
return `<div data-type="${mathBlockToken.type}" data-katex="true">${latex}</div>`;
|
||||||
|
},
|
||||||
|
};
|
||||||
@ -0,0 +1,55 @@
|
|||||||
|
import { Token, marked } from 'marked';
|
||||||
|
|
||||||
|
interface MathInlineToken {
|
||||||
|
type: 'mathInline';
|
||||||
|
text: string;
|
||||||
|
raw: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const inlineMathRegex = /^\$(?!\s)(.+?)(?<!\s)\$(?!\d)/;
|
||||||
|
|
||||||
|
export const mathInlineExtension = {
|
||||||
|
name: 'mathInline',
|
||||||
|
level: 'inline',
|
||||||
|
start(src: string) {
|
||||||
|
let index: number;
|
||||||
|
let indexSrc = src;
|
||||||
|
|
||||||
|
while (indexSrc) {
|
||||||
|
index = indexSrc.indexOf('$');
|
||||||
|
if (index === -1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const f = index === 0 || indexSrc.charAt(index - 1) === ' ';
|
||||||
|
if (f) {
|
||||||
|
const possibleKatex = indexSrc.substring(index);
|
||||||
|
if (possibleKatex.match(inlineMathRegex)) {
|
||||||
|
return index;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
indexSrc = indexSrc.substring(index + 1).replace(/^\$+/, '');
|
||||||
|
}
|
||||||
|
},
|
||||||
|
tokenizer(src: string): MathInlineToken | undefined {
|
||||||
|
const match = inlineMathRegex.exec(src);
|
||||||
|
|
||||||
|
if (match) {
|
||||||
|
return {
|
||||||
|
type: 'mathInline',
|
||||||
|
raw: match[0],
|
||||||
|
text: match[1]?.trim(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
},
|
||||||
|
renderer(token: Token) {
|
||||||
|
const mathInlineToken = token as MathInlineToken;
|
||||||
|
// parse to prevent escaping slashes
|
||||||
|
const latex = marked
|
||||||
|
.parse(mathInlineToken.text)
|
||||||
|
.toString()
|
||||||
|
.replace(/<(\/)?p>/g, '');
|
||||||
|
|
||||||
|
return `<span data-type="${mathInlineToken.type}" data-katex="true">${latex}</span>`;
|
||||||
|
},
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user