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

diff-javascript code block generates invalid HTML #80

Closed
marvinhagemeister opened this issue Mar 19, 2023 · 1 comment
Closed

diff-javascript code block generates invalid HTML #80

marvinhagemeister opened this issue Mar 19, 2023 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@marvinhagemeister
Copy link

Noticed on my blog that every diff block generates unclosed <span>-elements.

```diff-javascript
- foo();
```

I was able to trace the problem down to this particular line:

let lines = html.split("\n").slice(0, -1); // The last line is empty.

It makes the assumption that the last entry in the array is always an empty string, but in my case that's not true. For me the last entry is "</span>". The slicing ignores that and thereby passes invalid HTML from here on.

Putting the slicing behind a check if the last line really is empty solves the problem for me.

let lines = html.split("\n");

// Trim last line if it is empty
if (lines[lines.length - 1] === "") {
  lines = lines.slice(0, -1);
}

I tried reproducing that with the test setup in this repository, but I was unable to do so. I can reproduce the error in a full eleventy setup though.

Git repo where the problem is reproducible: https://github.com/marvinhagemeister/eleventy-diff-syntax-highlight-bug

Steps to reproduce:

  1. clone https://github.com/marvinhagemeister/eleventy-diff-syntax-highlight-bug and cd into it
  2. npm install
  3. npm run build
  4. Generated file dist/foo/index.html contains invalid HTML
@zachleat zachleat added this to the Syntax Highlight v5.0.0 milestone Apr 12, 2023
@zachleat zachleat self-assigned this Apr 12, 2023
@zachleat zachleat added the bug Something isn't working label Apr 12, 2023
@zachleat
Copy link
Member

Great reproduction and recommendation! This will ship with v5.0.0 (today)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants