Skip to content

Commit ef94350

Browse files
authored
Merge pull request microsoft#261467 from microsoft/copilot/fix-261440
Fix KaTeX regex to prevent false math detection in PowerShell code
2 parents d597731 + ab58d92 commit ef94350

6 files changed

+36
-7
lines changed

src/vs/workbench/contrib/markdown/browser/markedKatexSupport.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export namespace MarkedKatexExtension {
169169
export interface MarkedKatexOptions extends KatexOptions { }
170170

171171
const inlineRule = /^(\${1,2})(?!\$)((?:\\.|[^\\\n])*?(?:\\.|[^\\\n\$]))\1(?=[\s?!\.,:'\uff1f\uff01\u3002\uff0c\uff1a']|$)/;
172-
const inlineRuleNonStandard = /^(\${1,2})(?!\$)((?:\\.|[^\\\n])*?(?:\\.|[^\\\n\$]))\1/; // Non-standard, even if there are no spaces before and after $ or $$, try to parse
172+
const inlineRuleNonStandard = /^(?<![a-zA-Z0-9])(\${1,2})(?!\$)((?:\\.|[^\\\n])*?(?:\\.|[^\\\n\$]))\1(?![a-zA-Z0-9])/; // Non-standard, but ensure opening $ is not preceded and closing $ is not followed by word/number characters
173173

174174
const blockRule = /^(\${1,2})\n((?:\\[^]|[^\\])+?)\n\1(?:\n|$)/;
175175

@@ -214,13 +214,10 @@ export namespace MarkedKatexExtension {
214214
if (index === -1) {
215215
return;
216216
}
217-
const f = nonStandard ? index > -1 : index === 0 || indexSrc.charAt(index - 1) === ' ';
218-
if (f) {
219-
const possibleKatex = indexSrc.substring(index);
220217

221-
if (possibleKatex.match(ruleReg)) {
222-
return index;
223-
}
218+
const possibleKatex = indexSrc.substring(index);
219+
if (possibleKatex.match(ruleReg)) {
220+
return index;
224221
}
225222

226223
indexSrc = indexSrc.substring(index + 1).replace(/^\$+/, '');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>The cost is $10dollars for this item</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>for ($i = 1; $i -le 20; $i++) { echo "hello world"; Start-Sleep 1 }</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p><span class="katex"><span class="katex-mathml"><math><semantics><mrow><mfrac><mn>1</mn><mn>2</mn></mfrac></mrow><annotation encoding="application/x-tex">\frac{1}{2}</annotation></semantics></math></span><span class="katex-html"><span class="base"><span style="height: 1.1901em; vertical-align: -0.345em" class="strut"></span><span class="mord"><span class="mopen nulldelimiter"></span><span class="mfrac"><span class="vlist-t vlist-t2"><span class="vlist-r"><span style="height: 0.8451em" class="vlist"><span style="top: -2.655em"><span style="height: 3em" class="pstrut"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span><span style="top: -3.23em"><span style="height: 3em" class="pstrut"></span><span style="border-bottom-width: 0.04em" class="frac-line"></span></span><span style="top: -3.394em"><span style="height: 3em" class="pstrut"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span style="height: 0.345em" class="vlist"><span></span></span></span></span></span><span class="mclose nulldelimiter"></span></span></span></span></span> at start, and at end <span class="katex"><span class="katex-mathml"><math><semantics><mrow><msup><mi>x</mi><mn>2</mn></msup></mrow><annotation encoding="application/x-tex">x^2</annotation></semantics></math></span><span class="katex-html"><span class="base"><span style="height: 0.8141em" class="strut"></span><span class="mord"><span class="mord mathnormal">x</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span style="height: 0.8141em" class="vlist"><span style="top: -3.063em; margin-right: 0.05em"><span style="height: 2.7em" class="pstrut"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">2</span></span></span></span></span></span></span></span></span></span></span></p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>Hello (<span class="katex"><span class="katex-mathml"><math><semantics><mrow><mfrac><mn>1</mn><mn>2</mn></mfrac></mrow><annotation encoding="application/x-tex">\frac{1}{2}</annotation></semantics></math></span><span class="katex-html"><span class="base"><span style="height: 1.1901em; vertical-align: -0.345em" class="strut"></span><span class="mord"><span class="mopen nulldelimiter"></span><span class="mfrac"><span class="vlist-t vlist-t2"><span class="vlist-r"><span style="height: 0.8451em" class="vlist"><span style="top: -2.655em"><span style="height: 3em" class="pstrut"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">2</span></span></span></span><span style="top: -3.23em"><span style="height: 3em" class="pstrut"></span><span style="border-bottom-width: 0.04em" class="frac-line"></span></span><span style="top: -3.394em"><span style="height: 3em" class="pstrut"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight"><span class="mord mtight">1</span></span></span></span></span><span class="vlist-s">​</span></span><span class="vlist-r"><span style="height: 0.345em" class="vlist"><span></span></span></span></span></span><span class="mclose nulldelimiter"></span></span></span></span></span>) and [<span class="katex"><span class="katex-mathml"><math><semantics><mrow><msup><mi>x</mi><mn>2</mn></msup></mrow><annotation encoding="application/x-tex">x^2</annotation></semantics></math></span><span class="katex-html"><span class="base"><span style="height: 0.8141em" class="strut"></span><span class="mord"><span class="mord mathnormal">x</span><span class="msupsub"><span class="vlist-t"><span class="vlist-r"><span style="height: 0.8141em" class="vlist"><span style="top: -3.063em; margin-right: 0.05em"><span style="height: 2.7em" class="pstrut"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mtight">2</span></span></span></span></span></span></span></span></span></span></span>] work fine</p>

src/vs/workbench/contrib/markdown/test/browser/markdownKatexSupport.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Copyright (c) Microsoft Corporation. All rights reserved.
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
*--------------------------------------------------------------------------------------------*/
5+
import assert from 'assert';
56
import { getWindow } from '../../../../../base/browser/dom.js';
67
import { basicMarkupHtmlTags, defaultAllowedAttrs } from '../../../../../base/browser/domSanitize.js';
78
import { renderMarkdown } from '../../../../../base/browser/markdownRenderer.js';
@@ -28,11 +29,13 @@ suite('Markdown Katex Support Test', () => {
2829

2930
test('Basic inline equation', async () => {
3031
const rendered = await renderMarkdownWithKatex('Hello $\\frac{1}{2}$ World!');
32+
assert.ok(rendered.element.innerHTML.includes('katex'));
3133
await assertSnapshot(rendered.element.innerHTML);
3234
});
3335

3436
test('Should support inline equation wrapped in parans', async () => {
3537
const rendered = await renderMarkdownWithKatex('Hello ($\\frac{1}{2}$) World!');
38+
assert.ok(rendered.element.innerHTML.includes('katex'));
3639
await assertSnapshot(rendered.element.innerHTML);
3740
});
3841

@@ -43,6 +46,31 @@ suite('Markdown Katex Support Test', () => {
4346
'\\int_{-\\infty}^{\\infty} e^{-x^2} dx = \\sqrt{\\pi}',
4447
'$$',
4548
].join('\n'));
49+
assert.ok(rendered.element.innerHTML.includes('katex'));
50+
await assertSnapshot(rendered.element.innerHTML);
51+
});
52+
53+
test('Should not render math when dollar sign is preceded by word character', async () => {
54+
const rendered = await renderMarkdownWithKatex('for ($i = 1; $i -le 20; $i++) { echo "hello world"; Start-Sleep 1 }');
55+
assert.ok(!rendered.element.innerHTML.includes('katex'));
56+
await assertSnapshot(rendered.element.innerHTML);
57+
});
58+
59+
test('Should not render math when dollar sign is followed by word character', async () => {
60+
const rendered = await renderMarkdownWithKatex('The cost is $10dollars for this item');
61+
assert.ok(!rendered.element.innerHTML.includes('katex'));
62+
await assertSnapshot(rendered.element.innerHTML);
63+
});
64+
65+
test('Should still render math with special characters around dollars', async () => {
66+
const rendered = await renderMarkdownWithKatex('Hello ($\\frac{1}{2}$) and [$x^2$] work fine');
67+
assert.ok(rendered.element.innerHTML.includes('katex'));
68+
await assertSnapshot(rendered.element.innerHTML);
69+
});
70+
71+
test('Should still render math at start and end of line', async () => {
72+
const rendered = await renderMarkdownWithKatex('$\\frac{1}{2}$ at start, and at end $x^2$');
73+
assert.ok(rendered.element.innerHTML.includes('katex'));
4674
await assertSnapshot(rendered.element.innerHTML);
4775
});
4876
});

0 commit comments

Comments
 (0)