For my website, I am using Hextra theme. The live website is Part 2: Configuring VPS and DNS – minhperry's Blog (the one being mentioned here) with a button to “edit this page on github”, which will bring you to the corresponding repo file.
Currently, this code
~~~bash {filename="bash"}
sudo -u www-data stat /absolute/path/to/specified/root
~~~
will be rendered as
[See image 1 below]
I want to prepend something like user@machine:~$, however it would also be considered as content when user copies. I have asked the creator and they said to use codeblock render hook: Bash with $ at front. · imfing/hextra · Discussion #484 · GitHub
After messing around, I found the source for the code rendering of the theme: hexxtra/layouts/_default/_markup/render-codeblock.html
The highlight function is solely responsible for highlighting the language, so I can’t simply just intercept it by adding some html anywhere.
[See image 2 below]
Is my niche highlighting case even possible?
Looking at the HTML generated by your theme, it seems (!) that every line of highlighted code is wrapped in a <span class="line">. Use that to prepend a $ with a ::before pseudo-element in your CSS. Something like
I just wanted to suggest CSS content. The problem you will run into will be that if you have multiline bash scripts that are separated with \ and a new line you will still get the full content before the line. You could work around that by supplying the n-th numbers of the lines with the content in your shortcode (like first line, fifth line) and then use something along the lines of :first-child or :nth-child(5).
There are highlighting options that will result in rendering a list, which might make this all a bit easier.
In the CSS, you have no way to recognize continuation lines, as you can’t test for the content of the span. And it seems that Chroma has no token for the continuation character.
What is the fifth-line here – the fifth continuation line? And how would you tell the CSS that in this code block it should not prepend a $ to some lines? I don’t see a way short of using inline styles, which is horrible.
should format the first line red, all others however they are formatted. Using this you can add a content: "$myprompt";::before that line.
Using line:nth-child(2) instead will do the same with the second line. Increase the numbers and you know where it goes.
Use nth-child(2n+1) and it will do something to all odd lines… 2n catches the even lines.
and so on.
Creating a shortcode like this:
{{< shortcode fulllines="1,5" >}}
It could then be transformed into a CSS section with .line:nth-child(1), .line:nth-child(5) with a “full bash prompt” content. All other lines receive only the particle that you want to show in continued lines.
PS: By the way I am not saying that I think it’s a good idea to bring the bash prompt into the highlighting. The fact alone that this is solvable with CSS shows us that it’s a design issue, not a code presentation issue or something like that. What will happen with Python code, with CSS, with other code samples? Just an opinion, but I keep that out of my site and have a clear indicator what code or language we are highlighting per highlighting section. This seems more usable.