19

Stack Snippets have some strange issues centering around the way the delimiters are parsed. I was going to report an indentation bug, but then I was prevented from posting that by what seems to be an issue with how the delimiters are parsed in general.

The original issue I was going to report, is that the four spaces normally used to demarcate a code block seem to be optional, but will be consumed if included regardless of which line the spaces appear in:

<!-- begin snippet: ... -->

<!-- language: lang-html -->

<div>
    <div>
        <p></p>
    </div>
</div>

<!-- end snippet -->

This will render as:

<div>
<div>
    <p></p>
</div>
</div>

I am not sure if this is intentional, but even if it was, I would at least expect the leading spaces in the middle of the code block to be preserved as code indentation.

The other issue is that I wasn't even able to list the above source without changing the js identifier to ..., thereby invalidating the delimiter. It seems that the parser will treat any valid <!-- begin snippet: ... --> that can be matched with an <!-- end snippet --> as a Stack Snippet, regardless of whether they appear in their own lines, or in another Markdown code block, or, hell, in the middle of text. I can't even write this paragraph without it being disemboweled unless I change the language identifier.

And if the start delimiter appears in a code block and it includes at least one code block in a language that is supported by the snippet editor, different things happen depending on the context:

  • In the live preview, it generates a single code block containing a <pre> element with what appears to be a GUID for the snippet that's generated, as well as the usual "edit the above snippet" link. This GUID changes every time the live preview is updated (e.g. with every keystroke in the editor).

  • In the actual post, it also generates a <pre> element, but the GUID is swapped out for an actual snippet block, within the <pre> element. Hilariously, the leading spaces in the middle of the code block are preserved here, and judging by the HTML source of this post it has nothing to do with the wayward <pre> element.

Here's the example Markdown at the beginning of this post, but with the ... changed back to js:

<div>
    <div>
        <p></p>
    </div>
</div>

A simple solution to this would be to just not parse those delimiters unless they appear in their own lines, as is already done with language hints.

1 Answer 1

11

the four spaces normally used to demarcate a code block seem to be optional...

Yep, between the snippet begin & end tags, every line is treated as code, except for any snippet section tags.

... I would at least expect the leading spaces in the middle of the code block to be preserved as code indentation.

Contrary opinion - I would expect that snippets should conform to the markdown convention, and treat lines without the 4-space indent as regular text, not code. That would mean that the snippet would be broken, but so be it.

If you use the snippet editor, it will insert the leading spaces, so it would seem reasonable to expect a human editor to match that behavior.

IMHO, the bug is that snippets (and only snippets) are including non-indented lines of text as code.

1
  • 1
    That certainly makes more sense. +1
    – BoltClock
    Jun 2, 2015 at 14:14

You must log in to answer this question.

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