Non-verbatim fenced code blocks - with Markdown formatting #9431
Replies: 4 comments 2 replies
-
You could use pandoc.read to parse the text contents of your code block as markdown, then pandoc.write to convert it to HTML, and include that in the raw block with surround pre tags... |
Beta Was this translation helpful? Give feedback.
-
@jgm can I preserve the identation somehow? Tried this:
It loses the indentation:
And another problem is with extra spaces between words... Even if I add |
Beta Was this translation helpful? Give feedback.
-
You might try just using a line block (look up in manual). This will preserve indentation and spacing and allow markdown formatting. Might be what you want. |
Beta Was this translation helpful? Give feedback.
-
For the reference - this LUA filter eventually did the job for me, so it seems: function CodeBlock(elem)
local escaped_spaces = elem.text:gsub(' ', '\\ ')
local md = pandoc.read(escaped_spaces)
local html = "<pre><code>"..pandoc.write(md, 'html', pandoc.WriterOptions {prefer_ascii=true}).."</code></pre>"
html = html:gsub(' ', ' ')
return pandoc.RawBlock('html', html)
end The idea is to escape the spaces, so that pandoc.read() doesn't get rid of them when parsing the text as markdown. This preserves all the indentation and spacing in the AST returned by read(). End-result: <pre><code>This is some <em>italics</em> and some extra spaces between words
This is indentation with some <b>bold</b> text
This is some <span class="green">custom style added by the user</span></code></pre> This is displayed properly (with all the formatting, indentation and spacing!). |
Beta Was this translation helpful? Give feedback.
-
Hi,
In Pandoc Markdown, fenced code blocks are treated verbatim. Hence, all the Markdown syntax is ignored.
This means that we cannot do any Markdown-style formatting inside fenced code blocks.
For example:
None of the above will work, because the text inside is not treated as Markdown, so it will just appear verbatim.
But what if I do want it to be treated as Markdown?
I came up with a sort-of solution, tailored to my needs - since I am rendering into HTML.
So I can use a RawBlock to treat the text inside the fenced code block directly as HTML.
This LUA filter does the trick:
Unfortunately, this still doesn't let me apply the styling using Markdown syntax. I have to use HTML syntax:
This works, but I hate using the target format (HTML) in my markup files.
Wouldn't it be a good enhancement request to support the non-verbatim fenced code blocks?
I think it is somewhat similar to the idea of parsed raw blocks (#7753), just in this case the desired Format is "markdown".
Or maybe I am missing a simpler solution to my problem?
Beta Was this translation helpful? Give feedback.
All reactions