Extra lines in code blocks

Hi everyone,

First message here, I hope this is the right place to ask questions.

I’ve been a very happy Hugo user for a few years now, but I’ve been having issues lately with how code blocks are rendered. I’m pretty sure that this is an issue on my end and that I should be able to easily fix that issue by making some changes to the config file(s) but I can’t seem to find where.

Problem:

Say I write a code block in Markdown for a given language. In this case Python but I’m having the same issue with all the languages I’ve used:

```python
x = 1
print(x)
```

For some reason the above snippet will look like:

x = 1

print(x)

Does anyone know how to fix this please?

If you need more information, I’m using Mint and here’s the current Hugo version I’m on:

hugo version

If what I wrote above makes little sense, here’s the website: https://blanchardjulien.com/

Just open any article and look for some code.

Thanks!
J.

You have a CSS line-height problem.

1 Like

If you change the line height in the developer tools, the display doesn’t change. But what one can see in the HTML source are lots (lots!) of empty lines. Which normally doesn’t matter, but in a pre element, those are preserved, and that’s what we see in the code boxes.

How and why that comes about – no idea without seeing the source code.
So, @Loulou, please post a link to your repository.

1 Like

Yes, it does. Change the line-height on a span element with the cl class to .625.

I’m probably too dense. On this page Julien Blanchard | Explore your dataframes with PyGWalker, I set the style of the first span class="cl" element in the code block to line-height: 0.625 and don’t see any change in the code block.

Nor if I define a global rule

span.cl {
  line-height: 0.625;
}

To quote MDN:

The line-height CSS property sets the height of a line box in horizontal writing modes

line-height doesn’t do anything at all for span elements:

But setting line-height on the pre.chroma element changes everything. As it should. But a value of 0.625 for line-height is ridiculous: 1.2 is considered good for legibility, half of that would have lines run into each other – if not every second one were empty.

1 Like

I am failing to communicate.


The above sets (universally) the font size to 25.6 pixels, then reduces the font-size to 16 pixels within the html element. So the line height is based on 25.6 pixels… I think.

Yes. You set properties on *, not on span.cl. And it’s not clear if that * is inside a nested rule or not.

Anyway:

  • line-height doesn’t work for inline elements, only for block or inline-block.
  • setting a relative line-height refers to the current font-size. So, 0.625 for line-height translates to 62.5% of the font-size. Which would create overlapping lines, obviously. Unless every second line is empty, as in the case of a pre element that preserves the white space.

This is what you get if you set line-height: .625 for a p element:

There, empty lines are not preserved, so the lines are run together because line-height is smaller than font-size.

Add something like this to your stylesheet and you’ll be fine:

.chroma {
    line-height: 1.2rem;
}
1 Like

Tried that in the developer tools, no change. I’m giving up.

I don’t know what to tell you…

Hey folks and thanks for taking the time to try and sort me out!

Also, apologies if my initial post didn’t contain enough information.

@chrillek I don’t have a repo to share with all the files, but can post it on my GitHub if you think that my help

@jmooring public > css shows 1 file with a bit of an unusual name. And here’s what it contains:

So, if I’m following correctly, adding:

.chroma {
    line-height: 1.2rem;
}

… Should fix the issue?

On a possibly related note, I might add that I installed Hugo 3 years ago as well as the Anatole theme. And always copied / pasted that entire folder onto freshly installed OS, alongside a new install of Hugo (therefore on a higher version).

Should I just wipe out everything (just keeping the articles and images) and start over from scratch?

Not quite. .html is a more specific selector than * (which has specificity 0). So, it overrides the * setting of the font-size – nothing is accumulated or multiplied here. Which means that the font-size is set to 62,5 percent of the default for the element with class="html", which is html – be it the user-defined setting or the browser’s if the user didn’t set a preference. Which (in my case) means 11.25 px, since my preference is an 18px sized font.

So, 1rem corresponds to 11.25px (again: in my case) for this HTML document.

Now it’s getting interesting: The font-size for .html is 11.25px, but what happens with .chroma? Its 1 rem equivalent is still 11.25px, but now
* {font-size: 1.6rem;}

gets applied to the .chroma because there is no other rule with a more specific selector. Consequently, .chroma has a calculated font-size of 18px (1.6 times 11.25).

Now, we apply a line-height: 1.2rem to the .chroma element, and that gets us a calculated line-height of 13.5px.

Which is indeed what the developer tools show me for .chroma:

line-height is smaller than font-size. While that gives the desired result (kind of), it’s by no means a solution to the problem (in my opinion). Which is still (in my opinion) that there are empty lines interspersed with the code lines. As the OP already mentioned in their first post, btw. In a pre element, these empty lines will be preserved, so setting a line-height much smaller than the font-size will make the empty lines visually disappear. But they are still there, in the HTML.

That’s just a band-aid. Your code blocks should not contain empty lines. Which they do.

Why? You should get to the root of the issue, namely why you have these empty lines in your code block. If it were me, I’d probably go for a different theme (and better CSS). But that’s another topic.

Ah crap, I quite liked that theme :smiley:

I get your point on the band-aid approach. My initial thought was to run a Python or Bash script that removes these empty lines on all the html files contained in the public folder. But then started looking for a proper solution on the web.

Anyway, thank you and @jmooring for your help.

I think I’m missing something. What is the problem with blank lines inside of a code block? That’s common.

As the OP said: They write
```
line 1
line 2
```

and they get

line 1

line 2

Nothing is wrong with blank lines in a code block. If you want them there. If they get inserted by whatever is processing the MD, that’s probably not desirable.

I’m not sure how your pages are generated or if there’s any postprocessing at build time or live formatting using js…

But the html looks strange. One real blank line after each line after normal

tags. And in your code blocks blank lines inside spans…

So a reproducible one pager repo incl source file and all that code that processes your stuff will imho definitely help.

Fe this page: https://blanchardjulien.com/posts/markdown_to_html/

p.s. the loading time of your pages is quite slow…looks like there is somethin happening

Mobile connected only so no dev tools to dig in

1 Like

Added this to the css file in public > css and no luck :sob:

My bad for not adding in some sort of context around the ask. I just find that the extra blank lines make the code snippets difficult to read. Not so much for the articles I have on JavaScript / TypeScript, but more for the ones using Python. The indentation and blank lines don’t go well together. But that’s just my opinion.

100%, and the worst thing is that if I run hugo server -d to preview the whole site on localhost then everything looks fine.

As discussed earlier I might have messed up with some files when transferring this repo multiple times from one computer onto another.

I might do a clean-up tomorrow as I’ll be working from home.

Anyway I appreciate the help!

OK, now I get it.

The empty lines are in the rendered HTML, not in the markdown.

But you have to view source (Ctrl +U) to see them. Chrome and Firefox hide the blank lines when you use their dev tools to inspect the HTML… which isn’t great. Lesson learned.

As to why you have blank lines? I have no idea. Hugo doesn’t do it by default. I can only reproduce the problem by doing something dumb like this:

{{ .Content | replaceRE "\n" "\n\n" | safeHTML }}
1 Like

Well at least I’m glad it’s something you guys might not have seen before :smiley:

Like I said earlier, will do a fresh reinstall of Hugo tomorrow morning (European time) and will let you know.

And thank you all for your help :slight_smile: