Skip to content

Commit

Permalink
fix: math parsing hangs webchat (#5377)
Browse files Browse the repository at this point in the history
* fix: math parsing hangs

* Snaps

* Changelog

* Update packages/bundle/src/markdown/mathExtension/tokenizer.ts

Co-authored-by: William Wong <[email protected]>

* Update packages/bundle/src/markdown/mathExtension/tokenizer.ts

Co-authored-by: William Wong <[email protected]>

---------

Co-authored-by: William Wong <[email protected]>
  • Loading branch information
OEvgeny and compulim authored Nov 13, 2024
1 parent a2105fd commit f4bb6a9
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ Notes: web developers are advised to use [`~` (tilde range)](https://github.com/
- Resolved CSS class name conflicts between component and fluent packages to prevent styling issues, in PR [#5326](https://github.com/microsoft/BotFramework-WebChat/pull/5326), in PR [#5327](https://github.com/microsoft/BotFramework-WebChat/pull/5327), by [@OEvgeny](https://github.com/OEvgeny)
- Fixed [#5350](https://github.com/microsoft/BotFramework-WebChat/issues/pull/5350). Bundled `shiki` in component package, in PR [#5349](https://github.com/microsoft/BotFramework-WebChat/pull5349), by [@compulim](https://github.com/compulim)
- Fixed modal dialog rendering to prevent visual flicker and improve UX when opening code views, in PR [#5374](https://github.com/microsoft/BotFramework-WebChat/pull/5374), by [@OEvgeny](https://github.com/OEvgeny)
- Fixed math parsing that could cause Web Chat to hang when processing certain LaTeX expressions, in PR [#5377](https://github.com/microsoft/BotFramework-WebChat/pull/5377), by [@OEvgeny](https://github.com/OEvgeny)

# Removed

Expand Down
5 changes: 5 additions & 0 deletions __tests__/html2/markdown/math3.html
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@

14. Katex syntax error:
\[\int_0^\infty e^{-x} dx = 1 +}\]

15. LLM generation error:
\[
\
\]
</x-message>
</template>
<main id="webchat"></main>
Expand Down
Binary file modified __tests__/html2/markdown/math3.html.snap-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 3 additions & 4 deletions packages/bundle/src/markdown/mathExtension/tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function createTokenizer(effects: MathEffects, ok: State, nok: State) {

if (code === BACKSLASH) {
effects.consume(code);
return escaped;
return maybeCloseDelimiter;
}

effects.consume(code);
Expand All @@ -54,15 +54,14 @@ export function createTokenizer(effects: MathEffects, ok: State, nok: State) {
return content;
}

function escaped(code) {
function maybeCloseDelimiter(code: Code): State {
if ((!isDisplay && code === CLOSE_PAREN) || (isDisplay && code === CLOSE_BRACKET)) {
effects.consume(code);
effects.exit('mathChunk');
effects.exit('math');
return ok(code);
}

effects.consume(code);
return content;
return content(code);
}
}

0 comments on commit f4bb6a9

Please sign in to comment.