Skip to content

Commit 77815d1

Browse files
authored
feat: #1632 表格中也支持段落公式,段落公式按行内公式渲染 (#1634)
* feat: #1632 表格中也支持段落公式,段落公式按行内公式渲染 * fix: 修改测试用例
1 parent 033ca64 commit 77815d1

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

.changeset/fancy-glasses-send.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'cherry-markdown': patch
3+
---
4+
5+
feat: #1632 表格中也支持段落公式,段落公式按行内公式渲染

packages/cherry-markdown/src/core/hooks/InlineMath.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import ParagraphBase from '@/core/ParagraphBase';
1717
import { escapeFormulaPunctuations, LoadMathModule } from '@/utils/mathjax';
1818
import { getHTML } from '@/utils/dom';
1919
import { isBrowser } from '@/utils/env';
20-
import { getTableRule, isLookbehindSupported } from '@/utils/regexp';
20+
import { getTableRule, isLookbehindSupported, mathBlockReg } from '@/utils/regexp';
2121
import { replaceLookbehind } from '@/utils/lookbehind-replace';
2222

2323
/**
@@ -108,7 +108,8 @@ export default class InlineMath extends ParagraphBase {
108108
return whole
109109
.split('|')
110110
.map((oneTd) => {
111-
return this.makeInlineMath(oneTd);
111+
// 单元格里的段落公式直接替换成行内公式
112+
return this.makeInlineMath(this.transformBlockMathToInlineMath(oneTd));
112113
})
113114
.join('|')
114115
.replace(/\\~D/g, '~D') // 出现反斜杠的情况(如/$e=m^2$)会导致多一个反斜杠,这里替换掉
@@ -125,6 +126,19 @@ export default class InlineMath extends ParagraphBase {
125126
return $str;
126127
}
127128

129+
transformBlockMathToInlineMath(str) {
130+
if (isLookbehindSupported()) {
131+
return str.replace(mathBlockReg, '$1$2~D$3~D$4');
132+
}
133+
return replaceLookbehind(
134+
str,
135+
mathBlockReg,
136+
(whole, match1, match2, match3, match4) => `${match1}${match2}~D${match3}~D${match4}`,
137+
true,
138+
1,
139+
);
140+
}
141+
128142
makeInlineMath(str) {
129143
if (!this.test(str)) {
130144
return str;
@@ -143,7 +157,7 @@ export default class InlineMath extends ParagraphBase {
143157
const ret = {
144158
begin: isLookbehindSupported() ? '((?<!\\\\))~D\\n?' : '(^|[^\\\\])~D\\n?',
145159
content: '(.*?)\\n?',
146-
end: isLookbehindSupported() ? '((?<!\\\\))~D' : '[^\\\\]~D',
160+
end: '(\\s*)~D(?:\\s{0,1})',
147161
};
148162
ret.reg = new RegExp(ret.begin + ret.content + ret.end, 'g');
149163
return ret;

packages/cherry-markdown/src/utils/regexp.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ export function compileRegExp(obj, flags, allowExtendedFlags) {
2222
return new RegExp(source, flags || 'g');
2323
}
2424

25+
export const mathBlockReg = isLookbehindSupported()
26+
? /(\s*)((?<!\\))~D~D\s*([\w\W]*?)(\s*)~D~D(?:\s{0,1})/g
27+
: /(\s*)(^|[^\\])~D~D\s*([\w\W]*?)(\s*)~D~D(?:\s{0,1})/g;
28+
2529
export function isLookbehindSupported() {
2630
try {
2731
new RegExp('(?<=.)');

packages/cherry-markdown/test/core/__snapshots__/CommonMark.spec.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,8 +1066,8 @@ exports[`engine > CommonMark-142 1`] = `
10661066
`;
10671067

10681068
exports[`engine > CommonMark-143 1`] = `
1069-
"<p data-sign="af3a790fee49396f060a5758c9ecd8532c877061921cce2bf056005529a15a9b5" data-type="p" data-lines="5"><del>~~ ruby startline=3 <span class="Cherry-InlineMath" data-type="mathBlock"
1070-
data-lines="2">$%@#$</span><br>def foo(x)<br> return 3<br>end<br></del>~~~~~</p>
1069+
"<p data-sign="29c780a2e3c40841ea3af406d46e744595c68d330ad1df1a7aba53444e500cfa4" data-type="p" data-lines="4"><del>~~ ruby startline=3 <span class="Cherry-InlineMath" data-type="mathBlock"
1070+
data-lines="2">$%@#$</span>def foo(x)<br> return 3<br>end<br></del>~~~~~</p>
10711071
"
10721072
`;
10731073

0 commit comments

Comments
 (0)