It is necessary to add to the document on Markdown comments that would not be exported to the final document.

There is a standard html comment:

<!-- comment text --> 

The problem is that when exporting such a comment gets into the final document unchanged. Is it possible to make such comments that will be cut when exporting to HTML?

  • one
    And why is this necessary? - Qwertiy
  • @Qwertiy: for example, I write documentation. Sources in Markdown, the result is collected in PDF or HTML. I do not want end users to see my comments. - Nick Volynkin

1 answer 1

This is implemented using a link label (link label), which is not associated with any link. These link shortcuts will be cut when exporting:

 (пустая строка) [comment]: # (comment text) (пустая строка) [//]: # (comment text) 

Compare with the usual link syntax:

 [link text][label] [label]: http://ru.stackoverflow.com/questions/ask 

In principle, the Markdown specification allows for the option when there is no empty line before the comment, and <> used instead of # . The problem is that the specification is leaky like a sieve, and the existing 28+ implementations work differently. Welcome to Markdown!

The Babelmark2 toolkit, which checks the rendering of Markdown markup in various implementations, will help us check the performance of various implementations. ( + - passed the test, - - did not pass ? - leave non-displayable garbage in HTML).

According to the test, the most platform-independent option is # and the empty line before the comment. An empty line after the comment does not matter. In particular, the increasingly common CommonMark specification , which Jeff Atwood participates in the development, works with this option (and does not work with <> and / or without a blank line)

With these implementations, there is no way to use such comments:

  • cebe / markdown 1.1.0
  • cebe / markdown MarkdownExtra 1.1.0
  • cebe / markdown GFM 1.1.0
  • s9e \ TextFormatter (Fatdown / PHP)

The study is based on the solution proposed by Magnus on SO.EN.