Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntax Highlight does not split string as expected #4178

Open
PJUllrich opened this issue May 6, 2024 · 2 comments
Open

Syntax Highlight does not split string as expected #4178

PJUllrich opened this issue May 6, 2024 · 2 comments

Comments

@PJUllrich
Copy link

PJUllrich commented May 6, 2024

This is basically a new issue for #3041 which was closed by the quill-bot.

Steps for Reproduction

Example code

defmodule Test do
  def foo(bar) do
    bar + 2
  end
end

Expected behavior:

The defmodule Test do string is split into three parts hljs-keyword, hljs-title, and hljs-keyword and is highlighted correctly:
CleanShot 2024-05-06 at 10 22 20@2x

CleanShot 2024-05-06 at 10 21 47@2x

Actual behavior:

The string is only split into two parts: hljs-class and hljs-keyword and is not highlighted correctly:

CleanShot 2024-05-06 at 10 23 34@2x CleanShot 2024-05-06 at 10 24 03@2x

Platforms:

    "highlight.js": "^11.9.0",
    "quill": "^2.0.1",

Version:

2.0.1

@PJUllrich
Copy link
Author

PJUllrich commented May 6, 2024

I tracked it down to this piece of the code where the following input is transformed wrongly to the following output.

Somehow, the hljs-class-keyword confuses the highlight(text, language) function and it skips the inner keywords hljs-keyword and hljs-title.

Text Input

defmodule Test do
  def foo(bar) do
    bar + 2
  end
end

Input after call to hljs.highlight()

<span class="hljs-class"><span class="hljs-keyword">defmodule</span> <span class="hljs-title">Test</span></span> <span class="hljs-keyword">do</span>
  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">foo</span></span>(bar) <span class="hljs-keyword">do</span>
    bar + <span class="hljs-number">2</span>
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>

Output of recursive highlight()

{
    "ops": [
        {
            "insert": "defmodule Test",
            "attributes": {
                "code-token": "class"
            }
        },
        {
            "insert": " "
        },
        {
            "insert": "do",
            "attributes": {
                "code-token": "keyword"
            }
        },
        {
            "insert": "\n",
            "attributes": {
                "code-block": "elixir"
            }
        },
        {
            "insert": "  "
        },
        {
            "insert": "def foo",
            "attributes": {
                "code-token": "function"
            }
        },
        {
            "insert": "(bar) "
        },
        {
            "insert": "do",
            "attributes": {
                "code-token": "keyword"
            }
        },
        {
            "insert": "\n",
            "attributes": {
                "code-block": "elixir"
            }
        },
        {
            "insert": "    bar + "
        },
        {
            "insert": "2",
            "attributes": {
                "code-token": "number"
            }
        },
        {
            "insert": "\n",
            "attributes": {
                "code-block": "elixir"
            }
        },
        {
            "insert": "  "
        },
        {
            "insert": "end",
            "attributes": {
                "code-token": "keyword"
            }
        },
        {
            "insert": "\n",
            "attributes": {
                "code-block": "elixir"
            }
        },
        {
            "insert": "end",
            "attributes": {
                "code-token": "keyword"
            }
        },
        {
            "insert": "\n",
            "attributes": {
                "code-block": "elixir"
            }
        }
    ]
}

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

No branches or pull requests

1 participant