|
|
@@ -3,30 +3,84 @@
|
|
|
* katex.min.js来源 https://github.com/rojer95/katex-mini
|
|
|
*/
|
|
|
import parse from './katex.min'
|
|
|
+// import parse from "@rojer/katex-mini";
|
|
|
|
|
|
function Latex () {
|
|
|
|
|
|
}
|
|
|
|
|
|
Latex.prototype.onParse = function (node, vm) {
|
|
|
- // $...$包裹的内容为latex公式
|
|
|
- if (!vm.options.editable && node.type === 'text' && node.text.includes('$')) {
|
|
|
- const part = node.text.split(/(\${1,2})/)
|
|
|
+ // $...$、$$...$$、\(...\)、\[...\]包裹的内容为latex公式
|
|
|
+ if (!vm.options.editable && node.type === 'text' &&
|
|
|
+ (node.text.includes('$') || node.text.includes('\\(') || node.text.includes('\\['))) {
|
|
|
+ console.log(node.text)
|
|
|
+ // 同时匹配 $、$$、\(、\)、\[、\] 分隔符
|
|
|
+ const part = node.text.split(/(\$\$|\$|\\\(|\\\)|\\\[|\\\])/)
|
|
|
const children = []
|
|
|
- let status = 0
|
|
|
+ let status = 0 // 0-普通文本, 1-行内公式开始, 2-块公式开始
|
|
|
+ let formulaType = '' // 记录公式的起始分隔符类型
|
|
|
+ let formulaContent = '' // 临时存储公式内容
|
|
|
+
|
|
|
for (let i = 0; i < part.length; i++) {
|
|
|
if (i % 2 === 0) {
|
|
|
// 文本内容
|
|
|
if (part[i]) {
|
|
|
if (status === 0) {
|
|
|
+ // 普通文本
|
|
|
children.push({
|
|
|
type: 'text',
|
|
|
text: part[i]
|
|
|
})
|
|
|
} else {
|
|
|
+ // 在公式内部,累积内容
|
|
|
+ formulaContent += part[i]
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 分隔符
|
|
|
+ const delimiter = part[i]
|
|
|
+
|
|
|
+ if (status === 0) {
|
|
|
+ // 当前不在公式内,这是起始分隔符
|
|
|
+ if (delimiter === '$') {
|
|
|
+ status = 1
|
|
|
+ formulaType = '$'
|
|
|
+ formulaContent = ''
|
|
|
+ } else if (delimiter === '$$') {
|
|
|
+ status = 2
|
|
|
+ formulaType = '$$'
|
|
|
+ formulaContent = ''
|
|
|
+ } else if (delimiter === '\\(') {
|
|
|
+ status = 1
|
|
|
+ formulaType = '\\('
|
|
|
+ formulaContent = ''
|
|
|
+ } else if (delimiter === '\\[') {
|
|
|
+ status = 2
|
|
|
+ formulaType = '\\['
|
|
|
+ formulaContent = ''
|
|
|
+ } else {
|
|
|
+ // 没有起始符的结束符,当作普通文本
|
|
|
+ children.push({
|
|
|
+ type: 'text',
|
|
|
+ text: delimiter
|
|
|
+ })
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 当前在公式内,检查是否是对应的结束分隔符
|
|
|
+ let isClosing = false
|
|
|
+
|
|
|
+ if ((formulaType === '$' && delimiter === '$') ||
|
|
|
+ (formulaType === '$$' && delimiter === '$$') ||
|
|
|
+ (formulaType === '\\(' && delimiter === '\\)') ||
|
|
|
+ (formulaType === '\\[' && delimiter === '\\]')) {
|
|
|
+ isClosing = true
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isClosing) {
|
|
|
+ // 找到匹配的结束符,渲染公式
|
|
|
if (status === 1) {
|
|
|
// 行内公式
|
|
|
- const nodes = parse.default(part[i])
|
|
|
+ const nodes = parse.default(formulaContent, {})
|
|
|
children.push({
|
|
|
name: 'span',
|
|
|
attrs: {},
|
|
|
@@ -34,9 +88,9 @@ Latex.prototype.onParse = function (node, vm) {
|
|
|
f: 'display:inline-block',
|
|
|
children: nodes
|
|
|
})
|
|
|
- } else {
|
|
|
+ } else if (status === 2) {
|
|
|
// 块公式
|
|
|
- const nodes = parse.default(part[i], {
|
|
|
+ const nodes = parse.default(formulaContent, {
|
|
|
displayMode: true
|
|
|
})
|
|
|
children.push({
|
|
|
@@ -47,28 +101,25 @@ Latex.prototype.onParse = function (node, vm) {
|
|
|
children: nodes
|
|
|
})
|
|
|
}
|
|
|
+ // 重置状态
|
|
|
+ status = 0
|
|
|
+ formulaType = ''
|
|
|
+ formulaContent = ''
|
|
|
+ } else {
|
|
|
+ // 不是匹配的结束符,将分隔符作为公式内容的一部分
|
|
|
+ formulaContent += delimiter
|
|
|
}
|
|
|
}
|
|
|
- } else {
|
|
|
- // 分隔符
|
|
|
- if (part[i] === '$' && part[i + 2] === '$') {
|
|
|
- // 行内公式
|
|
|
- status = 1
|
|
|
- part[i + 2] = ''
|
|
|
- } else if (part[i] === '$$' && part[i + 2] === '$$') {
|
|
|
- // 块公式
|
|
|
- status = 2
|
|
|
- part[i + 2] = ''
|
|
|
- } else {
|
|
|
- if (part[i] && part[i] !== '$$') {
|
|
|
- // 普通$符号
|
|
|
- part[i + 1] = part[i] + part[i + 1]
|
|
|
- }
|
|
|
- // 重置状态
|
|
|
- status = 0
|
|
|
- }
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ // 如果还有未闭合的公式内容,作为普通文本处理
|
|
|
+ if (status !== 0 && formulaContent) {
|
|
|
+ children.push({
|
|
|
+ type: 'text',
|
|
|
+ text: (formulaType === '\\(' ? '\\(' : formulaType === '\\[' ? '\\[' : formulaType) + formulaContent
|
|
|
+ })
|
|
|
+ }
|
|
|
delete node.type
|
|
|
delete node.text
|
|
|
node.name = 'span'
|