I’m trying to convert a Markdown file containing a simple bar chart created with inline HTML and CSS to an HTML file using Pandoc. The Markdown preview in Visual Studio Code displays the chart perfectly fine, but the output from Pandoc is incorrect, with the chart elements not being rendered properly.
Here is the Markdown content I’m using:
# Bar Chart Example
<div style="width: 300px; height: 200px; border: 1px solid #ccc; padding: 20px; display: flex; align-items: flex-end; justify-content: space-around;">
<div style="width: 40px; background-color: #3498db; margin: 0 5px; height: 60%;">
<div style="text-align: center; font-family: Arial, sans-serif; font-size: 12px; padding-top: 5px;">A</div>
</div>
<div style="width: 40px; background-color: #3498db; margin: 0 5px; height: 80%;">
<div style="text-align: center; font-family: Arial, sans-serif; font-size: 12px; padding-top: 5px;">B</div>
</div>
<div style="width: 40px; background-color: #3498db; margin: 0 5px; height: 40%;">
<div style="text-align: center; font-family: Arial, sans-serif; font-size: 12px; padding-top: 5px;">C</div>
</div>
<div style="width: 40px; background-color: #3498db; margin: 0 5px; height: 100%;">
<div style="text-align: center; font-family: Arial, sans-serif; font-size: 12px; padding-top: 5px;">D</div>
</div>
<div style="width: 40px; background-color: #3498db; margin: 0 5px; height: 70%;">
<div style="text-align: center; font-family: Arial, sans-serif; font-size: 12px; padding-top: 5px;">E</div>
</div>
</div>
This is a simple bar chart created using HTML and inline CSS, embedded in Markdown.
To convert it to HTML, I’m using the following Pandoc command:
pandoc -o output.html bar_chart.md
The resulting HTML file doesn’t render the chart as expected. Here’s a screenshot comparison between the Markdown preview in Visual Studio Code (which is correct) and the HTML output from Pandoc:
Visual Studio Code (Correct):
Pandoc HTML Output (Incorrect):
Does anyone know why Pandoc might be rendering the inline HTML incorrectly, and how I can fix this?
2
Answers
I found a partial solution to the problem by switching from inline CSS to an external CSS file. Pandoc seems to handle external stylesheets better, which resolves the rendering issue in the HTML output.
Here’s how I updated the Markdown file:
And here’s the content of the
styles.css
file:With this setup, running the command:
produces the correct HTML output where the bar chart is rendered as expected.
The downside, however, is that Visual Studio Code’s Markdown preview can no longer render the chart since it doesn’t automatically load external CSS files.
This approach works if you prioritize the final HTML output over the Markdown preview in VS Code. If anyone has a way to make this work both in Pandoc and VS Code, I’d love to hear it!
P.S.1. I was able to sort the Visual Studio Code issue out. Create a
.vscode
folder in the same place as the root of the project, and create asettings.json
file in that folder with the content:P.S.2. I just discovered Charts.css and it seems amazing.
First, let us look at why the pandoc output looks different:
Pandoc supports various Markdown flavors and features, with one of them being
markdown_in_html_blocks
. That extension is enabled by default and allows to use Markdown syntax even in content nested in HTML blocks.As you probably know, Markdown syntax requires lines indented by four spaces (or more) to be parsed as part of a code block.
And know we can see what’s happening: the innermost div is indented by four spaces, so pandoc parses it as a Markdown code block.
There are three solutions:
Re-indent the HTML to ensure that there are never more than three spaces at the start of the line.
Disable the
markdown_in_html_blocks
extension by calling pandoc with the--from=markdown-markdown_in_html_blocks
parameter.Use the "raw attribute" syntax to explicitly mark the diagram as HTML:
VS Code preview might not support the raw attribute syntax, so the first two options are probably preferable in your case.