• Stars
    star
    173
  • Rank 220,124 (Top 5 %)
  • Language Astro
  • License
    MIT License
  • Created over 2 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Render remote HTML or Markdown content in Astro with full control over the output

Astro Remote

NPM Version

Render remote HTML or Markdown content in Astro with full control over the output.

Powered by ultrahtml and marked.

Install

npm install astro-remote
pnpm install astro-remote
yarn install astro-remote

Rendering Remote Content

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.

---
import { Markup, Markdown } from 'astro-remote';
const { html, markdown } = await fetch('http://my-site.com/api/v1/post').then(res => res.json());
---

<Markup content={html} />
<Markdown content={markdown} />

Sanitization

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.

---
const { as: Component, href } = Astro.props;
---

<Component><a href={href}><slot /></a></Component>

<CodeBlock />

The CodeBlock component allows you customize the rendering of code blocks. It receives the following props:

  • lang, the language specified after the three backticks (defaults to plaintext)
  • code, the raw code to be highlighted. Be sure to escape the output!
  • ...props, any other attributes passed to the three backticks. These should follow HTML attribute format (name="value")

A sample CodeBlock component might look something like this.

---
const { lang, code, ...props } = Astro.props;
const highlighted = await highlight(code, { lang });
---

<pre class={`language-${lang}`}><code set:html={highlighted} /></pre>

<CodeSpan />

The CodeSpan component allows you customize the rendering of inline code spans. It receives the following props:

  • code, the value of the code span

A sample CodeSpan component might look something like this.

---
const { code } = Astro.props;
---

<code set:text={code} />

<Note />

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.

Given the following markdown source:

# Hello world!

<MyCustomComponent a="1" b="2" c="3">It works!</MyCustomComponent>
---
import { Markdown } from 'astro-remote';
import MyCustomComponent from '../components/MyCustomComponent.astro';
const content = await fetch('http://my-site.com/api/v1/post').then(res => res.text());
---

<Markdown content={content} sanitize={{ allowComponents: true }} components={{ MyCustomComponent }} />

Using Marked Extensions

If you'd like to extend the underlying Marked behavior, the marked prop accepts extensions.

---
import { Markdown } from 'astro-remote';
import markedAlert from 'marked-alert'

const content = await fetch('http://my-site.com/api/v1/post').then(res => res.text());
---

<Markdown content={content} marked={{ extensions: [ markedAlert() ] }} />

More Repositories

1

clack

Effortlessly build beautiful command-line apps
TypeScript
5,170
star
2

astro-icon

Inline and sprite-based SVGs in Astro made easy!
MDX
1,072
star
3

microsite

Do more with less JavaScript. Microsite is a smarter, performance-obsessed static site generator powered by Preact and Snowpack.
TypeScript
877
star
4

ultrahtml

TypeScript
521
star
5

natemoo-re

TypeScript
433
star
6

micromorph

A very tiny library for diffing DOM nodes
TypeScript
339
star
7

satori-html

An HTML adapter for Vercel's Satori
TypeScript
273
star
8

ultrafetch

RFC-7234 compliant caching for any `fetch` implementation
TypeScript
222
star
9

ultramatter

<1kB frontmatter parser that supports a reasonable subset of YAML
TypeScript
123
star
10

animate-presence

Effortless element entrance/exit animations
TypeScript
99
star
11

proload

Searches for and loads your tool's JavaScript configuration files with full support for CJS, ESM, TypeScript and more.
JavaScript
81
star
12

astro-capo

TypeScript
57
star
13

eslexer

Incredibly tiny, fast JS tokenizer
TypeScript
57
star
14

astro-php

Please don't use this in production
JavaScript
44
star
15

ultraflag

<1kB CLI flag parser
TypeScript
44
star
16

feed

Astro
43
star
17

magic-element

Automatic custom-elements with zero boilerplate
TypeScript
34
star
18

tslint-stencil

Contributes some helpful tslint rules for Stencil projects
TypeScript
22
star
19

tinyshell

A tiny `node:child_process` wrapper for shell scripts
TypeScript
21
star
20

konami-listener

Vanilla web component that listens for the Konami Code
JavaScript
20
star
21

ultramark

TypeScript
19
star
22

tsdx-monorepo

TypeScript
19
star
23

goldmark

JavaScript
19
star
24

project-template

TypeScript
19
star
25

tiny-decode

Decode HTML entities the browser-friendly way
JavaScript
18
star
26

prettier-plugin-style-order

Prettier plugin to automatically sort style declarations
JavaScript
16
star
27

stencil-forms

📝🤯 Stencil component to automatically manage form state
TypeScript
14
star
28

miles-of-code

Astro
13
star
29

microsite-demo

TypeScript
11
star
30

stencil-tools

Editor enhancements and utilities for Stencil projects
TypeScript
10
star
31

emojex

Emoji-based Regular Expressions
TypeScript
10
star
32

astro-client-hints

TypeScript
9
star
33

astro-run

TypeScript
9
star
34

clack.cc

Astro
9
star
35

astro-form

Astro
7
star
36

raycast-microblog

TypeScript
7
star
37

topic

TypeScript
7
star
38

exo-store

TypeScript
6
star
39

astro-breadcrumbs

Astro
6
star
40

astro-beep

JavaScript
6
star
41

gamepad

Provides a simple, event-based API for Gamepads on the web
TypeScript
5
star
42

esm-create

npm init, but better
JavaScript
5
star
43

experiment--astro-editor

Astro
5
star
44

orbit

Astro
4
star
45

blocks

TypeScript
3
star
46

require-or-import

JavaScript
3
star
47

stencil-markdown

Stencil plugin that allows Markdown files to be imported as FunctionalComponents
TypeScript
3
star
48

a11y-types

Typescript Definitions for ARIA attribute constants
TypeScript
2
star
49

microstream

TypeScript
2
star
50

bottom-sheet

Bottom sheet web component
TypeScript
2
star
51

stencil-hooks

TypeScript
2
star
52

typescript-plugin-stencil

Typescript plugin for a smoother Developer Experience within Stencil projects
TypeScript
2
star
53

midu-astro

Astro
2
star
54

hibiscus

A *VERY ROUGH* proof of concept using Stencil to dynamically load and animate an SVG-based font.
TypeScript
1
star
55

astro-bundle-demo

JavaScript
1
star
56

node-shqxzf

Created with StackBlitz ⚡️
1
star
57

use-linked-context

JavaScript
1
star
58

pronouns

HTML
1
star
59

astro-playground

TypeScript
1
star
60

astro-dev-plugin-sample

Astro
1
star
61

actions-playground

Shell
1
star
62

test-social-web

1
star
63

astro-template-blog

1
star
64

astro-gh-pages-test

Astro
1
star
65

astro-minimal

JavaScript
1
star
66

microsite-templates

TypeScript
1
star
67

text-wrap-balance

1
star
68

node-wfhqwq

JavaScript
1
star
69

git-comments-test

1
star
70

astro-demo-named-slots

Astro
1
star
71

vscode-filename-snippets

TEST
TypeScript
1
star
72

astro-vercel-rewrites

Astro
1
star
73

action-test

TypeScript
1
star
74

bug--astro-netlify-libsql

TypeScript
1
star
75

natemoo-re.github.io

Public site
HTML
1
star