114

I am using MarkEd which implements GitHub flavoured markdown.

I have some working markdown:

## Test heading
a paragraph.
## second heading
another paragraph

Which creates:

<h2 id="test-heading">Test heading</h2>
<p>a paragraph.</p>
<h2 id="second-heading">second heading</h2>
<p>another paragraph</p>

I would like to wrap that markdown section in a div, eg:

<div class="blog-post">
## Test heading
a paragraph.
## second heading
another paragraph
</div>

However this returns the following HTML:

<div class="blog-post">
## Test heading
a paragraph.
## second heading
another paragraph
</div>

Eg, no markdown, literally '## Test heading' appears in the HTML.

How can I properly wrap my markdown in a div?

I have found the following workaround, however it is ugly and not an actual fix:

<div class="blog-post">
<div></div>

## Test heading
a paragraph.
## second heading
another paragraph

</div>
mikemaccana
  • 110,530
  • 99
  • 389
  • 494
  • 1
    You might wanna check out [header-sections](https://www.npmjs.com/package/markdown-it-header-sections) which wraps content in sections, based on the headline. – arve0 Nov 23 '16 at 22:55

6 Answers6

113

Markdown

For Markdown, This is by design. From the Inline HTML section of the Markdown reference:

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.

But it is explicitly allowed for span-level tags:

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

So, depending on your use-case, you might get away with using a span instead of a div.

CommonMark

If the library you use implements CommonMark, you are lucky. Example 108 and 109 of the spec show that if you keep an empty line in between the HTML block and the markdown code, the contents will be parsed as Markdown:

<div>

*Emphasized* text.

</div>

should work, while the following shouldn't:

<div>
*Emphasized* text.
</div>

And, again according to the same section in the reference, some implementations recognize an additional markdown=1 attribute on the HTML tag to enable parsing of Markdown inside it.

Though it doesn't seem to work in StackOverflow yet:

Testing **Markdown** inside a red-background div.
LucasB
  • 3,253
  • 1
  • 28
  • 31
  • 3
    I was trying to do the same with markdown library in python (used in pelican), and the
    trick worked. Thanks a lot.
    – bendtherules Jun 03 '15 at 03:51
  • For the record: SO intentionally blocks divs, scripts, and other inline elements. Changing color isn't possible at all (with the exception of MathJax, but that's not enabled here) – Zoe Sep 21 '19 at 15:38
  • @lucas smart, here is your live tested code including empty lines and md=1: `
    Testing **Markdown** inside a red-background div.
    `
    – Timo Aug 14 '20 at 07:54
  • ``markdown=1`` on a parent element made it possible to parse markdown content inside HTML tags inserted through erb in a template with ``.html.md.erb`` extension with default markdown engine (not which one is used, but it works). – Rafal Sep 02 '21 at 11:25
  • Your solution for CommonMark works under Jupyter Notebook too. – Aristide May 17 '22 at 15:10
  • any way to avoid the extra `

    ` tag in commonmark because of the newlines?

    – santiago arizti Aug 10 '22 at 00:15
40

GitHub Pages supports the markdown="1" attribute to parse markdown inside HTML elements, e.g.

<div class="tip" markdown="1">Have **fun!**</div>

Note: As of 2019/03, this doesn't work on github.com, only GitHub Pages.

Note: Quotes, as in markdown="1", are not required by HTML5 but if you don't use quotes (markdown=1), GitHub does not recognize it as HTML. Also, support is buggy right now. You will likely get incorrect output if your HTML element is larger than a single paragraph. For example, due to bugs I was unable to embed a Markdown list inside a div.

If you find yourself in an environment in which markdown="1" doesn't work but span does, another option is to use <span style="display:block"> so that block-level classes are compatible with it, e.g.

<span style="display:block" class="note">It **works!**</span>

Tip: <span class="note"></span> is shorter than <div class="note" markdown="1"></div>, so if you control the CSS you might prefer to use <span> and add display: block; to your CSS.

Qwertie
  • 16,354
  • 20
  • 105
  • 148
  • 1
    I tested the `markdown="1"` on github. it does not work. [link](https://gist.github.com/Foadsf/0c28f7cd83ccfabaa7a27386bb4a87ea) – Foad S. Farimani Mar 13 '19 at 22:22
  • 1
    the `span` solution works perfectly find on GitHub. [link](https://gist.github.com/Foadsf/0c28f7cd83ccfabaa7a27386bb4a87ea#gistcomment-2861789) Thanks a lot. – Foad S. Farimani Mar 13 '19 at 22:34
  • upon further investigation, Github and Typora can't render this properly but visual studio code does! – Foad S. Farimani Mar 14 '19 at 00:25
  • @Foad it works on GitHub Pages, but not on GitHub itself, e.g. witness the text ``**Tip:**`` appearing on [this page](https://github.com/qwertie/learn-react/blob/master/docs/tutorial-2.md), which appears as **Tip:** on the [GHP rendering of the same file](http://typescript-react-primer.loyc.net/tutorial-2.html). I'll clarify the text. – Qwertie Mar 18 '19 at 03:44
  • 1
    I also found (on GitHub Pages) that `markdown="1"` must be the last attribute in the tag. For example, `
    Have **fun!**
    ` will *not* work.
    – Gabriel Luci Aug 04 '20 at 19:32
11

Markdown Extra is needed to be able to for Markdown formatting works inside an HTML blocks, please check the documentation stated here -> https://michelf.ca/projects/php-markdown/extra/

Markdown Extra gives you a way to put Markdown-formatted text inside any block-level tag. You do this by adding a markdown attribute to the tag with the value 1 — which gives markdown="1"

4

Last resort option:

Some libraries may be case sensitive.

Try <DIV> instead of <div> and see what happens.

Markdownsharp has this characteristic - although on StackOverflow they strip out all DIVs anyway so don't expect it to work here.

Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
  • I would delete this if it weren't true, but for me this was part of my solution - as strange as it may sound – Simon_Weaver Feb 08 '17 at 09:50
  • 2
    Sounds like you're just tricking a buggy markdown parser implementation (case sensitive), which they may patch in the future. – StrangeWill Aug 14 '17 at 23:51
  • 6
    That's exactly what I did – Simon_Weaver Aug 14 '17 at 23:57
  • 1
    This is the only one that worked with hugo, thanks for the tip and let's hope it will never be patched !!!! (to be hones the span tricks kind of works, but I need to use div to give it special css class that do not work with span) – Julien Colomb Jan 16 '20 at 15:03
1

By looking at the docs for Extending Marked and modifying the html renderer method, you can do something like this to replace the parts between tags with parsed markdown. I haven't done extensive testing, but it worked with my first few attempts.

const marked = require('marked');
const renderer = new marked.Renderer();
renderer.html = (mixedContent) => mixedContent.replace(/[^<>]+?(?=<)/g, (match) => {
    const tokens = marked.lexer(match);
    return marked.parser(tokens);
});

Edit

this new regex will ensure that only markdown with lines between it and the html tags will be parsed.

const marked = require('marked');
const renderer = new marked.Renderer();
renderer.html = (mixedContent) => mixedContent.replace(/\n\n[^<>]+?\n\n(?=<)/g, (match) => {
    const tokens = marked.lexer(match);
    return marked.parser(tokens);
});
RyanDay
  • 1,856
  • 16
  • 23
1

In my case (on GitHub), the problem was resolved when I added newline between html tags and markdown text.

enter image description here

BKO
  • 43
  • 1
  • 8
  • 1
    Just paste the code, don't use a screenshot. This lets others copy and paste. Thanks! – mikemaccana Jan 19 '23 at 10:43
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 20 '23 at 07:49