[](https://jsr.io/@r4ai/remark-embed) [](https://codecov.io/gh/r4ai/remark-embed) [




A remark plugin to embed the content of the URL.
- Embed website using oEmbed by transformerOEmbed
- YouTube
- Spotify
- SpeakerDeck
- ...
- Generate link card using Open Graph metadata by transformerLinkCard
- Twitter
- Facebook
- ...
- Fully customizable with transformers
- You can define your own transformer
This plugin rewrite a paragraph containing only a URL, such as the following, into any element through the transformer.
``md`
https://example.com/hoge
> [!note]
> Note that URLs such as the following will not be converted:
>
> - according to https://example.com/hogeexample
> -
>
> 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.
Currently, this plugin provides the following transformers:
- transformerOEmbed - embeds the URL content by fetching the oEmbed metadata
- transformerLinkCard - 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.
1. let elements be a list of link nodes such that each node's parent paragraph contains only one link\elements = [{ type: 'link', url: 'https://example.com/hoge' }]
Example: element
2. for each of elements, do the following in parallel:url
1. let be the element's url value.transformer
2. for each of transformers, do the following in sequence:transformer.match(url)
1. if is true:element
1. replace the 's tag name with the result of transformer.tagName(url)element
2. replace the 's properties with the result of transformer.properties(url)element
3. replace the 's children with the result of transformer.children(url)
- Bun:
`sh`
bun add @r4ai/remark-embed
- Deno:
`sh`
deno add @r4ai/remark-embed
- NPM:
`sh`
npm install @r4ai/remark-embed
- PNPM:
`sh`
pnpm add @r4ai/remark-embed
- Yarn:
`sh`
yarn add @r4ai/remark-embed
`ts
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 = ;
const html = (
await unified()
.use(remarkParse)
.use(remarkRehype)
.use(remarkEmbed, {
transformers: [transformerOEmbed()],
})
.use(rehypeStringify)
.process(md)
).toString();
console.log(html);
`
Yields:
`html
See: