11ty / eleventy-plugin-syntaxhighlight

A pack of Eleventy plugins for syntax highlighting in Markdown, Liquid, and Nunjucks templates.
https://www.11ty.dev/docs/plugins/syntaxhighlight/
MIT License
129 stars 32 forks source link

diff-javascript code block generates invalid HTML #80

Closed marvinhagemeister closed 1 year ago

marvinhagemeister commented 1 year ago

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:

https://github.com/11ty/eleventy-plugin-syntaxhighlight/blob/ac92151c42996faef19650f34f54a77e4ac210f2/src/markdownSyntaxHighlightOptions.js#L28

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 commented 1 year ago

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