13

I have been using code fences to highlight my code in SO

```javascript

```

Recently I have noted that my code is being edited and the below syntax is being used

<!-- lang-js -->

I have seen code fences being used in several platforms to highlight code. Is there a reason for changing from code fences to the other syntax?

The post How do I format my code blocks? provides an explanation of how to format your code, unfortunately what I am looking for is how the two syntax highlighting differ, that is why edit to change from one syntax highlighting to the other

15
  • 7
    Aside from the post you linked yesterday, were there more instances of this happening? There is no practical reason to change from one code formatting style to the other. And if that's the only thing being edited in the post, it's a pointless edit.
    – yivi
    Commented Oct 15, 2020 at 12:53
  • 1
    For example, in this question, the change was not only in code formatting style, but they actually added the language so the proper highlighting was applied. In the original revision, syntax-highlighting wasn't used.
    – yivi
    Commented Oct 15, 2020 at 12:56
  • Strongly related (but not a dupe): meta.stackoverflow.com/q/378962/6296561
    – Zoe Mod
    Commented Oct 15, 2020 at 12:57
  • Thanks @Zoe I can see in the post it says Edits like those should be rejected... But How to reject a code edit if it is approved before I even get a chance to have a look at it? Commented Oct 15, 2020 at 12:59
  • 2
    @OwenKelvin You can rollback the edit. But please make sure it's actually worth rolling it back. Check the edit in my other comment, and you'll see the edit actually improved the post (change of code formatting style notwithstanding).
    – yivi
    Commented Oct 15, 2020 at 13:01
  • If you're the OP of the post, you can force reject the edit after the fact, provided you didn't approve it originally of course. In the remaining cases, you can roll it back, but you should mod flag instead. Mods can also force reject after the fact provided there's no revisions after the offending edit, as well as impose an edit ban if necessary.
    – Zoe Mod
    Commented Oct 15, 2020 at 13:03
  • There is no difference between the two code-formatting styles. Use whichever suits you.
    – yivi
    Commented Oct 15, 2020 at 13:04
  • 11
    Ideally <!-- lang-js --> should not be used as that form of language declaration has been depracated, ```javascript should be preferred, although isn't mandated Commented Oct 15, 2020 at 13:06
  • 2
    @yivi that's not technically entirely true. <!-- language: whatever --> is deprecated and will be removed eventually. meta.stackexchange.com/questions/348746/…
    – Zoe Mod
    Commented Oct 15, 2020 at 13:06
  • @Zoe It's technically true in the sense that there is no difference in the resulting output. Yup, it's deprecated. And the OP already uses the preferred style. If for some reason they want to use the older style, it will be automatically converted the day SO decides to stop supporting it (or break thousands and thousands of posts).
    – yivi
    Commented Oct 15, 2020 at 13:08
  • 1
    So basically indented blocks is deprecated and using it faces the risk of breaking the day SO decides to stop supporting it Commented Oct 15, 2020 at 13:18
  • 2
    No, indented blocks are gonna keep working, but at some point in the future, there will be no way to specify what language to use for the block. The only way to set a language is through the automatic detection system, which IIRC only relies on tags
    – Zoe Mod
    Commented Oct 15, 2020 at 14:31
  • 1
    If the tag doesn't provide a cue, then the auto detection system also tries to "read" your code to determine what language it is, and highlights accordingly. The results vary, dramatically.
    – zcoop98
    Commented Oct 15, 2020 at 15:21
  • 1
    Nothing like a bit of progress; looking forward to all those vb.net/sqlserver tagged questions where everything is highlighted as TSQL
    – Caius Jard
    Commented Oct 16, 2020 at 15:59
  • @Nick that looks like an actual answer.
    – SQB
    Commented Oct 16, 2020 at 21:13

2 Answers 2

12

In addition to the deprecation MattDMo pointed out here (and Nick in the comments), one small point:

Your two examples aren't quite equivalent. The precise equivalent of <!-- language: lang-js --> with code fences is ```lang-js (with the lang- prefix). ```js says "use the default formatter for the tag" (because js is an alias for javascript), but <!-- language: lang-js --> tells the site explicitly to format the code as JavaScript, without referring to the tag. It doesn't matter for JavaScript, but it does for some other tags, like .

Details:

The token following the ``` in code fences is a tag identifier unless you have a lang- prefix on it. It tells the site to use the default formatting for that tag, it doesn't refer to a specific language directly. To do that, use the lang- prefix.

It matters for some tags, like , because the default formatting for posts is JavaScript, not TypeScript (at least for now). So if you do this:

```typescript
codeHere();
```

you'll get JavaScript formatting. This gives you TypeScript formatting:

```lang-typescript
codeHere();
```

It can matter, particularly with function overloads. Here's what a ```typescript block looks like on SO (an image because the tag marker doesn't work on MSO):

TypeScript code (mis)formatted as JavaScript

Here's a ```lang-typescript block:

function exampleOverload(a: number): number;
function exampleOverload(a: number[]): number[];
function exampleOverload(a: number | number[]): number | number[] {
    if (Array.isArray(a)) {
        return a.map(v => v * 2);
    }
    return a * 2;
}
4
  • It can matter, particularly with function overloads Maybe you can demonstrate here.
    – TheMaster
    Commented Oct 18, 2020 at 6:11
  • 1
    @TheMaster - I was worried it was going too far into the TypeScript thing. But I've added an example from the linked feature request. Commented Oct 18, 2020 at 7:20
  • 1
    This is... kinda ground-breaking. I've never seen this referenced anywhere on the site before, and if accurate it helps explain some odd behaviors I've noticed. How did you learn this?
    – zcoop98
    Commented Oct 20, 2020 at 17:37
  • 1
    @zcoop98 - It's documented here: "You can use either one of the supported language codes, like lang-cpp or lang-sql, or you can specify a tag, and the syntax highlighting language associated with this tag will be used." I didn't know it either until Martijn Pieters pointed it out to me fairly recently. Commented Oct 20, 2020 at 17:46
8

Visually, there is no difference when the Markdown is rendered into HTML and displayed in your browser. However, as @Nick pointed out in the comments above, the <!-- language: lang-whatever --> directive has been deprecated with the switch to CommonMark and may be subject to removal at some point in the future. So, for forwards compatibility, using code fences is the preferred way now.

It should be noted that edits changing the old language directive to code fences are not desirable, unless significant other improvements have been made to the post as well.

You must log in to answer this question.

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