Hello, from what I read online, it’s a file that needs to be placed in the layouts/_default/_markup/
folder. I think its name is render-codeblock.html
, but the problem is I can’t find the original file. I would like to add a button to all of my code blocks so I can use clipboard.js. I would be grateful for any suggestions.
This question may sound silly, but does a fenced code block refer to the ones inserted using three backticks (```) ? If yes, then I guess clipboard.js doesn’t work with the highlight shortcode in Hugo.
Yes. The terminology is from the CommonMark specification.
https://spec.commonmark.org/0.31.2/#fenced-code-blocks
They can certainly work together, but the example above is about fenced code blocks.
A fenced code block is preferable to a shortcode because the resulting markdown is portable to other applications (e.g., GitHub, GitLab, Obsidian, Typora, VS Code, etc.).
Thank you for the clarification. I didn’t know about this. I think I need to enable fenced code blocks in hugo.toml
because it’s not activated. I only have pygmentsUseClasses
enabled.
First, fenced code blocks are enabled by default.
Second, pygmentsUseClasses
in the root of your site configuration doesn’t do anything.
This is the default syntax highlighting configuration:
https://gohugo.io/getting-started/configuration-markup/#highlight
If I don’t have pygmentsUseClasses
set to true
, then syntax.css
is not used, I mean the css file generated with hugo gen chromastyles --style=monokai > syntax.css
.
I think I need this instead if I want to use a separate css file:
[markup]
[markup.highlight]
noClasses = false
Am I right?
I’m not sure if code fences work in Org files. I need to check, but so far the code displays in a paragraph when backticks are inserted.
Correction. We’re still reading the legacy configuration keys…
…but you should use the current configuration keys instead:
Legacy config key | New config key |
---|---|
pygmentsCodeFences |
markup.highlight.codeFences |
pygmentsCodefencesGuessSyntax |
markup.highlight.guessSyntax |
pygmentsStyle |
markup.highlight.style |
pygmentsUseClasses |
markup.highlight.noClasses |
Note that you must negate the boolean value with the last one.
So… that little bit of information changes the entire discussion:
- You can’t use fenced code blocks in Org files.
- Render hooks are only applicable to Markdown.
In Org mode you would typically do something like this to highlight code:
#+begin_src go
fmt.Println("Hello world.")
#+end_src
Which is rendered to:
<div class="src src-go">
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-go" data-lang="go"><span style="display:flex;"><span><span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#e6db74">"Hello world."</span>)</span></span></code></pre></div>
</div>
You would have to modify the JS to add a button to the DOM.
Thank you. That makes sense. Then I will try to insert the button via the DOM.
At least I’m glad I learned something new. I wasn’t aware of the new syntax. I’ll modify hugo.toml
and remove pygmentsUseClasses
then.
You could also override Hugo’s embedded highlight shortcode to include a button element. Shortcodes are parsed regardless of content format (i.e., they work in Markdown, AsciiDoc, Org, etc.).
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.