@r4ai/remark-embed
v0.3.1
Published
[![JSR](https://jsr.io/badges/@r4ai/remark-embed)](https://jsr.io/@r4ai/remark-embed) [![codecov](https://codecov.io/gh/r4ai/remark-embed/graph/badge.svg?token=B9EZXC0PR8)](https://codecov.io/gh/r4ai/remark-embed) [![CI](https://github.com/r4ai/remark-emb
Downloads
234
Maintainers
Readme
@r4ai/remark-embed
A remark plugin to embed the content of the URL.
Features
- Embed website using oEmbed by
transformerOEmbed
- YouTube
- Spotify
- SpeakerDeck
- ...
- Generate link card using Open Graph metadata by
transformerLinkCard
- ...
- Fully customizable with transformers
- You can define your own transformer
About this plugin
This plugin rewrite a paragraph containing only a URL, such as the following, into any element through the transformer.
https://example.com/hoge
[!note] Note that URLs such as the following will not be converted:
according to https://example.com/hoge
[example](https://example.com/hoge)
URL must be the only content in the paragraph.
Also, if there is no blank line before and after the paragraph, it will not be converted.
Transformer
Currently, this plugin provides the following transformers:
transformerOEmbed
- embeds the URL content by fetching the oEmbed metadatatransformerLinkCard
- generates a link card by fetching the Open Graph metadata
You can also define your own transformer. Please refer to the transformer in the ./src/transformers directory for details on how to define them.
Following is the algorithm of how this plugin will apply the transformers.
- let
elements
be a list of link nodes such that each node's parent paragraph contains only one link
Example:elements = [{ type: 'link', url: 'https://example.com/hoge' }]
- for each
element
ofelements
, do the following in parallel:- let
url
be theelement
's url value. - for each
transformer
oftransformers
, do the following in sequence:- if
transformer.match(url)
istrue
:- replace the
element
's tag name with the result oftransformer.tagName(url)
- replace the
element
's properties with the result oftransformer.properties(url)
- replace the
element
's children with the result oftransformer.children(url)
- replace the
- if
- let
Installation
Bun:
bun add @r4ai/remark-embed
Deno:
deno add @r4ai/remark-embed
NPM:
npm install @r4ai/remark-embed
PNPM:
pnpm add @r4ai/remark-embed
Yarn:
yarn add @r4ai/remark-embed
Usage
import rehypeStringify from "rehype-stringify";
import remarkParse from "remark-parse";
import remarkRehype from "remark-rehype";
import { unified } from "unified";
import remarkEmbed from "@r4ai/remark-embed";
import { transformerOEmbed } from "@r4ai/remark-embed/transformers";
const md = `
<https://www.youtube.com/watch?v=jNQXAC9IVRw>
`;
const html = (
await unified()
.use(remarkParse)
.use(remarkRehype)
.use(remarkEmbed, {
transformers: [transformerOEmbed()],
})
.use(rehypeStringify)
.process(md)
).toString();
console.log(html);
Yields:
<p>
<div class="oembed oembed-video">
<iframe
width="200"
height="150"
src="https://www.youtube.com/embed/jNQXAC9IVRw?feature=oembed"
frameborder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share"
referrerpolicy="strict-origin-when-cross-origin"
allowfullscreen
title="Me at the zoo">
</iframe>
</div>
</p>