20

Why is "DRSI" displaying in italics in this SO answer?

I expect markdown to be disabled in code blocks, because some code happens to conform to certain markdown patterns.

Interestingly, on SO the preview does not display is italics, but rendered version does.
Here, on the other hand it renders OK.

Here's a copy of the code block:

FOR PURPOSES.*?DRSI.*?\.
9
  • 1
    Because highlight.js marked the text as emphasis. This often happens when highlight.js has to guess at the source language.
    – Martijn Pieters Mod
    Commented Sep 29, 2020 at 0:03
  • 3
    Fixed by setting the code block language to none
    – Martijn Pieters Mod
    Commented Sep 29, 2020 at 0:05
  • Is it a bug? It doesn't really matter that much since the code can still be correctly highlighted, copied and pasted.
    – Bohemian Mod
    Commented Sep 29, 2020 at 0:05
  • The preview will also show the issue of you let syntax highlighting catch up. It is not being parsed as markdown, it’s parsed as code but the highlighter has support for markdown-like constructs when in auto-mode.
    – Martijn Pieters Mod
    Commented Sep 29, 2020 at 0:06
  • 3
    So we except our users to do this? Hmmm. I'm leaving this open to see what kind of voting it attracts. Happy for it to be nuked too. Just reporting it really.
    – Bohemian Mod
    Commented Sep 29, 2020 at 0:07
  • 2
    Wow. That's quite a rendering lag. About 5+ seconds. Cool.
    – Bohemian Mod
    Commented Sep 29, 2020 at 0:09
  • It amazes me that you haven't noticed the preview render lag before this point ^^", it's been there for... Quite some time Commented Sep 29, 2020 at 0:10
  • 1
    @Nick Be amazed. I've never noticed it. I don't look at preview that often - mainly just to the code indentation right.
    – Bohemian Mod
    Commented Sep 29, 2020 at 0:16
  • 1
    If you're annoyed at the 5-second delay before syntax highlighting kicks in like I was, you can use my userscript Commented Sep 29, 2020 at 0:48

1 Answer 1

15

This is the syntax highlighter trying to guess at a language for the regex. highlight.js has 3 language definitions that use an emphasis class, rendered as italic text.

Since there is very little text and there is no regex language definition for highlight.js, the highlighter ended up picking the markdown language definition and marked the part between asterisks with the emphasis class. Highlight.js uses a reasonably smart algorithm to pick languages but because the text is so short it had very little to work with.

I’ve forced the issue by disabling highlighting for that code block by setting the language to none, like this:

```none
<lines incorrectly highlighted>
```

Note that the issue definitely is visible in the editor preview too, you just need to make sure you let the highlighter kick in (there is a few second delay after you stop editing).

6
  • 1
    Should perhaps the site be updated to automatically supply or guess none when no explicit language tag is present? I'm getting this on a regex question (probably deleted by the time you read this, but here.)
    – tripleee
    Commented Sep 30, 2020 at 10:05
  • 1
    @tripleee: we can set tags to have no highlighting, but I don't think we want to disable guessing altogether (which is the default for most tags). I don't recall what happens when you use, say, [regex] [python] if regex had highlighting set to none, but if that Just Works (code blocks default to being marked up as Python) then perhaps we should set regex to none by default.
    – Martijn Pieters Mod
    Commented Sep 30, 2020 at 10:28
  • 4
    @tripleee [Current maintainer of Highlight.js here] I've made the suggestion elsewhere that really short snippets should be defaulted to none (or use a heuristic) because guessing with so little data isn't going to produce great results. Commented Feb 15, 2021 at 3:00
  • I went looking for it so I could upvote it, but I guess it's just the last paragraph in a broader discussion here?
    – tripleee
    Commented Feb 15, 2021 at 5:53
  • @tripleee see meta.stackexchange.com/questions/355852/…
    – Martijn Pieters Mod
    Commented Feb 15, 2021 at 9:06
  • Yeh, I found (and upvoted) that too, but it doesn't specifically discuss the case of short snippets.
    – tripleee
    Commented Feb 15, 2021 at 9:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .