Skip to content

Instantly share code, notes, and snippets.

@aczw
Created August 8, 2026 02:14
Show Gist options
  • Select an option

  • Save aczw/042fef829cc17ced6fdb20e2ba795cc6 to your computer and use it in GitHub Desktop.

Select an option

Save aczw/042fef829cc17ced6fdb20e2ba795cc6 to your computer and use it in GitHub Desktop.
Sätteri HAST plugin for unwrapping images
import { defineHastPlugin } from "satteri";
const satteriUnwrapImagesPlugin = defineHastPlugin({
name: "test",
element: {
filter: ["p"],
visit(node, ctx) {
const containsOnlyImages =
node.children.length > 0 &&
node.children.every(
(child) =>
// Every child is an image...
(child.type === "element" && child.tagName === "img") ||
// ...or empty whitespace (ignore)
(child.type === "text" && child.value.trim() === ""),
);
// Inserts children into parent node and deletes paragraph node
if (containsOnlyImages) {
ctx.insertBefore(node, node.children);
ctx.removeNode(node);
}
return;
},
},
});
export { satteriUnwrapImagesPlugin };
@aczw

aczw commented Aug 8, 2026

Copy link
Copy Markdown
Author

Largely based on this example but theirs didn't handle whitespace or multiple image nodes wrapped in a single <p>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment