jlelong / vscode-latex-basics

LaTeX grammars for Visual Studio Code
MIT License
19 stars 10 forks source link

LaTeX and TeX highlighted incorrectly in Markdown #83

Closed BearToCode closed 6 months ago

BearToCode commented 6 months ago

A clear and concise description of what the bug is.

LaTeX and TeX are highlighted incorrectly inside Markdown, they stop working after a \section(and also other commands) is used. I'm not sure whether this issue is related more with this repo, or with the markdown one; I'll move the discussion there in case.

This is the rule that implements LaTeX in Markdown:

      <key>fenced_code_block_latex</key>
      <dict>
        <key>begin</key>
        <string>(^|\G)(\s*)(`{3,}|~{3,})\s*(?i:(latex|tex)((\s+|:|,|\{|\?)[^`]*)?$)</string>
        <key>name</key>
        <string>markup.fenced_code.block.markdown</string>
        <key>end</key>
        <string>(^|\G)(\2|\s{0,3})(\3)\s*$</string>
         // ...
        <key>patterns</key>
        <array>
          <dict>
            <key>begin</key>
            <string>(^|\G)(\s*)(.*)</string>
            <key>while</key>
            <string>(^|\G)(?!\s*([`~]{3,})\s*$)</string>
            <key>contentName</key>
            <string>meta.embedded.block.latex</string>
            <key>patterns</key>
            <array>
              <dict>
                <key>include</key>
                <string>text.tex.latex</string>
              </dict>
            </array>
          </dict>
        </array>

A minimal example to reproduce the issue

Go into vscode and type the following(please unescape the backticks `)

\`\`\`latex
\begin{document}
    \maketitle

    \section{Hello World!} Highlighted correctly!
\end{document} No longer highlighted :(
\`\`\`

Result: snippet

zepinglee commented 6 months ago

I can reproduce that. It seems the last $base in the following block causes the tokenizer enters meta.paragraph.markdown scope and fails to come out until the closing ```. Perhaps it should be changed to $self.

Screenshot 2024-04-08 at 23 39 05

https://github.com/jlelong/vscode-latex-basics/blob/cf291a400201d1c0a857344fc82f05e7d8e57e69/syntaxes/src/LaTeX.tmLanguage.base.yaml#L46-L66

jlelong commented 6 months ago

@zepinglee Good catch. Thanks