4

For many years, it's been possible to apply rich text formatting to code blocks inside posts by formatting the code block using the HTML tags <pre><code> and using more HTML tags inside the post.

As an example, the following code:

<pre><code>.
This is <b>bold</b>, <i>italic</i>, and <strike>striked-out</strike> text.
.</code></pre>

results in:

.
This is bold, italic, and striked-out text.
.

Today, this functionality continues to work so long as the block is not being syntax-highlighted. In the past, however, this used to work for all posts, since it was supported by SE's previous highlighter Prettify and by the original version of the new Highlight.js highlighter to be deployed here, version 10. However, this feature was removed in version 11. The same code, when highlighted as default, has said formatting stripped out:

.
This is bold, italic, and striked-out text.
.

When looking at the post source, both of the above two code blocks have the identical source, the one in the first block above: the only difference is in the HTML comments above each in the source: the former has highlighting disabled, while the latter has it enabled.

The stated reason for the removal of support in version 11 is to prevent against inadvertent HTML injection if a user puts in HTML code into an HTML code block. However, a plugin exists and can be installed by Stack Exchange to restore the prior functionality. Installing this plugin, as far as I can tell, requires no more overhead than adding support for an additional highlighting language already supported by Highlight.js but not by SE.

Querying for affected posts is difficult due to requiring a regex and using regex to parse HTML* go through the site's database is difficult due to SO's massive database size, so I'd like to start a discussion to gauge from the community as to how important they perceive having this feature. There has been at least one complaint on the global meta regarding answers being broken as a result of this change to Highlight.js, but I'd like the overall community consensus.

Should the plug-in be implemented, or should the current configuration be retained?

Reasons to install it

  • It un-breaks older posts which depended on the feature without needing to edit them.
  • It prevents unexpected formatting removals when users type posts (users wondering where their formatting went/why it vanished despite it working in the 5-second window when highlighting is turned off in the post editor).
  • It removes the requirement to turn off syntax highlighting in revision histories and suggested edit reviews (the highlighter was swallowing the strikethrough formatting), and as such makes it easier to detect inappropriate changes to syntax highlighting language in HTML comments (changes to a post's hidden content aren't shown by default unless a hyperlink is changed).

Reasons to retain the current configuration

  • Text formatting in code blocks is rarely used due to it not being supported in four-space indent or triple-backtick code blocks.
  • A workaround exists for posts where it's important, to disable syntax highlighting for that particular block or post using the HTML comment <!-- language[-all]: lang-none -->. Broken posts which depended on the feature can be edited to fix them using this.
  • It doesn't require dev team effort (which isn't that much given that the plugin already exists; the team just needs to implement it).

* That is, fundamentally, what a regex to search for affected posts would be doing anyway.

16
  • I agree with this, it could be useful to provide emphasis to especially important lines in code blocks. I have never done this myself, but only because I didn't know it was possible. My only concern is the removal reason, but I can't think of why HTML injection is a concern with highlighting on vs. highlighting off. I'd think it would be relevant in either case.
    – codewario
    Apr 21, 2022 at 21:18
  • 5
    About as important as the bumps on a pickle. If your associated text cannot or does not adequately describe the important bits of the code block, then you've already failed to write a good answer.
    – Cody Gray Mod
    Apr 22, 2022 at 3:08
  • Do you have some actual examples where this is useful? Most (all?) languages have ways to draw attention (read: comments, whitespace, ...); code highlighting itself already does so as well. Needing to draw even more attention suggests huge, convoluted or otherwise unreadable code – that's not something that should be helped with, IMO. Apr 22, 2022 at 7:58
  • 1
    @MisterMiyagi I can see it being useful (but not a necessity) to provide emphasis in code blocks where the minimal example is rather large or contains boilerplate important for reproducing or fixing a problem, but is not the crux of the question or answer. That said, most of the time I'd agree it's probably unnecessary and generally better left to commentary on the code itself. It could also be useful for drawing attention to specific areas of general monospaced text, but the workaround of disabling highlighting for the block seems to work for this case.
    – codewario
    Apr 22, 2022 at 8:31
  • 5
    And (automatic) line numbers in code while we are at it? Apr 22, 2022 at 8:36
  • 2
    The option to have line numbers I can see being useful; you can then make a statement like "The important part of this solution is on line 6 of the below where {some explanation}." It's also an optional feature in many "prettifying" tool. Bolding/italic not so much.
    – Thom A
    Apr 22, 2022 at 8:38
  • 1
    I'll agree I would much rather have automatic line numbers than this feature.
    – codewario
    Apr 22, 2022 at 8:41
  • 2
    @MisterMiyagi The complaint on the global meta I linked to links to an example post: stackoverflow.com/a/19849537/2157772
    – gparyani
    Apr 22, 2022 at 8:53
  • Personally I like HTML5's <mark> tag and I'd have some sort of gold background color for the mark... Bold and italic are easily lost as (as well as being used by some highlighting themes as well, like Markdown). If this wasn't a 5kb chunk of code that most users get no value from (plus the security concerns) we'd likely support it in core but ONLY for a single "blessed" tag, like mark. As for whether SE needs this feature, I'm on the fence, but if it was added to SE I'd suggest spending a few minutes adding a tag whitelist to the plugin, which should be easy to do. Apr 22, 2022 at 12:31
  • In the revisions page, the formatting seems completely fine
    – MrMythical
    Apr 22, 2022 at 13:37
  • 2
    @MrMythical That's because highlighting is disabled in revision histories, as it was swallowing the strikethrough formatting. I mention this in the third bullet in Reasons to install it.
    – gparyani
    Apr 22, 2022 at 15:45
  • 1
    @CodyGray: If your associated text cannot or does not adequately describe the important bits of the code block, then you've already failed to write a good answer. Over the last decade, I have written hundreds of answers with formatted code. Mostly <b> and <strike>. It serves well to highlight crucial bits or errors. Then you (SO) switched to Highlight.js without due diligence and broke existing answers, only to blame the otherwise excellent highlighter for your failure. Stripping <strike> completely breaks some answers. Your presumptuous comment just adds insult to injury. May 5, 2022 at 1:55
  • 4
    @ErwinBrandstetter without due diligence...nope, that's absolutely wrong. The previous highlighter, Prettify, had been retired and discontinued by its maintainer Google, so it was important for them to switch to another highlighter that was actively supported. Also, version 10 of the highlighter, which was the one originally deployed on the sites, did support having HTML formatting inside code blocks, so you can't blame them for deliberately choosing one that doesn't support it since it did at the time of the switch.
    – gparyani
    May 5, 2022 at 2:27
  • @gparyani: OK, I don't have insight in the details of the process. And I don't blame them for choosing Prettify. But they changed behaviour and broke existing answers, which could have been handled better. (Like, if there is a <strike> tag in an existing code block, rather disable code highlighting than HTML tags. Or better yet, enable HTML tags (at least for pre-existing posts), which is totally possible as I have learned from Josh's comments. Instead of trying to fix it - or at least admitting fault - I see presumptuous comments like the above. May 5, 2022 at 2:42
  • 4
    The irony of someone who abuses inline code formatting for long sequences of text that are obviously not code preaching to me about the superiority of their use of formatting is not lost on me, @Erwin. My aim is not and will never be to defend careless actions taken by the company who runs this site, but it remains true that formatting alone should not stand as the sole indicator of content and that anything you wish to highlight should be explicitly mentioned in the surrounding text.
    – Cody Gray Mod
    May 5, 2022 at 6:35

0

You must log in to answer this question.