Repository navigation
Rendering <video> tag from Markdown
#14
Replies: 1 comment
|
Not out of the box - Astro always turns With the file in Simplest option: Markdown allows inline HTML, so put the video in <video src="/clip.mp4" controls></video>If you want to keep the image syntax, a remark plugin can swap video URLs for a // remark-video.mjs
import { visit } from "unist-util-visit";
export function remarkVideo() {
return (tree) => {
visit(tree, "image", (node, index, parent) => {
if (!/\.(mp4|webm|mov)$/i.test(node.url)) return;
parent.children[index] = {
type: "html",
value: `<video src="${node.url}" controls${node.alt ? ` aria-label="${node.alt}"` : ""}></video>`,
};
});
};
}On Astro 7 the default Markdown processor is Sätteri, which doesn't run remark plugins, so register it through the unified processor ( // astro.config.mjs
import { defineConfig } from "astro/config";
import { unified } from "@astrojs/markdown-remark";
import { remarkVideo } from "./remark-video.mjs";
export default defineConfig({
markdown: {
processor: unified({ remarkPlugins: [remarkVideo] }),
},
});On Astro 6 and older, Source, if you want it: |
Uh oh!
There was an error while loading. Please reload this page.
Could image syntax:
be used to render a<video>tag?Thanks!
All reactions