Skip to content

fix(markdown-math): prevent KaTeX parsing conflicts with jQuery selectors#268628

Open
Parasaran-Python wants to merge 1 commit intomicrosoft:mainfrom
Parasaran-Python:268378
Open

fix(markdown-math): prevent KaTeX parsing conflicts with jQuery selectors#268628
Parasaran-Python wants to merge 1 commit intomicrosoft:mainfrom
Parasaran-Python:268378

Conversation

@Parasaran-Python
Copy link
Contributor

@Parasaran-Python Parasaran-Python commented Sep 27, 2025

fix(markdown-math): prevent KaTeX parsing conflicts with jQuery selectors

Adds preprocessing rule to escape dollar signs in non-mathematical contexts
like, $("#id"), $("selector") and shell command substitutions to prevent
KaTeX parse errors.

fix #268378

…tors

Adds preprocessing rule to escape dollar signs in non-mathematical contexts
like $("#id"), $("selector"), and shell command substitutions to prevent
KaTeX parse errors.

fix microsoft#268378
@Parasaran-Python
Copy link
Contributor Author

Hi @mjbvz ,

Issue: KaTeX was attempting to parse jQuery selectors like $("#dialogDetalleZona") as mathematical notation, causing parse errors: "Expected 'EOF', got '#' at position 3".

Root Cause: The markdown-math extension passes all $ patterns to KaTeX without distinguishing between mathematical expressions and other contexts (JavaScript, shell commands, etc.).

Solution: Added preprocessing rule in extensions/markdown-math/src/extension.ts that identifies and escapes non-mathematical $ patterns before KaTeX processing. The fix handles:

  • jQuery/JavaScript: $("#id"), $(".class"), $(document)
  • Shell commands: $(echo), $(pwd), $(ENVIRONMENT_VAR)
  • Programming patterns: $(variable), $(obj.method())

Could you please review this approach? Open to suggestions for improvements.

Copy link
Collaborator

@mjbvz mjbvz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of hardcoding a list of non-math patterns, I think we should update the katex regex to be more strict. Something like:

  • The opening dollar sign cannot be followed by .. We could also potentially disallow ( but there may be more cases where that's valid
  • The closing dollar sign cannot be followed by a (

Please also update the test file to add tests for these cases

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ParseError: KaTeX parse error: Expected 'EOF', got '#'

2 participants