@mirrordown/remd-unwrap-images 0.0.0 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +94 -0
- package/dist/index.d.mts +8 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +37 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +33 -1
package/README.md
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# @mirrordown/remd-unwrap-images
|
|
2
|
+
|
|
3
|
+
> Part of [Mirrordown](https://github.com/mirrordown/mirrordown) — a suite of markdown syntax extensions for the unified and markdown-it ecosystems.
|
|
4
|
+
|
|
5
|
+
A remark/rehype (unified) plugin for the `unwrap-images` syntax extension.
|
|
6
|
+
|
|
7
|
+
## Overview
|
|
8
|
+
|
|
9
|
+
The `unwrap-images` plugin removes the `<p>` wrapper that Markdown adds around standalone images, allowing images to render as direct block-level elements. Images inside links are also unwrapped.
|
|
10
|
+
|
|
11
|
+
**Before:**
|
|
12
|
+
|
|
13
|
+
```html
|
|
14
|
+
<p><img src="photo.jpg" alt="A photo" /></p>
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
**After:**
|
|
18
|
+
|
|
19
|
+
```html
|
|
20
|
+
<img src="photo.jpg" alt="A photo" />
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Syntax
|
|
24
|
+
|
|
25
|
+
Any image that stands alone on a paragraph — with no other text or inline content — is unwrapped:
|
|
26
|
+
|
|
27
|
+
```markdown
|
|
28
|
+

|
|
29
|
+
|
|
30
|
+
Images mixed with text are **not** unwrapped:
|
|
31
|
+
|
|
32
|
+
Here is an inline image:  within a sentence.
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
[!NOTE]
|
|
36
|
+
This is a rehype plugin (`rehype-*`), not a remark plugin. Add it **after** `remarkRehype` in your pipeline.
|
|
37
|
+
|
|
38
|
+
## Install
|
|
39
|
+
|
|
40
|
+
```sh
|
|
41
|
+
npm install @mirrordown/remd-unwrap-images
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Unified
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
import { unified } from "unified";
|
|
48
|
+
import remarkParse from "remark-parse";
|
|
49
|
+
import remarkRehype from "remark-rehype";
|
|
50
|
+
import rehypeStringify from "rehype-stringify";
|
|
51
|
+
import { rehypeUnwrapImages } from "@mirrordown/remd-unwrap-images";
|
|
52
|
+
|
|
53
|
+
const processor = unified()
|
|
54
|
+
.use(remarkParse)
|
|
55
|
+
.use(remarkRehype)
|
|
56
|
+
.use(rehypeUnwrapImages)
|
|
57
|
+
.use(rehypeStringify);
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### Astro
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
// astro.config.ts
|
|
64
|
+
import { defineConfig } from "astro/config";
|
|
65
|
+
import { rehypeUnwrapImages } from "@mirrordown/remd-unwrap-images";
|
|
66
|
+
|
|
67
|
+
export default defineConfig({
|
|
68
|
+
markdown: {
|
|
69
|
+
rehypePlugins: [rehypeUnwrapImages]
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### VitePress
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
// .vitepress/config.ts
|
|
78
|
+
import { defineConfig } from "vitepress";
|
|
79
|
+
import { rehypeUnwrapImages } from "@mirrordown/remd-unwrap-images";
|
|
80
|
+
|
|
81
|
+
export default defineConfig({
|
|
82
|
+
markdown: {
|
|
83
|
+
rehypePlugins: [rehypeUnwrapImages]
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Documentation
|
|
89
|
+
|
|
90
|
+
Full documentation, more examples, and configuration options: [github.com/mirrordown/mirrordown](https://github.com/mirrordown/mirrordown) (dedicated docs site coming soon).
|
|
91
|
+
|
|
92
|
+
## License
|
|
93
|
+
|
|
94
|
+
MIT © [Drake Costa](https://saeris.gg)
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"mappings":";;;;cAsCa,kBAAA,EAAoB,MAAA,KAAW,IAAA"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { SKIP, visit } from "unist-util-visit";
|
|
2
|
+
import { whitespace } from "hast-util-whitespace";
|
|
3
|
+
//#region src/index.ts
|
|
4
|
+
const UNKNOWN = 1;
|
|
5
|
+
const HAS_IMAGE = 2;
|
|
6
|
+
const HAS_OTHER = 3;
|
|
7
|
+
const applicable = (node, inLink) => {
|
|
8
|
+
let result = UNKNOWN;
|
|
9
|
+
for (const child of node.children) {
|
|
10
|
+
if (child.type === "text" && whitespace(child.value)) continue;
|
|
11
|
+
if (child.type !== "element") return HAS_OTHER;
|
|
12
|
+
if (child.tagName === "img") {
|
|
13
|
+
result = HAS_IMAGE;
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
if (!inLink && (child.tagName === "a" || child.tagName === "button")) {
|
|
17
|
+
const inner = applicable(child, true);
|
|
18
|
+
if (inner === HAS_OTHER) return HAS_OTHER;
|
|
19
|
+
if (inner === HAS_IMAGE) result = HAS_IMAGE;
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
return HAS_OTHER;
|
|
23
|
+
}
|
|
24
|
+
return result;
|
|
25
|
+
};
|
|
26
|
+
const rehypeUnwrapImages = () => (tree) => {
|
|
27
|
+
visit(tree, "element", (node, index, parent) => {
|
|
28
|
+
if (node.tagName === "p" && parent && typeof index === "number" && applicable(node, false) === HAS_IMAGE) {
|
|
29
|
+
parent.children.splice(index, 1, ...node.children);
|
|
30
|
+
return [SKIP, index];
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
//#endregion
|
|
35
|
+
export { rehypeUnwrapImages };
|
|
36
|
+
|
|
37
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["import { SKIP, visit } from \"unist-util-visit\";\nimport { whitespace } from \"hast-util-whitespace\";\nimport type { Element, Root } from \"hast\";\nimport type { Plugin } from \"unified\";\n\nconst UNKNOWN = 1 as const;\nconst HAS_IMAGE = 2 as const;\nconst HAS_OTHER = 3 as const;\ntype ApplicableResult = typeof UNKNOWN | typeof HAS_IMAGE | typeof HAS_OTHER;\n\n// Returns HAS_IMAGE if node's children are only images/whitespace (possibly\n// wrapped in a link), UNKNOWN if empty/whitespace-only, HAS_OTHER otherwise.\nconst applicable = (node: Element, inLink: boolean): ApplicableResult => {\n let result: ApplicableResult = UNKNOWN;\n\n for (const child of node.children) {\n if (child.type === \"text\" && whitespace(child.value)) continue;\n\n if (child.type !== \"element\") return HAS_OTHER;\n\n if (child.tagName === \"img\") {\n result = HAS_IMAGE;\n continue;\n }\n\n if (!inLink && (child.tagName === \"a\" || child.tagName === \"button\")) {\n const inner = applicable(child, true);\n if (inner === HAS_OTHER) return HAS_OTHER;\n if (inner === HAS_IMAGE) result = HAS_IMAGE;\n continue;\n }\n\n return HAS_OTHER;\n }\n\n return result;\n};\n\nexport const rehypeUnwrapImages: Plugin<[], Root> = () => (tree) => {\n visit(tree, \"element\", (node, index, parent) => {\n if (\n node.tagName === \"p\" &&\n parent &&\n typeof index === \"number\" &&\n applicable(node, false) === HAS_IMAGE\n ) {\n parent.children.splice(index, 1, ...node.children);\n return [SKIP, index];\n }\n return;\n });\n};\n"],"mappings":";;;AAKA,MAAM,UAAU;AAChB,MAAM,YAAY;AAClB,MAAM,YAAY;AAKlB,MAAM,cAAc,MAAe,WAAsC;CACvE,IAAI,SAA2B;AAE/B,MAAK,MAAM,SAAS,KAAK,UAAU;AACjC,MAAI,MAAM,SAAS,UAAU,WAAW,MAAM,MAAM,CAAE;AAEtD,MAAI,MAAM,SAAS,UAAW,QAAO;AAErC,MAAI,MAAM,YAAY,OAAO;AAC3B,YAAS;AACT;;AAGF,MAAI,CAAC,WAAW,MAAM,YAAY,OAAO,MAAM,YAAY,WAAW;GACpE,MAAM,QAAQ,WAAW,OAAO,KAAK;AACrC,OAAI,UAAU,UAAW,QAAO;AAChC,OAAI,UAAU,UAAW,UAAS;AAClC;;AAGF,SAAO;;AAGT,QAAO;;AAGT,MAAa,4BAA8C,SAAS;AAClE,OAAM,MAAM,YAAY,MAAM,OAAO,WAAW;AAC9C,MACE,KAAK,YAAY,OACjB,UACA,OAAO,UAAU,YACjB,WAAW,MAAM,MAAM,KAAK,WAC5B;AACA,UAAO,SAAS,OAAO,OAAO,GAAG,GAAG,KAAK,SAAS;AAClD,UAAO,CAAC,MAAM,MAAM;;GAGtB"}
|
package/package.json
CHANGED
|
@@ -1,10 +1,42 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mirrordown/remd-unwrap-images",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Drake Costa <drake@saeris.io> (https://saeris.gg)",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
8
|
"url": "git+https://github.com/mirrordown/mirrordown.git"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"dist"
|
|
12
|
+
],
|
|
13
|
+
"type": "module",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"development": "./src/index.ts",
|
|
17
|
+
"types": "./dist/index.d.mts",
|
|
18
|
+
"import": "./dist/index.mjs"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"hast-util-whitespace": "^3.0.0",
|
|
23
|
+
"unist-util-visit": "catalog:"
|
|
24
|
+
},
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"@types/hast": "catalog:",
|
|
27
|
+
"unified": "catalog:"
|
|
28
|
+
},
|
|
29
|
+
"peerDependencies": {
|
|
30
|
+
"unified": "^11.0.0"
|
|
31
|
+
},
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=24.0.0"
|
|
34
|
+
},
|
|
35
|
+
"bumpy": {
|
|
36
|
+
"buildCommand": "vp pack",
|
|
37
|
+
"publishCommand": [
|
|
38
|
+
"node ../../scripts/sync-jsr-version.mjs",
|
|
39
|
+
"npm publish --access public"
|
|
40
|
+
]
|
|
9
41
|
}
|
|
10
42
|
}
|