Embed indented HTML in Markdown with Pandoc
Asked Answered
D

2

10

I have some embedded HTML in my Markdown (bulleted list within a table). Is there a way to indent my HTML without Pandoc treating it as a verbatim code block?

Deodorize answered 30/8, 2016 at 6:37 Comment(0)
S
17

Sort of, but you have to change Pandoc's defaults.

The Markdown rules expressly prohibit this:

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.

However, if you notice, the above quoted rule does specifically state that only "the start and end tags of the block should not be indented." There is no restriction from indenting the content inside the "the start and end tags". In fact, the content between ""the start and end tags" is not even processed as Markdown, so feel free to indent away. In other words, this is completely acceptable:

<table>
    <thead>
        <tr>
            <th>A header</th>
        </tr>
    </thead>
</table>

Except that it doesn't work in Pandoc by default. As explained in Pandoc's documentation:

Standard Markdown allows you to include HTML “blocks”: blocks of HTML between balanced tags that are separated from the surrounding text with blank lines, and start and end at the left margin. Within these blocks, everything is interpreted as HTML, not Markdown; so (for example), * does not signify emphasis.

Pandoc behaves this way when the markdown_strict format is used; but by default, pandoc interprets material between HTML block tags as Markdown.

Therefore you either need to use the raw_html extension or the markdown_strict output format.

For "strict mode" use:

pandoc --from markdown_strict

Or to not use strict mode but still get the HTML behavior you want (disable markdown_in_html_blocks extension and enable raw_html extension):

pandoc --from markdown-markdown_in_html_blocks+raw_html
Sainted answered 30/8, 2016 at 13:47 Comment(1)
So does this mean pandoc by default apply "markdown_in_html_blocks" to transform the content wrapped in <table>{{content}}</table>?Hamann
Z
0

raw_html does not guarantee table output in HTML format.

I created a PR to pandoc, and tried to guarantee HTML table when raw_html output option is presented, it was been denied by maintainer. Howerver you can check my PR, do the code change and build a local pandoc binary.

Basically you need to move the following code to top of switch cases:

| isEnabled Ext_raw_html opts -> fmap (id,) $
   literal <$>
   writeHtml5String opts{ writerTemplate = Nothing } (Pandoc nullMeta [t])
Zeppelin answered 30/8, 2022 at 6:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.