Render styleable Markdown in your HTML with <md-block>

To render Markdown on web pages, Lea Verou created <md-block>. It’s a zero-dependency Custom Element that, unlike other solutions, renders the content in the light DOM which allows you to style the output easily. There are many web components these days to render Markdown to HTML. However, all render the resulting Markdown in Shadow DOM, …

To render Markdown on web pages, Lea Verou created <md-block>. It’s a zero-dependency Custom Element that, unlike other solutions, renders the content in the light DOM which allows you to style the output easily.

There are many web components these days to render Markdown to HTML. However, all render the resulting Markdown in Shadow DOM, making it painful to style like a regular part of the page, which my use cases required.

I also wanted a few more things existing web components didn’t have.

So I made my own. Feel free to use it. Or don’t. 🤷🏽‍♀️ I primarily wrote it to scratch my own itch anyway! 😊

Projects that grow out of a personal necessity are the best ones 🙂

~

Load it in via a <script> tag, or import the proper classes.

<script type="module" src="https://md-block.verou.me/md-block.js"></script>
import { MarkdownBlock, MarkdownSpan, MarkdownElement } from "https://md-block.verou.me/md-block.js";

Both methods self-register the <md-block> Custom Element, which can then be used as follows:

<md-block>
	# Heading

	Text **bold text** *italic text* ~strike~

	```js
	test.foo();
	```

	```html
	&lt;foo&gt;
	```

	* List item 1
	* List item 2
</md-block>

~

In addition to the simple example above, it also supports rendering externally loaded markdown, allows customisation of the start heading level, has syntax highlighting (using Prism), can auto-link the headings, etc. Also comes with a <md-span> Custom Element (which is also registered upon loading).

In order to get to the final result, some heavy “Yak Shaving” was involved though. Now that sounded a wee bit too familiar to be honest … 😅

<md-block>
On Yak Shaving and <md-block>, a new HTML element for Markdown →


Print Share Comment Cite Upload Translate
APA
Bramus! | Sciencx (2024-03-28T20:37:54+00:00) » Render styleable Markdown in your HTML with <md-block>. Retrieved from https://www.scien.cx/2021/11/29/render-styleable-markdown-in-your-html-with-md-block/.
MLA
" » Render styleable Markdown in your HTML with <md-block>." Bramus! | Sciencx - Monday November 29, 2021, https://www.scien.cx/2021/11/29/render-styleable-markdown-in-your-html-with-md-block/
HARVARD
Bramus! | Sciencx Monday November 29, 2021 » Render styleable Markdown in your HTML with <md-block>., viewed 2024-03-28T20:37:54+00:00,<https://www.scien.cx/2021/11/29/render-styleable-markdown-in-your-html-with-md-block/>
VANCOUVER
Bramus! | Sciencx - » Render styleable Markdown in your HTML with <md-block>. [Internet]. [Accessed 2024-03-28T20:37:54+00:00]. Available from: https://www.scien.cx/2021/11/29/render-styleable-markdown-in-your-html-with-md-block/
CHICAGO
" » Render styleable Markdown in your HTML with <md-block>." Bramus! | Sciencx - Accessed 2024-03-28T20:37:54+00:00. https://www.scien.cx/2021/11/29/render-styleable-markdown-in-your-html-with-md-block/
IEEE
" » Render styleable Markdown in your HTML with <md-block>." Bramus! | Sciencx [Online]. Available: https://www.scien.cx/2021/11/29/render-styleable-markdown-in-your-html-with-md-block/. [Accessed: 2024-03-28T20:37:54+00:00]
rf:citation
» Render styleable Markdown in your HTML with <md-block> | Bramus! | Sciencx | https://www.scien.cx/2021/11/29/render-styleable-markdown-in-your-html-with-md-block/ | 2024-03-28T20:37:54+00:00
https://github.com/addpipe/simple-recorderjs-demo