0

Question title should be self explanatory. I couldn't find anything in Markdown of Sphinx raw.

I know I can type directly html within the md file, but I have a very long html code snippet that I'd like to keep separated from the markdown.

matteo
  • 3,681
  • 7
  • 31
  • 61
  • You showed no effort to read the documentation in your question: https://daringfireball.net/projects/markdown/syntax#html or https://daringfireball.net/projects/markdown/syntax#precode Please do so before asking a question on SO. – Steve Piercy Feb 14 '19 at 17:49
  • Hi @Steve. I already read those links, but there is no explanation of *calling* html code of another file in the markdown file, only to write raw html but in the file itself. – matteo Feb 15 '19 at 07:12
  • You could use an iframe, but that has issues. See also https://stackoverflow.com/questions/4779582/markdown-and-including-multiple-files – Steve Piercy Feb 15 '19 at 12:12
  • Possible duplicate of [Markdown and including multiple files](https://stackoverflow.com/questions/4779582/markdown-and-including-multiple-files) – Steve Piercy Feb 15 '19 at 12:12

1 Answers1

2

As the rules plainly state (emphasis added):

For any markup that is not covered by Markdown’s syntax, you simply use HTML itself. There’s no need to preface it or delimit it to indicate that you’re switching from Markdown to HTML; you just use the tags.

The only restrictions are that block-level HTML elements — e.g. <div>, <table>, <pre>, <p>, etc. — must be separated from surrounding content by blank lines, and the start and end tags of the block should not be indented with tabs or spaces. Markdown is smart enough not to add extra (unwanted) <p> tags around HTML block-level tags.

If you are concerned that Markdown might munge up the content of your raw HTML, the rules also state:

Note that Markdown formatting syntax is not processed within block-level HTML tags. E.g., you can’t use Markdown-style *emphasis* inside an HTML block.

So, as long as your HTML blocks are properly wrapped in block level HTML tags, Markdown will leave them alone. Of course, that does not aply to inline HTML elements, included within Markdown text:

Span-level HTML tags — e.g. <span>, <cite>, or <del> — can be used anywhere in a Markdown paragraph, list item, or header. If you want, you can even use HTML tags instead of Markdown formatting; e.g. if you’d prefer to use HTML <a> or <img> tags instead of Markdown’s link or image syntax, go right ahead.

Unlike block-level HTML tags, Markdown syntax is processed within span-level tags.

It is also important to note that some Markdown implementations do not follow the rules as stated above. So you might want to check the documentation for the implementation you are using to ensure it either follows the rules, or has a configuration switch which forces it to follow the rules.

And then there is Commonmark, which does not follow the original Markdown rules, but uses a different set of rules. Specifically, Commonmark requires no blank lines within the raw HTML block to avoid Markdown parsing. CommonMark will treat everything as Markdown after the first blank line within a raw HTML block. However, if you are using an old-school Markdown parser rather than a Commonmark parser, this should be a non-issue.

As a final word of caution, Markdown (not Commonmark) is much older than HTML 5. Therefore the newer block-level elements added to HTML 5 (<section>, <article>, etc.) are not recognized by most Markdown implementations as being block level elements. Some implementations have been updated to add support, but even then there is no consistency between implementations regarding which new elements should be treated as block-level as the reference implementation has not been updated to set a president. Therefore, it is best to stick with elements defined as block-level in the old HTML 4 and/or XHTML 1 specs.

Waylan
  • 25,184
  • 8
  • 62
  • 93
  • HI @Waylan, thanks for the comment. So you are saying that I can paste the code directly in the markdown file (as I mention in the question) but there is no way to embed the raw html code of another file? – matteo Feb 15 '19 at 07:11
  • @matteo I just reread you question. I don't see anything there about "including files," which is why my answer doesn't address that. I answered the question you asked. If your question is about including content from other files, then I have to agree that it is a duplicate of [Markdown and including multiple files](https://stackoverflow.com/questions/4779582/markdown-and-including-multiple-files). The answers there cover a number of different solutions. – Waylan Feb 15 '19 at 13:52