Created
August 8, 2026 02:14
-
-
Save aczw/042fef829cc17ced6fdb20e2ba795cc6 to your computer and use it in GitHub Desktop.
Sätteri HAST plugin for unwrapping images
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 { 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 }; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Largely based on this example but theirs didn't handle whitespace or multiple image nodes wrapped in a single
<p>.