-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
Indented highlight
shortcode brings in the indentation as part of the code block
#4717
Comments
Updated the tests showing the bug with smaller/targeted test files. |
@bep can you please tag this as a bug (and hopefully see if an easy fix is possible for this? |
Can this be tagged as a Bug please? This issue prevents me from using |
@bep can you please add this issue to a near term milestone. This particular issue prevents me from using highlight shortcode in lists (which I seem to end up using a lot more than general, I think). Also, I don't have any outside-hugo workaround for this. Thanks. |
Your shortcode variant shows the hightlighting as a table with line numbers, you can adjust the indentation with CSS, I guess, but there will be indentation ...
If you remove the "linenos=table", that should fix it. |
The indentation is due to extra spaces in the pre block. And also the closing tag of the highlight shortcode introduces a blank line. The indentation also increases with each nested list item. So it gets very tricky to fix with CSS (also not sure if it would be possible because then I need to make the CSS rules apply only on the badly indented blocks created by highlight shortcode and not by the fenced blocks).
But I need to use the line numbers and so I am using the highlight shortcode. I use the fenced code blocks when I don't need line numbers. Can you please reopen this issue? The root cause is that the highlight shortcode absorbs all the indentation whitespace which it should not. It should be behaving like indented fenced code blocks. |
OK, but in any case you need to take that with Chroma. Note that the code fence example and shortcode example are two totally different cases. |
When using fenced code blocksFrom https://ox-hugo.scripter.co/test/posts/source-block-indented/#code-blocks-in-list-using-code-fences: When using
|
From the above screenshots, see that Chroma is used in both cases, and Chroma knows nothing about the highlight shortcode. I think that Chroma simply wraps the code blocks that Hugo passes to it in pre tags. Isn't that right? Chroma also cannot blindly remove all the leading spaces, because what if one actually wants indentation in the code blocks (Python comes to mind). |
One more take at explaining the issue: |
I have reopened this to track this upstream issue. I'm not spending more time investigating this. Others are welcome, but I suggest you start by looking at what you get from Chroma. Also, for the future: When doing a "it works fine for code fences, but fails for shortcodes", it would be good if you do the same thing in the two cases. |
I don't know Go, but trying some poorman's debug here using printing of debug info to stdout .. I am still not convinced that this is a Chroma issue.. see below. I added those two func (h highlighters) chromaHighlight(code, lang, optsStr string) (string, error) {
opts, err := h.cs.parsePygmentsOpts(optsStr)
if err != nil {
jww.ERROR.Print(err.Error())
return code, err
}
style, found := opts["style"]
if !found || style == "" {
style = "friendly"
}
f, err := h.cs.chromaFormatterFromOptions(opts)
if err != nil {
jww.ERROR.Print(err.Error())
return code, err
}
b := bp.GetBuffer()
defer bp.PutBuffer(b)
// Added debug
fmt.Printf("[%s] input to Chroma = `%s'\n", lang, code)
err = chromaHighlight(b, code, lang, style, f)
if err != nil {
jww.ERROR.Print(err.Error())
return code, err
}
// Added debug
fmt.Printf("output from Chroma = `%s'\n\n", b.String())
return h.injectCodeTag(`<div class="highlight">`+b.String()+"</div>", lang), nil
} Fenced code blockFor the code block in:
I get:
Notice that the highlight shortcode with tableFor the code block in:
I get:
Notice that the Now I need your help in understanding where that Line 83 in 7c30e2c
|
@bep OK, I have another proof that this has little to do with Chroma. This time, I removed the linenos and linenostart args. With this in the content file:
I get this in debug:
|
highlight
shortcode brings in the indentation as part of the code block
@bep What do you think of the above analysis? Can you remove the "upstream" tag? I believe the fix is simple but I don't know how to code that in Go:
|
I claim that
That Hugo can work around this by implementing a 8 step algo isn't very convincing ... Note that the Christmas train has left for this particular issue. I started to look at it assuming we (Hugo) did something stupid (adding some space), but that isn't the case. |
Did you read through the debug messages I pasted? The whitespace exists in the Pinging @alecthomas for some help here .. |
Will come back to this ... after Christmas. |
As I see it, this is local to Hugo, not upstream. The shortcode parser doesn't care where the shortcode is in the document and doesn't understand the context of the surrounding markdown. It doesn't know that all of the leading whitespace in your code block is for the surrounding markdown list. It just sees whitespace and assumes you put it there for a reason. Making the shortcode parser "markdown-aware" would take a lot of work, I think, but I'm wondering if this scenario could be solved easier if the shortcode parser was a BFv2 renderer. Either way, I don't think this is a trivial matter. |
@jmooring I had been this this shortcode: shortcodes/highlight.html{{- $code := (trim .Inner "\n\r") -}}
{{- $lang := .Get 0 -}}
{{- $options := .Get 1 | default "" -}}
{{- $code_ending_in_line_with_spaces := (findRE "[\\r\\n][[:blank:]]+$" $code) -}}
{{/* Ref: https://github.com/gohugoio/hugo/issues/4717#issuecomment-449375012 */}}
{{- with $code_ending_in_line_with_spaces -}}
{{- $code = (replace $code "\r" "") -}} {{/* Windows -> Unix style line endings for ease in further parsing */}}
{{- $offset := (trim (index . 0) "\n") -}}
{{- $lines := (split $code "\n") -}}
{{- $num_lines := (len $lines) -}}
{{- $scratch := newScratch -}}
{{- $scratch.Add "lines_minus_offset" (slice) -}}
{{- range $i, $line := $lines -}}
{{- $line_minus_offset := (strings.TrimPrefix $offset $line) -}}
{{- if (lt $i (sub $num_lines 1)) -}} {{/* Do not add the last blank line */}}
{{- $scratch.Add "lines_minus_offset" (slice $line_minus_offset) -}}
{{- end -}}
{{- end -}}
{{- $code = (delimit ($scratch.Get "lines_minus_offset") "\n") -}}
{{- $scratch.Delete "lines_minus_offset" -}}
{{- end -}}
{{- highlight $code $lang $options -}} That shortcode has been working great for me (it was also submitted in a PR with a test in #5553). I tested out your shortcode and it's working well as well 👍 . |
I was asked if my recent "indentation fix" fixes this issue, but no, this is a different setup. Hugo currently includes every bytes (include whitespace) between open/close shortcode as the |
This commit adds a new `.InnerDeindent` method to the shortcode context, which is `.Inner` with any indendation removed. This is then used in the built-in `highlight` shortcode to prevent the extra whitespace getting hightlighted. Fixes gohugoio#4717
This commit adds a new `.InnerDeindent` method to the shortcode context, which is `.Inner` with any indendation removed. This is then used in the built-in `highlight` shortcode to prevent the extra whitespace getting hightlighted. Fixes #4717
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
So the bug seems to be localized to Hugo and not Blackfriday.
Here is a test page demonstrating the bug:
In the same test page above, the first section has the same code blocks, but done using the code fences, where that extra indentation issue is not seen.
Another effect of not removing that indentation is that the indentation before the closing
{{< /highlight >}}
creates an extra blank line in the final HTML.The text was updated successfully, but these errors were encountered: