The most basic function of astro-remote is to convert a string of HTML or Markdown to HTML. Use the Markup and Markdown components depending on your input.
By default, all HTML content will be sanitized with sensible defaults (script blocks are dropped). This can be controlled using the SanitizeOptions available in ultrahtml. Set to false to disable sanitization.
---import { Markup } from 'astro-remote';const content = await fetch('http://my-site.com/api/v1/post').then(res => res.text());---<!-- Disallow `head` and `style` attributes, and standard formatting from host website --><Markup content={content} sanitize={{ dropElements: ["head","style"], blockElements: ["html", "body", "div"], }}/>
Customization
Both Markup and Markdown allow full control over the rendering of output. The components option allows you to replace a standard HTML element with a custom component.
---import { Markdown, Markup } from 'astro-remote';import Title from '../components/Title.astro';const content = await fetch('http://my-site.com/api/v1/post').then(res => res.text());---<!-- Render <h1> as custom <Title> component --><Markup content={content} components={{ h1: Title }} /><Markdown content={content} components={{ h1: Title }} />
In addition to built-in HTML Elements, Markdown also supports a few custom components for convenience.
<Heading />
The Heading component renders all h1 through h6 elements. It receives the following props:
as, the h1 through h6 tag
href, a pre-generated, slugified href
text, the text content of the children (for generating a custom slug)
---import { Markdown } from 'astro-remote';import Heading from '../components/Heading.astro';const content = await fetch('http://my-site.com/api/v1/post').then(res => res.text());---<!-- Render all <h1> through <h6> using custom <Heading> component --><Markdown content={content} components={{ Heading }} />
A sample Heading component might look something like this.
The Note component allows you customize the rendering of GitHub-style notes and warnings. It receives the following props:
type, either "note" or "warning"
To use a Note component in Markdown, use the following syntax:
> **Note**> Some tip here!> **Warning**> Some warning here!
Custom Components in Markdown
If you'd like to allow custom components in Markdown, you can do so using a combination of the sanitize and components options. By default, sanitization removes components.