Created
April 9, 2026 12:41
-
-
Save Princesseuh/8f051ddd2b6703ffbd3d6c28b0c231b0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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\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