remark-inline-svg-flex 0.1.1 → 0.2.2

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.
@@ -0,0 +1 @@
1
+ export * from './plugin';
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./plugin"), exports);
@@ -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 };
@@ -0,0 +1,84 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.inlineSvg = void 0;
4
+ const node_path_1 = require("node:path");
5
+ const node_fs_1 = require("node:fs");
6
+ const unist_util_visit_1 = require("unist-util-visit");
7
+ const svgo_1 = require("svgo");
8
+ /**
9
+ * Transforms image nodes that match the given suffix (default: .svg) into inline SVG.
10
+ *
11
+ * @param options - Plugin configuration
12
+ * @param options.suffix - File extension to match (default: '.svg')
13
+ * @param options.assetsDir - Optional base directory for resolving SVG paths (default: undefined)
14
+ * @param options.wrapper - Optional HTML wrapper for the inline SVG (default: `<figure class="inline-svg"></figure>`)
15
+ * @param options.svgo - Whether to optimize SVG using SVGO (default: true)
16
+ */
17
+ const inlineSvg = (consumerOptions = {}) => {
18
+ const options = {
19
+ suffix: consumerOptions.suffix ?? '.svg',
20
+ assetsDir: consumerOptions.assetsDir,
21
+ wrapper: consumerOptions.wrapper ?? '<figure class="inline-svg"></figure>',
22
+ svgo: consumerOptions.svgo ?? true,
23
+ };
24
+ return function transformer(tree, file) {
25
+ (0, unist_util_visit_1.visit)(tree, 'image', (node, i, parent) => {
26
+ if (!node.url?.endsWith(options.suffix) || !parent)
27
+ return;
28
+ try {
29
+ const svgPath = resolvePath(options.assetsDir, node, node_path_1.default.dirname(file.history[0]));
30
+ const svgString = processSvg(svgPath, options.svgo);
31
+ parent.children[i] = {
32
+ type: 'html',
33
+ value: options.wrapper ? wrap(svgString, options.wrapper) : svgString,
34
+ };
35
+ }
36
+ catch (error) {
37
+ console.warn(error);
38
+ }
39
+ });
40
+ };
41
+ };
42
+ exports.inlineSvg = inlineSvg;
43
+ /**
44
+ * Reads an SVG file and optionally optimizes it with SVGO.
45
+ */
46
+ function processSvg(path, svgo) {
47
+ const svgString = node_fs_1.default.readFileSync(path, 'utf8');
48
+ return svgo ? optimizeSvg(svgString).data : svgString;
49
+ }
50
+ /**
51
+ * Injects SVG markup into an HTML wrapper before its closing tag.
52
+ */
53
+ function wrap(svgString, htmlWrapper) {
54
+ const i = htmlWrapper.lastIndexOf('</');
55
+ if (i === -1) {
56
+ throw new Error(`Invalid HTML wrapper`);
57
+ }
58
+ return htmlWrapper.slice(0, i) + svgString + htmlWrapper.slice(i);
59
+ }
60
+ /**
61
+ * Resolves the final SVG file path based on:
62
+ * 1) absolute URL → from project root
63
+ * 2) `assetsDir` → relative to it
64
+ * 3) fallback → relative to Markdown file directory
65
+ */
66
+ function resolvePath(assetsDir, node, markdownFileDir) {
67
+ if (node_path_1.default.isAbsolute(node.url)) {
68
+ return node_path_1.default.resolve(process.cwd(), node.url);
69
+ }
70
+ else if (assetsDir) {
71
+ return node_path_1.default.resolve(process.cwd(), assetsDir, node.url);
72
+ }
73
+ else {
74
+ return node_path_1.default.resolve(markdownFileDir, node.url);
75
+ }
76
+ }
77
+ /**
78
+ * Optimizes SVG content using predefined SVGO plugins.
79
+ */
80
+ function optimizeSvg(svgString) {
81
+ return (0, svgo_1.optimize)(svgString, {
82
+ plugins: ['preset-default', 'removeXMLNS', 'removeDimensions'],
83
+ });
84
+ }
@@ -0,0 +1,6 @@
1
+ export type Options = {
2
+ suffix?: string;
3
+ assetsDir?: string | undefined;
4
+ wrapper?: string;
5
+ svgo?: boolean;
6
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,6 @@
1
+ export type Options = {
2
+ suffix?: string;
3
+ assetsDir?: string | undefined;
4
+ wrapper?: string;
5
+ svgo?: boolean;
6
+ };
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "remark-inline-svg-flex",
3
- "version": "0.1.1",
3
+ "version": "0.2.2",
4
4
  "description": "Flexible Remark plugin that inlines and optimizes SVGs with SVGO, featuring customizable path resolution and wrappers.",
5
5
  "keywords": [
6
6
  "unified",
@@ -24,9 +24,18 @@
24
24
  "license": "MIT",
25
25
  "author": "gass-git",
26
26
  "type": "module",
27
- "main": "index.js",
27
+ "main": "./dist/cjs/index.js",
28
+ "module": "./dist/index.js",
29
+ "types": "./dist/index.d.ts",
30
+ "exports": {
31
+ ".": {
32
+ "import": "./dist/index.js",
33
+ "require": "./dist/cjs/index.js",
34
+ "types": "./dist/index.d.ts"
35
+ }
36
+ },
28
37
  "scripts": {
29
- "build": "tsc",
38
+ "build": "rimraf dist && tsc -p tsconfig.esm.json && tsc -p tsconfig.cjs.json",
30
39
  "dev": "rimraf --glob \"*.tgz\" && npm run build && npm pack && npm link",
31
40
  "tests": "vitest"
32
41
  },
@@ -36,13 +45,14 @@
36
45
  "prettier": "^3.8.1",
37
46
  "remark": "^15.0.1",
38
47
  "remark-parse": "^11.0.0",
39
- "svgo": "^4.0.1",
48
+ "rimraf": "^6.1.3",
40
49
  "typescript": "^5.9.3",
41
50
  "unified": "^11.0.5",
42
51
  "vfile": "^6.0.3",
43
52
  "vitest": "^4.1.0"
44
53
  },
45
54
  "dependencies": {
55
+ "svgo": "^4.0.1",
46
56
  "unist-util-visit": "^5.1.0"
47
57
  }
48
58
  }