@riebeckite/plugin-responsive-image 0.0.5 → 0.0.6

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 CHANGED
@@ -87,3 +87,7 @@ commit them next to the original image. Sites copy referenced vault assets into
87
87
  - `collectKnownAssetPaths(manifest)` — manifest asset set helper
88
88
  - Types: `ResponsiveImageOptions`, `ResolvedResponsiveImageOptions`,
89
89
  `ResponsiveImagePlan`, `ResponsiveImageSource`, `ResponsiveImageVariant`
90
+
91
+ ## See also
92
+
93
+ - [Plugin guide](../../../docs/en/plugin-system.md)
package/README_ja.md CHANGED
@@ -67,3 +67,7 @@ export default defineConfig({
67
67
  - `applyResponsiveImages(html, existingPaths, options?)` — HTML 変換
68
68
  - `collectKnownAssetPaths(manifest)` — マニフェストのアセット集合を取得
69
69
  - 型: `ResponsiveImageOptions`、`ResolvedResponsiveImageOptions`、`ResponsiveImagePlan`、`ResponsiveImageSource`、`ResponsiveImageVariant`
70
+
71
+ ## ????
72
+
73
+ - [?????????](../../../docs/ja/plugin-system.md)
package/dist/index.d.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  import { type ResponsiveImageOptions } from "./src/options.js";
2
+ export { applyResponsiveImages } from "./src/html.js";
2
3
  export type { ResolvedResponsiveImageOptions, ResponsiveImageOptions, } from "./src/options.js";
3
4
  export { DEFAULT_RESPONSIVE_IMAGE_CLASS, DEFAULT_RESPONSIVE_IMAGE_FORMATS, DEFAULT_RESPONSIVE_IMAGE_SIZES, DEFAULT_RESPONSIVE_IMAGE_WIDTHS, resolveResponsiveImageOptions, } from "./src/options.js";
4
- export type { ResponsiveImagePlan, ResponsiveImageSource, ResponsiveImageVariant, } from "./src/types.js";
5
- export { buildResponsiveSrcset, collectKnownAssetPaths } from "./src/srcset.js";
6
5
  export { rehypeResponsiveImage } from "./src/rehype.js";
7
- export { applyResponsiveImages } from "./src/html.js";
6
+ export { buildResponsiveSrcset, collectKnownAssetPaths } from "./src/srcset.js";
7
+ export type { ResponsiveImagePlan, ResponsiveImageSource, ResponsiveImageVariant, } from "./src/types.js";
8
8
  /**
9
9
  * Build-time responsive image layer.
10
10
  *
package/dist/index.js CHANGED
@@ -92,10 +92,74 @@ function isFormatList(value) {
92
92
  return Array.isArray(value) && value.every((item) => typeof item === "string" && item.trim() !== "");
93
93
  }
94
94
 
95
+ // src/hast.ts
96
+ function visitElements(node, visitor, parent, index) {
97
+ if (isElementNode(node)) {
98
+ visitor(node, parent, index);
99
+ }
100
+ for (const [childIndex, child] of [...getChildren(node)].entries()) {
101
+ visitElements(
102
+ child,
103
+ visitor,
104
+ isElementNode(node) ? node : parent,
105
+ childIndex
106
+ );
107
+ }
108
+ }
109
+ function isElementNode(node) {
110
+ return node.type === "element";
111
+ }
112
+ function getStringProperty(node, key) {
113
+ const value = node.properties?.[key];
114
+ if (typeof value === "string") return value;
115
+ if (Array.isArray(value)) return value.join(" ");
116
+ return null;
117
+ }
118
+ function hasProperty(node, key) {
119
+ return node.properties?.[key] !== void 0;
120
+ }
121
+ function ensureProperty(node, key, value) {
122
+ if (!node.properties) node.properties = {};
123
+ if (key in node.properties) return;
124
+ node.properties[key] = value;
125
+ }
126
+ function getChildren(node) {
127
+ const children = node.children;
128
+ return Array.isArray(children) ? children : [];
129
+ }
130
+
131
+ // src/rehype.ts
132
+ var RESPONSIVE_IMAGE_MARKER = "data-responsive-image";
133
+ var RESPONSIVE_IMAGE_SOURCE = "@riebeckite/plugin-responsive-image";
134
+ var MARKER_PROPERTY = "dataResponsiveImage";
135
+ function rehypeResponsiveImage(options = {}) {
136
+ const resolved = resolveResponsiveImageOptions(options);
137
+ return (tree, file) => {
138
+ visitElements(tree, (node) => {
139
+ if (node.tagName !== "img") return;
140
+ if (hasProperty(node, MARKER_PROPERTY)) return;
141
+ const src = getStringProperty(node, "src");
142
+ if (!src || src.trim() === "") {
143
+ warnMalformed(
144
+ file,
145
+ 'An <img> element is missing a usable "src".'
146
+ );
147
+ }
148
+ if (resolved.lazy) ensureProperty(node, "loading", "lazy");
149
+ if (resolved.decoding) ensureProperty(node, "decoding", "async");
150
+ ensureProperty(node, "sizes", resolved.sizes);
151
+ if (!node.properties) node.properties = {};
152
+ node.properties[MARKER_PROPERTY] = "";
153
+ });
154
+ };
155
+ }
156
+ function warnMalformed(file, message) {
157
+ if (typeof file?.message !== "function") return;
158
+ file.message(message, { source: RESPONSIVE_IMAGE_SOURCE });
159
+ }
160
+
95
161
  // src/srcset.ts
96
- import {
97
- attachmentUrl
98
- } from "@riebeckite/core";
162
+ import { attachmentUrl } from "@riebeckite/core";
99
163
  var FORMAT_MIME_TYPES = {
100
164
  avif: "image/avif",
101
165
  bmp: "image/bmp",
@@ -125,7 +189,9 @@ function buildResponsiveSrcset(existingPaths, src, options = {}) {
125
189
  const sources = [];
126
190
  for (const format of resolved.formats) {
127
191
  if (format === split.extension) continue;
128
- const widthVariants = variants.filter((variant) => variant.format === format && variant.width !== void 0).toSorted((a, b) => (a.width ?? 0) - (b.width ?? 0));
192
+ const widthVariants = variants.filter(
193
+ (variant) => variant.format === format && variant.width !== void 0
194
+ ).toSorted((a, b) => (a.width ?? 0) - (b.width ?? 0));
129
195
  if (widthVariants.length > 0) {
130
196
  sources.push({
131
197
  type: mimeTypeFor(format),
@@ -230,72 +296,6 @@ function encodeAssetPath(path) {
230
296
  return path.split("/").map(encodeURIComponent).join("/");
231
297
  }
232
298
 
233
- // src/hast.ts
234
- function visitElements(node, visitor, parent, index) {
235
- if (isElementNode(node)) {
236
- visitor(node, parent, index);
237
- }
238
- for (const [childIndex, child] of [...getChildren(node)].entries()) {
239
- visitElements(
240
- child,
241
- visitor,
242
- isElementNode(node) ? node : parent,
243
- childIndex
244
- );
245
- }
246
- }
247
- function isElementNode(node) {
248
- return node.type === "element";
249
- }
250
- function getStringProperty(node, key) {
251
- const value = node.properties?.[key];
252
- if (typeof value === "string") return value;
253
- if (Array.isArray(value)) return value.join(" ");
254
- return null;
255
- }
256
- function hasProperty(node, key) {
257
- return node.properties?.[key] !== void 0;
258
- }
259
- function ensureProperty(node, key, value) {
260
- if (!node.properties) node.properties = {};
261
- if (key in node.properties) return;
262
- node.properties[key] = value;
263
- }
264
- function getChildren(node) {
265
- const children = node.children;
266
- return Array.isArray(children) ? children : [];
267
- }
268
-
269
- // src/rehype.ts
270
- var RESPONSIVE_IMAGE_MARKER = "data-responsive-image";
271
- var RESPONSIVE_IMAGE_SOURCE = "@riebeckite/plugin-responsive-image";
272
- var MARKER_PROPERTY = "dataResponsiveImage";
273
- function rehypeResponsiveImage(options = {}) {
274
- const resolved = resolveResponsiveImageOptions(options);
275
- return (tree, file) => {
276
- visitElements(tree, (node) => {
277
- if (node.tagName !== "img") return;
278
- if (hasProperty(node, MARKER_PROPERTY)) return;
279
- const src = getStringProperty(node, "src");
280
- if (!src || src.trim() === "") {
281
- warnMalformed(
282
- file,
283
- 'An <img> element is missing a usable "src".'
284
- );
285
- }
286
- if (resolved.lazy) ensureProperty(node, "loading", "lazy");
287
- if (resolved.decoding) ensureProperty(node, "decoding", "async");
288
- ensureProperty(node, "sizes", resolved.sizes);
289
- if (!node.properties) node.properties = {};
290
- node.properties[MARKER_PROPERTY] = "";
291
- });
292
- };
293
- }
294
- function warnMalformed(file, message) {
295
- if (typeof file?.message !== "function") return;
296
- file.message(message, { source: RESPONSIVE_IMAGE_SOURCE });
297
- }
298
-
299
299
  // src/html.ts
300
300
  var IMG_TAG_PATTERN = /<img\b(?:[^>"']|"[^"]*"|'[^']*')*>/gi;
301
301
  var ATTRIBUTE_PATTERN = /([^\s"'=<>`/]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+)))?/g;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@riebeckite/plugin-responsive-image",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -15,7 +15,11 @@
15
15
  "./style.css": "./style.css"
16
16
  },
17
17
  "dependencies": {
18
- "@riebeckite/core": "0.0.5"
18
+ "@riebeckite/core": "0.0.6"
19
+ },
20
+ "devDependencies": {
21
+ "tsx": "^4.22.4",
22
+ "@riebeckite/test": "0.0.6"
19
23
  },
20
24
  "publishConfig": {
21
25
  "access": "public"
@@ -50,6 +54,7 @@
50
54
  "./style.css"
51
55
  ],
52
56
  "scripts": {
53
- "build": "node ../../../scripts/build_package.mjs"
57
+ "build": "node ../../../scripts/build_package.mjs",
58
+ "test": "node --import tsx --test \"test/*.test.ts\""
54
59
  }
55
60
  }
package/style.css CHANGED
@@ -1,11 +1,11 @@
1
- .rb-responsive-image {
2
- display: block;
3
- max-width: 100%;
4
- }
5
-
6
- .rb-responsive-image > img {
7
- display: block;
8
- width: 100%;
9
- max-width: 100%;
10
- height: auto;
11
- }
1
+ .rb-responsive-image {
2
+ display: block;
3
+ max-width: 100%;
4
+ }
5
+
6
+ .rb-responsive-image > img {
7
+ display: block;
8
+ width: 100%;
9
+ max-width: 100%;
10
+ height: auto;
11
+ }