#Modify MDX before rendering

6 messages · Page 1 of 1 (latest)

obsidian yarrow
#

I'm migrating a project that did some manipulation over the Markdown before rendering.
I want to see if Astro is an option to replace the current system so I tried this:

---
import { getCollection } from 'astro:content';
const posts = await getCollection('blog');
const post = posts[0];
post.body = 'a';
const { Content } = await post.render();
---

<pre>{JSON.stringify(post, null, 2)}</pre>
<Content />

My expectation was that by changing the post body to a then post.render() would render that text. But it's rendering the original string.

Is there any way to modify the markdown before rendering?
Maybe middleware is the way but I'm not sure I understand it, pointing to any reference in the docs would help.

timber dust
#

I think body is read only, if I understamd your use case right, I think I did much worse.
What I would suggest, if possible to you, to decorrelate your mdx pre-processing before you feed it in astro, and call build.
What I did is, renaming .md to .mdx which can result in fails of non closed html tags which are only tolerated in .md. I wrote a node.js script to check and replace content.
If you want a better integration inside Astro pipeline, you can think of an "integration" it is more advanced though. The other way of adapting the remark pipeline is possible but hard because you can't have a guarantee of execution order or on the very start.

obsidian yarrow
#

I see, the reason I want to manipulate the content is because we have the same article written in two languages in the same file (this works for us as they can share the links and metadata) so I was expecting to be able to render once for english stripping the spanish part and vice versa. Looks like this is not an option.

I've seen the i18n recipes and they don't fit our needs as they separate the content by language in different folders, increasing the distance between the same content in different languages 😦

timber dust
#

I see now, I think you're into a more advanced cms usecase. I think you can work on a custom cms, generating mdx is by design quite easy, you can parse the mdx content and rework it, see the example mdx-check-mate, that only shows how to start though.