remark-inline-svg-flex 0.1.1

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 gass-git
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,124 @@
1
+ > 🚧 **Work in progress** This plugin is still under active development. Expect changes.
2
+
3
+ # remark-inline-svg-flex
4
+
5
+ [![Downloads](https://img.shields.io/npm/dm/remark-inline-svg-flex.svg?style=flat-square)](https://www.npmjs.com/package/remark-inline-svg-flex)
6
+ [![version](https://img.shields.io/npm/v/remark-inline-svg-flex.svg?style=flat-square)](https://www.npmjs.com/package/remark-inline-svg-flex)
7
+ [![MIT License](https://img.shields.io/npm/l/remark-inline-svg-flex.svg?style=flat-square)](https://github.com/gass-git/remark-inline-svg-flex/blob/main/LICENSE)
8
+
9
+ Flexible Remark plugin that inlines and optimizes SVGs with SVGO, featuring customizable path resolution, wrappers and others.
10
+
11
+ - [remark-inline-svg-flex](#remark-inline-svg-flex)
12
+ - [Features](#features)
13
+ - [Installation](#installation)
14
+ - [Usage](#usage)
15
+ - [Options](#options)
16
+ - [`suffix`](#suffix)
17
+ - [`assetsDir`](#assetsdir)
18
+ - [`wrapper`](#wrapper)
19
+ - [`svgo`](#svgo)
20
+ - [SVGO configuration](#svgo-configuration)
21
+
22
+ ## Features
23
+
24
+ ### ✔️ Robust and customizable path resolution
25
+
26
+ - If the SVG path is absolute, it is used as-is.
27
+ - If the path is relative and assetsDir is defined, it is resolved relative to that directory.
28
+ - Otherwise, the path is resolved relative to the Markdown file’s location.
29
+
30
+ ### ✔️ Custom HTML wrapper support
31
+
32
+ Use your own custom HTML wrapper, no wrapper at all, or the default:
33
+
34
+ ```
35
+ <figure class="inline-svg"></figure>
36
+ ```
37
+
38
+ ### ✔️ Optional SVG optimization
39
+
40
+ SVG optimization can be disabled if needed e.g. if SVGO removes required attributes.
41
+
42
+ ### ✔️ Configurable suffix
43
+
44
+ By default, only files ending in `.svg` are processed. You can customize the suffix to suit your needs.
45
+
46
+ ## Installation
47
+
48
+ ```
49
+ npm i remark-inline-svg-flex
50
+ ```
51
+
52
+ ## Usage
53
+
54
+ Say we have the following file `example.md`:
55
+
56
+ ```markdown
57
+ # Hello
58
+
59
+ This is a test markdown document.
60
+
61
+ ![some svg](some.svg)
62
+ ```
63
+
64
+ And our module `example.js` looks as follows:
65
+
66
+ ```js
67
+ import { remark } from 'remark';
68
+ import { readFile } from 'node:fs/promises';
69
+ import { inlineSvg } from 'remark-inline-svg-flex';
70
+
71
+ const markdown = await readFile(_path, { encoding: 'utf8' });
72
+
73
+ return await remark().use(remarkParse).use(inlineSvg).process(markdown);
74
+ ```
75
+
76
+ Now running `node example.js` yields:
77
+
78
+ ```markdown
79
+ # Hello
80
+
81
+ This is a test markdown document.
82
+
83
+ <figure class="inline-svg">
84
+ <svg fill="none" viewBox="0 0 250 250" role="img" aria-hidden="true"><circle cx="125" cy="125" r="100" fill="#BA5B5B"/></svg>
85
+ </figure>
86
+ ```
87
+
88
+ ## Options
89
+
90
+ | Key | type | Default value | Description |
91
+ | ------------------------- | --------------------- | ---------------------------------------- | ---------------------------------------------------------- |
92
+ | [`suffix`](#suffix) | `string` | `'.svg'` | The plugin only processes SVG files ending with this value |
93
+ | [`assetsDir`](#assetsdir) | `string \| undefined` | `undefined` | Base directory where SVG files are located |
94
+ | [`wrapper`](#wrapper) | `string` | `'<figure class="inline-svg"></figure>'` | HTML wrapper used to wrap the inlined SVG |
95
+ | [`svgo`](#svgo) | `boolean` | `true` | Enable or disable SVG optimization |
96
+
97
+ ### `suffix`
98
+
99
+ Only image nodes whose URL ends with the specified suffix will be processed. Defaults to `'.svg'`.
100
+
101
+ ### `assetsDir`
102
+
103
+ Base directory where SVG files are located. By default is `undefined` and it will resolve paths either in an absolute or relative manner.
104
+
105
+ ### `wrapper`
106
+
107
+ Defines the HTML wrapper used around the inlined SVG.
108
+
109
+ - Set to an empty string `''` to disable wrapping entirely.
110
+ - Defaults to:
111
+
112
+ ```
113
+ <figure class="inline-svg"></figure>
114
+ ```
115
+
116
+ ### `svgo`
117
+
118
+ The SVG's are optimized by default. Disable it by setting it to `false`.
119
+
120
+ ## SVGO configuration
121
+
122
+ ```
123
+ { plugins: ['preset-default', 'removeXMLNS', 'removeDimensions'] }
124
+ ```
@@ -0,0 +1 @@
1
+ export * from './plugin';
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from './plugin';
@@ -0,0 +1,14 @@
1
+ import type { Plugin } from 'unified';
2
+ import type { Root } from 'mdast';
3
+ import type { Options } from './types';
4
+ /**
5
+ * Transforms image nodes that match the given suffix (default: .svg) into inline SVG.
6
+ *
7
+ * @param options - Plugin configuration
8
+ * @param options.suffix - File extension to match (default: '.svg')
9
+ * @param options.assetsDir - Optional base directory for resolving SVG paths (default: undefined)
10
+ * @param options.wrapper - Optional HTML wrapper for the inline SVG (default: `<figure class="inline-svg"></figure>`)
11
+ * @param options.svgo - Whether to optimize SVG using SVGO (default: true)
12
+ */
13
+ declare const inlineSvg: Plugin<[Options?], Root, Root>;
14
+ export { inlineSvg };
package/dist/plugin.js ADDED
@@ -0,0 +1,82 @@
1
+ import path from 'node:path';
2
+ import fs from 'node:fs';
3
+ import { visit } from 'unist-util-visit';
4
+ import { VFile } from 'vfile';
5
+ import { optimize } from 'svgo';
6
+ /**
7
+ * Transforms image nodes that match the given suffix (default: .svg) into inline SVG.
8
+ *
9
+ * @param options - Plugin configuration
10
+ * @param options.suffix - File extension to match (default: '.svg')
11
+ * @param options.assetsDir - Optional base directory for resolving SVG paths (default: undefined)
12
+ * @param options.wrapper - Optional HTML wrapper for the inline SVG (default: `<figure class="inline-svg"></figure>`)
13
+ * @param options.svgo - Whether to optimize SVG using SVGO (default: true)
14
+ */
15
+ const inlineSvg = (consumerOptions = {}) => {
16
+ const options = {
17
+ suffix: consumerOptions.suffix ?? '.svg',
18
+ assetsDir: consumerOptions.assetsDir,
19
+ wrapper: consumerOptions.wrapper ?? '<figure class="inline-svg"></figure>',
20
+ svgo: consumerOptions.svgo ?? true,
21
+ };
22
+ return function transformer(tree, file) {
23
+ visit(tree, 'image', (node, i, parent) => {
24
+ if (!node.url?.endsWith(options.suffix) || !parent)
25
+ return;
26
+ try {
27
+ const svgPath = resolvePath(options.assetsDir, node, path.dirname(file.history[0]));
28
+ const svgString = processSvg(svgPath, options.svgo);
29
+ parent.children[i] = {
30
+ type: 'html',
31
+ value: options.wrapper ? wrap(svgString, options.wrapper) : svgString,
32
+ };
33
+ }
34
+ catch (error) {
35
+ console.warn(error);
36
+ }
37
+ });
38
+ };
39
+ };
40
+ /**
41
+ * Reads an SVG file and optionally optimizes it with SVGO.
42
+ */
43
+ function processSvg(path, svgo) {
44
+ const svgString = fs.readFileSync(path, 'utf8');
45
+ return svgo ? optimizeSvg(svgString).data : svgString;
46
+ }
47
+ /**
48
+ * Injects SVG markup into an HTML wrapper before its closing tag.
49
+ */
50
+ function wrap(svgString, htmlWrapper) {
51
+ const i = htmlWrapper.lastIndexOf('</');
52
+ if (i === -1) {
53
+ throw new Error(`Invalid HTML wrapper`);
54
+ }
55
+ return htmlWrapper.slice(0, i) + svgString + htmlWrapper.slice(i);
56
+ }
57
+ /**
58
+ * Resolves the final SVG file path based on:
59
+ * 1) absolute URL → from project root
60
+ * 2) `assetsDir` → relative to it
61
+ * 3) fallback → relative to Markdown file directory
62
+ */
63
+ function resolvePath(assetsDir, node, markdownFileDir) {
64
+ if (path.isAbsolute(node.url)) {
65
+ return path.resolve(process.cwd(), node.url);
66
+ }
67
+ else if (assetsDir) {
68
+ return path.resolve(process.cwd(), assetsDir, node.url);
69
+ }
70
+ else {
71
+ return path.resolve(markdownFileDir, node.url);
72
+ }
73
+ }
74
+ /**
75
+ * Optimizes SVG content using predefined SVGO plugins.
76
+ */
77
+ function optimizeSvg(svgString) {
78
+ return optimize(svgString, {
79
+ plugins: ['preset-default', 'removeXMLNS', 'removeDimensions'],
80
+ });
81
+ }
82
+ export { inlineSvg };
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "remark-inline-svg-flex",
3
+ "version": "0.1.1",
4
+ "description": "Flexible Remark plugin that inlines and optimizes SVGs with SVGO, featuring customizable path resolution and wrappers.",
5
+ "keywords": [
6
+ "unified",
7
+ "rehype",
8
+ "plugin",
9
+ "rehype-plugin",
10
+ "inline",
11
+ "svg",
12
+ "image",
13
+ "img",
14
+ "html"
15
+ ],
16
+ "homepage": "https://github.com/gass-git/remark-inline-svg-flex#readme",
17
+ "bugs": {
18
+ "url": "https://github.com/gass-git/remark-inline-svg-flex/issues"
19
+ },
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/gass-git/remark-inline-svg-flex.git"
23
+ },
24
+ "license": "MIT",
25
+ "author": "gass-git",
26
+ "type": "module",
27
+ "main": "index.js",
28
+ "scripts": {
29
+ "build": "tsc",
30
+ "dev": "rimraf --glob \"*.tgz\" && npm run build && npm pack && npm link",
31
+ "tests": "vitest"
32
+ },
33
+ "devDependencies": {
34
+ "@types/mdast": "^4.0.4",
35
+ "@types/node": "^25.5.0",
36
+ "prettier": "^3.8.1",
37
+ "remark": "^15.0.1",
38
+ "remark-parse": "^11.0.0",
39
+ "svgo": "^4.0.1",
40
+ "typescript": "^5.9.3",
41
+ "unified": "^11.0.5",
42
+ "vfile": "^6.0.3",
43
+ "vitest": "^4.1.0"
44
+ },
45
+ "dependencies": {
46
+ "unist-util-visit": "^5.1.0"
47
+ }
48
+ }