I'd like to be able to put markdown items within a <div class=...> container, or just to tag them with custom classes.
To achieve that, I'm trying to update mdbook-classy to mdbook 0.5.4, but I know little about how mdbook works internally. I've succeeded to make it compile and work again, except for the <div class=...> added by this preprocessor around the element that follows.
For example,
{:.red big}
You can write **Markdown** paragraphs
should produce this (with or without the paragraph tags):
<div class="red big">
<p>You can write <strong>Markdown</strong> paragraphs</p>
</div>
(links to the code at the end of the post)
Below are two attempts that failed:
(1) If I keep the code's original idea, inserting an Event::Html before and another one after, respectively for <div> and </div>, I get this HTML at the end: (plus warnings about unclosed div tags)
<p>to get this:
<div class="red big"></div>
</p>
<p>You can write <strong>Markdown</strong> paragraphs</p>
even though the modified sequence of events seems fine before they're sent to pulldown_cmark_to_cmark::cmark(...):
Start(Paragraph)
Text(Borrowed("to get this:"))
End(Paragraph)
Html(Boxed("<div class=\"red big\">\n"))
Start(Paragraph)
Text(Borrowed("You can write "))
Start(Strong)
Text(Borrowed("Markdown"))
End(Strong)
Text(Borrowed(" paragraphs with red text anytime you want."))
End(Paragraph)
Html(Borrowed("</div>\n"))
(2) If I enclose the divs between Tag::HtmlBlocks (commented out in the fork) or Tag::Paragraphs, the div tags surround the text correctly, but that text isn't evaluated by mdbook any more[1], as if it was a code block: (this is with the extra Paragraph tag)
<p>to get this:</p>
<div class="red big">
You can write **Markdown** paragraphs with red text anytime you want.</div>
(you can see the strong tags not being applied around "Markdown" on the last line)
even though the sequence looks fine, too:
Start(Paragraph)
Text(Borrowed("to get this:"))
End(Paragraph)
Start(Paragraph)
Html(Boxed("<div class=\"red big\">\n"))
Start(Paragraph)
Text(Borrowed("You can write "))
Start(Strong)
Text(Borrowed("Markdown"))
End(Strong)
Text(Borrowed(" paragraphs with red text anytime you want."))
End(Paragraph)
Html(Borrowed("</div>\n"))
End(Paragraph)
Actually, it's possible to achieve the same result as above with the standard mdbook by enclosing items between <div> and </div>. And that's what the preprocessor above outputs, in fact, so it doesn't seem to be the correct way to preprocess the markdown.
In conclusion, I don't see any other way to enclose items in a container or to "mark" them with class names. Is it possible at all?
EDIT: I'm starting to think it requires a combination of preprocessor for the custom syntax and backend to transform something into the final <div class=...> ... </div>, without mdbook knowing the items in-between are in a div container. Though I don't know if it's possible to append that after the default renderer (e.g. transforming HTML comments to div tags)...
Links:
EDIT: which is apparently in line with the Markdown specs ↩︎