-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Conversation
use slots.render to get html and transform it to markdown
|
||
export const partial = true; | ||
|
||
Astro.response.headers.set('Content-Type', 'text/markdown; charset=utf-8'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
HTML entities in the content are still encoded (<
becomes <
etc). This is not the cause of the ToMarkdown
component as it also happens with the HTML comment below. So Astro forces this higher up I believe. Haven't been able to stop encoding or force decoding 🤷 .
|
||
**Renders nested HTML content to Markdown.** | ||
|
||
## Examples |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to write these and others as test. But while this component works in a route, I'm not able to isolate the output properly. 🤷 .
import { datocmsCollection } from '@lib/datocms'; | ||
import { type PageRouteForPath, getPagePath } from '@lib/routing/page'; | ||
|
||
export async function getStaticPaths() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could keep this in index.astro
and try to import it in index.md.astro
. I like this separate file as it makes it more clear it can be used across routes. And it also obfuscated the content of index.astro
a bit. So I'm happy with this.
@@ -0,0 +1,16 @@ | |||
--- | |||
import ToMarkdown from '@components/ToMarkdown/ToMarkdown.astro'; | |||
import Page from './index.astro'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this pattern where the Page
from index.astro
can remain unmodified and this route does all the ToMarkdown
magic. If a developer using Head Start doesn't need this behaviour, this index.md.astro
route can simply be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It works (apart from the html entities) and there are some changes that are IMHO beneficial to the project regardless (moving staticPaths to separate file).
I do wonder if this functionality makes sense out of the box, instead of adding it later on for a project that needs it. For starters this increases build time for every project, even if they don't use it. A developer might remove the markdown route but forget the link tag, serving erroneous alternates that might negatively impact SEO.
|
||
## 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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): |
Changes
Making our HTML pages available as Markdown is of interest to some people and especially suitable for software such as LLM's. These changes make HTML content available as Markdown.
ToMarkdown
component to render HTML as Markdown (works both run- and build-time).[locale]/[...path]/index.md.astro
route to render matching route as Markdown (works both run- and build-time)..md
as Astro always generates.md/index.html
files.See decision log entry for background.
Associated issue
N/A
How to test
/en/documentation/getting-started/
.md
, like/en/documentation/getting-started.md
/en/demos/tables-demo.md
Checklist