Skip to content

Commit b3cb330

Browse files
committed
Add Katex MathMode
1 parent 8db1bb9 commit b3cb330

File tree

3 files changed

+25
-27
lines changed

3 files changed

+25
-27
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/ContentParser.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@ export function ContentParser (content, parsers) {
77
let stringMap = []
88

99
parsers.forEach(function (parserConfig) {
10-
let segments = newContent.match(parserConfig.reg)
11-
12-
if (segments) {
13-
segments.forEach(function (segment) {
14-
let mapItem = {
15-
hash: '$' + md5(segment) + '$',
16-
segment: segment,
17-
content: parserConfig.parser(segment)
18-
}
19-
newContent = newContent.replace(segment, mapItem.hash)
20-
stringMap.push(mapItem)
21-
})
10+
let matches = newContent.match(parserConfig.reg)
11+
12+
while (matches !== null) {
13+
let mapItem = {
14+
hash: md5(matches[0]),
15+
segment: matches[0],
16+
content: parserConfig.parser(matches)
17+
}
18+
newContent = newContent.replace(mapItem.segment, mapItem.hash)
19+
stringMap.push(mapItem)
20+
21+
matches = newContent.match(parserConfig.reg)
2222
}
2323
})
2424

src/components/plugin/KatexParser.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,18 @@ import 'katex/dist/katex.css'
22
import katex from 'katex/dist/katex'
33

44
export default {
5-
reg: RegExp(/(\$+[^$]*\$+)/, 'g'),
6-
parser (segment) {
7-
let matchReg = new RegExp(/^\$+(.*)\$+$/)
8-
let matched = segment.match(matchReg)
9-
10-
if (matched) {
11-
segment = matched[1]
12-
try {
13-
return katex.renderToString(segment)
14-
} catch (exception) {
15-
return '$' + segment + '$'
16-
}
17-
} else {
18-
return ''
5+
reg: /(\$\$?)(.+?)\$\$?/,
6+
parser (matches) {
7+
let isMathMode = false
8+
if (matches[1] === '$$') {
9+
isMathMode = true
10+
}
11+
try {
12+
return katex.renderToString(matches[2], {
13+
displayMode: isMathMode
14+
})
15+
} catch (exception) {
16+
return matches[1] + matches[2] + matches[1]
1917
}
2018
}
2119
}

0 commit comments

Comments
 (0)