@jayalfredprufrock/ansel 0.1.0 → 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.
Files changed (2) hide show
  1. package/dist/vite.mjs +15 -6
  2. package/package.json +1 -1
package/dist/vite.mjs CHANGED
@@ -40,9 +40,11 @@ async function scanImages(dir) {
40
40
  if (!IMAGE_EXTENSIONS.includes(extension)) continue;
41
41
  const name = file.slice(0, dot);
42
42
  if (!name.split("/").every((segment) => NAME_SEGMENT.test(segment))) {
43
+ const suggestion = suggestName(name);
43
44
  invalid.push({
44
45
  file,
45
- reason: `use lowercase letters, digits and dashes, e.g. "${suggestName(name)}${extension}"`
46
+ reason: `use lowercase letters, digits and dashes, e.g. "${suggestion}${extension}"`,
47
+ suggestion
46
48
  });
47
49
  continue;
48
50
  }
@@ -66,6 +68,12 @@ async function scanImages(dir) {
66
68
  invalid
67
69
  };
68
70
  }
71
+ /** Camera, phone and stock-site default names, and names that are mostly a long number. */
72
+ const POOR_NAME = /^(img|dsc[nf]?|pxl|mvimg|gopr|dji|screenshot|screen-shot|untitled|image|photo|pexels-photo|pexels|unsplash|shutterstock|adobestock|istock|istockphoto|gettyimages|depositphotos)(-|$)|\d{5,}|^\d+$/;
73
+ /** Whether a valid image name's last segment is a camera or stock default (`team/img-4821`). */
74
+ function isPoorName(name) {
75
+ return POOR_NAME.test(name.split("/").pop() ?? name);
76
+ }
69
77
  /** A valid name close to `name`: `Dr Patel (Portrait)` → `dr-patel-portrait`. */
70
78
  function suggestName(name) {
71
79
  return name.split("/").map((segment) => segment.normalize("NFKD").replace(/[̀-ͯ]/g, "").toLowerCase().replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "") || "image").join("/");
@@ -275,8 +283,7 @@ const VIRTUAL_ALL = "virtual:ansel";
275
283
  const RESOLVED_ALL = `\0${VIRTUAL_ALL}`;
276
284
  const QUERY = "?ansel";
277
285
  const RESOLVED_ONE = "\0ansel:";
278
- /** Camera, phone and stock-site default names, and names that are mostly a long number. */
279
- const POOR_NAME = /^(img|dsc[nf]?|pxl|mvimg|gopr|dji|screenshot|screen-shot|untitled|image|photo|pexels-photo|pexels|unsplash|shutterstock|adobestock|istock|istockphoto|gettyimages|depositphotos)(-|$)|\d{5,}|^\d+$/;
286
+ const POOR_NAME_PROBLEM = "the name doesn't say what the image shows; rename it in lowercase letters, digits and dashes, e.g. \"team/dr-patel.jpg\"";
280
287
  /**
281
288
  * Keeps the image manifest and types up to date, provides image data to `<Img>`, and serves
282
289
  * `/_img/*` locally in dev with the same output as the optimizer.
@@ -293,7 +300,10 @@ function ansel(options = {}) {
293
300
  const problems = [];
294
301
  const result = await syncManifest(dir, await readManifest(dir));
295
302
  const { manifest } = result;
296
- for (const { file, reason } of result.invalid) problems.push(`${file}: ${reason}`);
303
+ for (const { file, reason, suggestion } of result.invalid) {
304
+ const poor = options.rejectPoorNames && suggestion !== void 0 && isPoorName(suggestion);
305
+ problems.push(`${file}: ${poor ? POOR_NAME_PROBLEM : reason}`);
306
+ }
297
307
  const entries = await loadEntries();
298
308
  const manifestPath = join(dir, MANIFEST_FILE);
299
309
  const typesPath = join(dir, TYPES_FILE);
@@ -333,8 +343,7 @@ function ansel(options = {}) {
333
343
  }
334
344
  function checkImage(name, data) {
335
345
  const problems = [];
336
- const base = name.split("/").pop() ?? name;
337
- if (options.rejectPoorNames && POOR_NAME.test(base)) problems.push(`${data.file}: the name doesn't say what the image shows; rename it, e.g. "team/dr-patel"`);
346
+ if (options.rejectPoorNames && isPoorName(name)) problems.push(`${data.file}: ${POOR_NAME_PROBLEM}`);
338
347
  if (options.rejectGps && data.gps) problems.push(`${data.file}: contains GPS location data; strip it before adding the image`);
339
348
  if (options.maxFileSize && data.bytes > options.maxFileSize) problems.push(`${data.file}: ${formatBytes(data.bytes)} is over the ${formatBytes(options.maxFileSize)} limit`);
340
349
  if (options.maxPixels && data.width * data.height > options.maxPixels) problems.push(`${data.file}: ${data.width}×${data.height} is over the ${options.maxPixels.toLocaleString("en")} pixel limit`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jayalfredprufrock/ansel",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Responsive images for Vite, Astro and React: a typed image manifest, build-time placeholders, an <Img> component and a shared URL format for an image optimizer",
5
5
  "homepage": "https://github.com/jayalfredprufrock/ansel#readme",
6
6
  "bugs": {