Skip to content

Instantly share code, notes, and snippets.

@Princesseuh
Created April 9, 2026 12:41
Show Gist options
  • Select an option

  • Save Princesseuh/8f051ddd2b6703ffbd3d6c28b0c231b0 to your computer and use it in GitHub Desktop.

Select an option

Save Princesseuh/8f051ddd2b6703ffbd3d6c28b0c231b0 to your computer and use it in GitHub Desktop.
import { markdownToHtml, defineMdastPlugin, defineHastPlugin } from "satteri";
import { slug } from "./slug.ts";
const unwrapImages = defineMdastPlugin({
name: "unwrap-images",
paragraph(node, ctx) {
const image = node.children.find((c) => c.type === "image");
if (image && node.children.every((c) => c.type === "image" || (c.type === "text" && !c.value.trim())))
ctx.replaceNode(node, image);
},
});
const headingSlugs = defineHastPlugin({
name: "heading-slugs",
element: {
filter: ["h1", "h2", "h3", "h4", "h5", "h6"],
visit(node, ctx) {
ctx.setProperty(node, "id", slug(ctx.textContent(node)));
},
},
});
const md = `# Hello\n\n![cat](cat.jpg)\n\nSome **bold** text.\n\n## Details`;
const html = markdownToHtml(md, {
mdastPlugins: [unwrapImages],
hastPlugins: [headingSlugs],
});
console.log(html);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment