1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
| /**
| * @fileoverview latex 插件
| * katex.min.js来源 https://github.com/rojer95/katex-mini
| */
| const parse = require('./katex.min')
|
| 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})/)
| const children = []
| let status = 0
| 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 {
| if (status === 1) {
| // 行内公式
| const nodes = parse.default(part[i])
| children.push({
| name: 'span',
| attrs: {},
| l: 'T',
| f: 'display:inline-block',
| children: nodes
| })
| } else {
| // 块公式
| const nodes = parse.default(part[i], {
| displayMode: true
| })
| children.push({
| name: 'div',
| attrs: {
| style: 'text-align:center'
| },
| children: nodes
| })
| }
| }
| }
| } 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
| }
| }
| }
| delete node.type
| delete node.text
| node.name = 'span'
| node.attrs = {}
| node.children = children
| }
| }
|
| module.exports = Latex
|
|