Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: HTML to Markdown (component & routes) #235

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions docs/decision-log/2024-12-27-render-to-markdown.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Render to Markdown

**Use `Astro.slots.render` to transform HTML content to Markdown both during build- and run-time.**

- Date: 2024-12-27
- Alternatives Considered: `AstroContainer.renderToString`, to Markdown per block, post build script to transform HTML to Markdown.
- Decision Made By: [Jasper](https://github.com/jbmoelker)

## Decision

Making our HTML pages available as Markdown is of interest to some people and especially suitable for software such as LLM's. Using `Astro.slots.render` seems to be the only solution that works both during build- and run-time.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Making our HTML pages available as Markdown is of interest to some people and especially suitable for software such as LLM's. Using `Astro.slots.render` seems to be the only solution that works both during build- and run-time.
Making our HTML pages available as Markdown is of interest to some people and especially suitable for software such as LLMs. Using `Astro.slots.render` seems to be the only solution that works both during build- and run-time.


### Solution

A componanion `.md.astro` route with a `ToMarkdown` component that leverages `Astro.slots.render` to get a usable HTML string, which can be used to transform to Markdown (and other formats):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
A componanion `.md.astro` route with a `ToMarkdown` component that leverages `Astro.slots.render` to get a usable HTML string, which can be used to transform to Markdown (and other formats):
A companion `.md.astro` route with a `ToMarkdown` component that leverages `Astro.slots.render` to get a usable HTML string, which can be used to transform to Markdown (and other formats):


```astro
---
const html = Astro.slots.render('default');
const md = /* transform html to md any way you want */
---
<Fragment set:text={md} />
```

### Alternatives considerd

These alternatives were explored and rejected:

- Using `AstroContainer.renderToString` in a `.ts` route rather than a `.astro` route would give direct access to the HTML string. However this method currently relies on `node:path` which is not available in the Cloudflare runtime.
- Using alternative render methods in each block that return a Markdown instead of an HTML output. This requires extra work for every existing and new component and is hard to keep in sync. This option is rejected because of the extra complexity.
- Using a post build script with the same transformers used in the `ToMarkdown` component or other libraries relying on the Node.js runtime. This works only for pages statically generated during build. It's unsuitable for dynamically rendered pages and is therefore rejected.
218 changes: 214 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading