Real-world Examples
babakfp.ir
This site is built using the MDX Svelte library. You can check out the source code to see how it works.
https://github.com/babakfp/babakfp.ir
How it works
elements
option
The mdxPreprocess
function has an elements
option, which lets you replace markdown elements with custom components. Here's how this site uses it:
blockquote
tag
The blockquote
tag is replaced with a custom Svelte component. This adds custom styling and an icon at the top left corner. Example:
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
img
tag
The img
tag is replaced with a custom Svelte component. This adds loading="lazy"
and a button to open the image in a new tab. Example:
pre
tag
The pre
tag is replaced with a custom Svelte component. This adds a copy button. Example:
Unified transformer
makeToc
function
The built-in remarkToc
plugin is disabled, replaced with the makeToc
function.
A custom getHeadings
function uses makeToc
to extract headings from the .article-content
container.
rehypeAutolinkHeadings
plugin
The rehypeAutolinkHeadings
plugin is customized to add a permalink icon to headings. It also adds an aria-label
to the anchor tag, a class
to the permalink icon, and excludes the h1
heading.