Skip to content

bug: Syntax highlighting broken by LaTeX string #353

@dmca-glasgow

Description

@dmca-glasgow

Did you check existing issues?

  • I have read all the tree-sitter docs if it relates to using the parser
  • I have searched the existing issues of tree-sitter-typescript

Tree-Sitter CLI Version, if relevant (output of tree-sitter --version)

No response

Describe the bug

Hello,

I originally created an issue at zed-latex about this syntax highlighting bug but have been directed to the TypeScript language, which I believe is here?

This is an example test with broken syntax-highlighting, where I create a LaTeX string, in TypeScript:

Image

LaTeX makes heavy use of the backslash character, and I've found String.raw helps to ignore character escape sequences.

I noticed removing the line \usepackage{graphicx} fixes the issue. If I remove it the colours are now back to normal:

Image

Steps To Reproduce/Bad Parse Tree

The code from the screenshot:

test('image', async () => {
  const latex = String.raw`
    \documentclass{article}
    \usepackage{graphicx}
    \begin{document}
    \includegraphics[]{image.png}
    \end{document}
  `;

  const markdown = await testProcessor.latex(latex);
  // console.log(markdown);

  const expectedMarkdown = unindentStringAndTrim(`
    ![](image.png)
  `);

  expect(markdown).toBe(expectedMarkdown);

  const html = await testProcessor.md(markdown);
  // console.log(html);

  const expectedHtml = unindentStringAndTrim(`
    <p><img src="image.png" alt="Image" /></p>
  `);

  expect(html).toBe(expectedHtml);

  // const quartoHtml = await markdownToQuartoHtml(markdown);
  // console.log(quartoHtml);
});

(Just looking at GitHub's syntax highlighting here, it also seems to struggle with the line \usepackage{graphicx}. Perhaps \u is problematic as it can represent the unicode escape sequence).

Expected Behavior/Parse Tree

Syntax highlighting works correctly for TypeScript files which include strings of LaTeX code using String.raw.

Repro

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions