Markdown with IDs and classes


(Plevan) #1

Does anyone know if it is possible to use the markdown content type, but specify CSS classes and IDs within the Markdown code?

For example PHP Markdown Extra supports this: https://michelf.ca/projects/php-markdown/extra/#spe-attr


(Luke Wright) #2

We’ve only tested the current version of the Markdown content type with the basic PHP Markdown library, which doesn’t support IDs and classes in the way the Extra version does. I don’t believe it’s possible to substitute the PHP Markdown Extra version of the library, either.

We’re working on support for Parsedown library as an alternative, which will at least give you Github-Flavoured Markdown as an option. We’re hoping this will appear in a future 5.4 version (rather than waiting for 5.5 which is a while away), but we can’t say when at the moment.

Although custom IDs/classes in the form of PHP Markdown Extra aren’t supported, the CommonMark spec - which a lot of parsers (including Parsedown) aim to comply with for interoperability purposes - suggests it may be possible to use ordinary block HTML, including IDs and classes, as long as it’s separated from other Markdown text by a blank line at each end:

<div class="foo" id="bar">

**This** should work, according to CommonMark 0.27.

</div>

However, this wasn’t originally in John Gruber’s original spec, which suggested that all Markdown parsing stops when you hit a block HTML tag. I’m really not sure of the path the classic PHP Markdown library goes with this (especially the older version currently used by Matrix), so my best suggestion is to try it for yourself.

If this method of including Markdown inside block HTML doesn’t work, the only alternative is to use HTML inside the block tag, which I can imagine may not be that helpful when wrapping everything in one.

However, using character-level HTML to insert classes, using <span> and the like, should always work (this was in the very original Markdown spec).

Hope this answers your question. :slight_smile:


(Plevan) #3

Thanks Luke - I suspected as much but it’s good to have the clarification.

We’ll just have to find a way to work within the limitations of Markdown and a lot of that can probably be solved by clever definition of CSS.


(Peter McLeod) #4

Hi
Not sure how you need to use it - so this is probably not ideal…
But you could also add the classes and ids in your markdown content using keyword replacements - something like:

%asset_assetid^gt:0:<div class="foo" id="bar">%
  **This** should work, according to CommonMark 0.27.
%asset_assetid^gt:0:</div>%

or some other keyword replacement that will outputs the html string should do.
Thanks
Peter