Testing how different overrides work in Astro:
- Code - doesn't work as expected
-
inline
not passed - no way to distinguish inline-code and block-code- had to do hack with
props.class === undefined
- had to do hack with
-
lang
not passed- had to do hack with
class?.replace("language-", "")
- had to do hack with
- block-code get's wrapped in extra
pre
- had to do hack with overriding
pre
and render it witout tag
- had to do hack with overriding
-
<slot />
- content is HTML string, which means that one needs to unescape it- is there a way to use something like
allowDangerousHTML
to pass raw strings to some nodes?
- is there a way to use something like
-
metastring
-
- remark-directive
- works if you add
smallRemarkAdapter
, thenleafdirective
,containerdirective
andtextdirective
can be handled with MDX overrides
- works if you add
- Image - works as expected
-
src
-
alt
-
title
-
- Link - works as expected
-
href
-
title
-
<slot />
-
- Blockquote
-
<slot />
- content is HTML string though
-
- Heading
-
id
-
<slot />
-
{#custom-id}
(works with remark plugin)remark-custom-heading-id
didn't work in my caseremark-heading-id
works, but in MDX one needs to escape curly-braces\{#custom-id\}
- see withastro/roadmap#329
-
Currently overrides only supported for MDX, but not for markdown. There is a proposal to support component overrides for markdown as well.
Ideally it should be as easy as Hugo render hooks.
Starlight provides <Aside />
component.
Also it provides markdown support for it:
:::note
Highlights information that users should take into account, even when skimming.
:::
They basically duplicate code: once it is written as remark plugin and once it is written as Astro component.
Instead they can reuse it like this:
<Aside type={directive}>
<slot />
</Aside>
Astro provides <Code />
component.
Also it provides markdown support for it:
```ts
const x = 1;
```
They basically duplicate code: once it is written as remark plugin and once it is written as Astro component.
Plus: if you want to customize it, for example, to add Mermaid support you need to jump through hoops to undo what built-in syntax highlighter does.
Instead they can reuse components like this:
{
lang === "mermaid" ? (
<Mermaid diagram={code} />
) : (
<Code code={code} lang={lang} inline={inline} />
)
}
Other potential use cases:
You may want to add anchors to headings. There is already rehype plugin for this - rehype-autolink-headings
. But this requires quite some configuration in different places.
On the other hand overriding component encapsulates all logic in one place.
You may want to add icons to external links or target="_blank"
or rel="nofollow"
. There is already rehype plugin for this - rehype-external-links
. But this requires quite some configuration in different places.
On the other hand overriding component encapsulates all logic in one place.
remark-directive
allows to create shortcuts for components, for example, one can create:
:youtube{#TtRtkTzHVBU}
to useastro-embed
YouTube component. See TextDirective:icon{mdi:account}
to useastro-icon
Icon component:::tip
to useAside
component (see above). See ContainerDirective
One can implement remark/rehype plugin and use await astro.renderToString(Component, { props, slots, request, params })
to render components.