@r4ai/remark-embed
v0.5.0
Published
[](https://jsr.io/@r4ai/remark-embed) [](https://codecov.io/gh/r4ai/remark-embed) [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
elementsbe 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
elementofelements, do the following in parallel:- let
urlbe theelement's url value. - for each
transformeroftransformers, 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-embedDeno:
deno add @r4ai/remark-embedNPM:
npm install @r4ai/remark-embedPNPM:
pnpm add @r4ai/remark-embedYarn:
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>