I want to use code blocks in markdown and enable code highlighting. At the same time, I often want to override the formatting of certain parts of the block to pay attention to it.
For example, I have this tag:
```bash var="Hello World" # print it echo "$var" # Another way of printing it printf "%s\n" "$var" ```
This gives me this highlight:
var="Hello World" # print it echo "$var" # Another way of printing it printf "%s\n" "$var"
Now, let's say I want to focus on %s
. I want it to be bold (or red, or some other background color, it doesn't matter).
But I don't see this possibility. There are many similar questions, such as this one. But my problem is not that the highlight is lost. I want to keep it.
I'd even be happy to see any hack, maybe something like this:
<pre> <code class="language-bash">var="Hello World" # print it echo "$var" # Another way of printing it printf "</code></pre>**%s**<pre><code class="language-bash">\n" "$var" </code></pre>
But unfortunately it doesn't work.
Perhaps there is a way to apply highlighting to random pieces of text? I mean, there is no need to put it in a block as that will prevent me from writing further on the same line (i.e. %s
is on the next line).
Perhaps markdown should be extended to support rewriting like this:
```bash var="Hello World" # print it echo "$var" # Another way of printing it printf "```**%s**```\n" "$var" ```
Currently it is just ignored:
var="Hello World" # print it echo "$var" # Another way of printing it printf "```**%s**```\n" "$var"
In Markdown, code blocks are typically used to display code snippets without any other formatting. Syntax highlighting is automatically applied based on the language specified when code fence is turned on (e.g. "``bash"). Markdown itself does not provide a way to selectively override formatting within a block of code while retaining syntax highlighting.
If you need to highlight a specific part of a code block without losing syntax highlighting, you can use a combination of inline HTML and CSS. Here is an example:
var="Hello World" # print it echo "$var" # Another way of printing it printf "%s\n" "$var"
In this example, we wrap the
%s
part inside aelement and apply the CSS style
font-weight:bold;
so that It becomes bold. You can modify the CSS style as needed, such as changing the color or background.Note that the effectiveness of this approach depends on the rendering engine used to display Markdown, as different platforms may handle HTML and CSS differently. It's a good practice to test your Markdown on the specific platform or tool you're using to ensure the desired formatting is preserved.