image-exporter 1.0.5 → 1.0.7

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.
@@ -777,7 +777,8 @@ async function toJpeg(node, options = {}) {
777
777
  function handleFileNames(imageOptions, filenames) {
778
778
  let proposedFilename = imageOptions.label;
779
779
  if (imageOptions.includeScaleInLabel) proposedFilename += `_@${imageOptions.scale}x`;
780
- proposedFilename += `.${imageOptions.format}`;
780
+ const extension = `.${imageOptions.format}`;
781
+ proposedFilename += extension;
781
782
  if (!filenames.includes(proposedFilename)) {
782
783
  filenames.push(proposedFilename);
783
784
  return proposedFilename;
@@ -787,18 +788,19 @@ function handleFileNames(imageOptions, filenames) {
787
788
  if (match) {
788
789
  const baseFilename = proposedFilename.replace(numberPattern, "");
789
790
  let counter = parseInt(match[1], 10);
790
- while (filenames.includes(`${baseFilename}-${counter}`)) {
791
+ while (filenames.includes(`${baseFilename}-${counter}${extension}`)) {
791
792
  counter++;
792
793
  }
793
- const newFilename = `${baseFilename}-${counter}`;
794
+ const newFilename = `${baseFilename}-${counter}${extension}`;
794
795
  filenames.push(newFilename);
795
796
  return newFilename;
796
797
  } else {
798
+ const baseFilename = proposedFilename.replace(extension, "");
797
799
  let counter = 2;
798
- while (filenames.includes(`${proposedFilename}-${counter}`)) {
800
+ while (filenames.includes(`${baseFilename}-${counter}${extension}`)) {
799
801
  counter++;
800
802
  }
801
- const newFilename = `${proposedFilename}-${counter}`;
803
+ const newFilename = `${baseFilename}-${counter}${extension}`;
802
804
  filenames.push(newFilename);
803
805
  return newFilename;
804
806
  }
@@ -3494,11 +3496,13 @@ async function getImageOptions(element, config) {
3494
3496
  try {
3495
3497
  const label = element.dataset.label || config.defaultImageLabel;
3496
3498
  if (label === "") return config.defaultImageLabel;
3497
- const endsWithSpecial = label.endsWith("@#x");
3499
+ const endsWithSpecial = /@\d+x$/.test(label);
3498
3500
  let cleanedLabel = label;
3499
3501
  const regex = /[^a-zA-Z0-9-_]/g;
3500
3502
  if (endsWithSpecial) {
3501
- cleanedLabel = label.slice(0, -3).replace(regex, "") + "@#x";
3503
+ const match = label.match(/@\d+x$/);
3504
+ if (!match) return config.defaultImageLabel;
3505
+ cleanedLabel = label.slice(0, -match[0].length).replace(regex, "") + match[0];
3502
3506
  } else {
3503
3507
  cleanedLabel = label.replace(regex, "");
3504
3508
  }
@@ -782,7 +782,8 @@
782
782
  function handleFileNames(imageOptions, filenames) {
783
783
  let proposedFilename = imageOptions.label;
784
784
  if (imageOptions.includeScaleInLabel) proposedFilename += `_@${imageOptions.scale}x`;
785
- proposedFilename += `.${imageOptions.format}`;
785
+ const extension = `.${imageOptions.format}`;
786
+ proposedFilename += extension;
786
787
  if (!filenames.includes(proposedFilename)) {
787
788
  filenames.push(proposedFilename);
788
789
  return proposedFilename;
@@ -792,18 +793,19 @@
792
793
  if (match) {
793
794
  const baseFilename = proposedFilename.replace(numberPattern, "");
794
795
  let counter = parseInt(match[1], 10);
795
- while (filenames.includes(`${baseFilename}-${counter}`)) {
796
+ while (filenames.includes(`${baseFilename}-${counter}${extension}`)) {
796
797
  counter++;
797
798
  }
798
- const newFilename = `${baseFilename}-${counter}`;
799
+ const newFilename = `${baseFilename}-${counter}${extension}`;
799
800
  filenames.push(newFilename);
800
801
  return newFilename;
801
802
  } else {
803
+ const baseFilename = proposedFilename.replace(extension, "");
802
804
  let counter = 2;
803
- while (filenames.includes(`${proposedFilename}-${counter}`)) {
805
+ while (filenames.includes(`${baseFilename}-${counter}${extension}`)) {
804
806
  counter++;
805
807
  }
806
- const newFilename = `${proposedFilename}-${counter}`;
808
+ const newFilename = `${baseFilename}-${counter}${extension}`;
807
809
  filenames.push(newFilename);
808
810
  return newFilename;
809
811
  }
@@ -3499,11 +3501,13 @@
3499
3501
  try {
3500
3502
  const label = element.dataset.label || config.defaultImageLabel;
3501
3503
  if (label === "") return config.defaultImageLabel;
3502
- const endsWithSpecial = label.endsWith("@#x");
3504
+ const endsWithSpecial = /@\d+x$/.test(label);
3503
3505
  let cleanedLabel = label;
3504
3506
  const regex = /[^a-zA-Z0-9-_]/g;
3505
3507
  if (endsWithSpecial) {
3506
- cleanedLabel = label.slice(0, -3).replace(regex, "") + "@#x";
3508
+ const match = label.match(/@\d+x$/);
3509
+ if (!match) return config.defaultImageLabel;
3510
+ cleanedLabel = label.slice(0, -match[0].length).replace(regex, "") + match[0];
3507
3511
  } else {
3508
3512
  cleanedLabel = label.replace(regex, "");
3509
3513
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "image-exporter",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "Easily download one or more DOM elements as images",
5
5
  "main": "src/index.ts",
6
6
  "type": "module",
@@ -56,14 +56,16 @@ export async function getImageOptions(
56
56
  if (label === "") return config.defaultImageLabel;
57
57
 
58
58
  // Check if the label ends with '@#x'
59
- const endsWithSpecial = label.endsWith("@#x");
59
+ const endsWithSpecial = /@\d+x$/.test(label);
60
60
  let cleanedLabel = label;
61
61
 
62
62
  // Allowed characters: a-z, A-Z, 0-9, -, _
63
- // Remove all other characters using regex, except '@#x' at the end
63
+ // Remove all other characters using regex, except '@Nx' at the end
64
64
  const regex = /[^a-zA-Z0-9-_]/g;
65
65
  if (endsWithSpecial) {
66
- cleanedLabel = label.slice(0, -3).replace(regex, "") + "@#x";
66
+ const match = label.match(/@\d+x$/);
67
+ if (!match) return config.defaultImageLabel;
68
+ cleanedLabel = label.slice(0, -match[0].length).replace(regex, "") + match[0];
67
69
  } else {
68
70
  cleanedLabel = label.replace(regex, "");
69
71
  }
@@ -9,12 +9,13 @@ import { ImageOptions, Label } from "../types";
9
9
  * If it doesn't, the function will start with "-2" and increment the number until a unique filename is found.
10
10
  */
11
11
  export function handleFileNames(imageOptions: ImageOptions, filenames: string[]): Label {
12
- // Finish alterting filenames before checking for uniqueness
12
+ // Finish altering filenames before checking for uniqueness
13
13
  let proposedFilename = imageOptions.label;
14
14
  // Add scale to filename if includeScaleInLabel is true
15
15
  if (imageOptions.includeScaleInLabel) proposedFilename += `_@${imageOptions.scale}x`;
16
16
  // Add format to filename last
17
- proposedFilename += `.${imageOptions.format}`;
17
+ const extension = `.${imageOptions.format}`;
18
+ proposedFilename += extension;
18
19
 
19
20
  // If filename is unique, add it to array and return as-is
20
21
  if (!filenames.includes(proposedFilename)) {
@@ -31,21 +32,22 @@ export function handleFileNames(imageOptions: ImageOptions, filenames: string[])
31
32
  const baseFilename = proposedFilename.replace(numberPattern, "");
32
33
  let counter = parseInt(match[1], 10);
33
34
 
34
- while (filenames.includes(`${baseFilename}-${counter}`)) {
35
+ while (filenames.includes(`${baseFilename}-${counter}${extension}`)) {
35
36
  counter++;
36
37
  }
37
38
 
38
- const newFilename = `${baseFilename}-${counter}`;
39
+ const newFilename = `${baseFilename}-${counter}${extension}`;
39
40
  filenames.push(newFilename);
40
41
  return newFilename;
41
42
  } else {
42
- // File doesn't end with -n, start with -1 and increment if needed
43
+ // File doesn't end with -n, start with -2 and increment if needed
44
+ const baseFilename = proposedFilename.replace(extension, "");
43
45
  let counter = 2;
44
- while (filenames.includes(`${proposedFilename}-${counter}`)) {
46
+ while (filenames.includes(`${baseFilename}-${counter}${extension}`)) {
45
47
  counter++;
46
48
  }
47
49
 
48
- const newFilename = `${proposedFilename}-${counter}`;
50
+ const newFilename = `${baseFilename}-${counter}${extension}`;
49
51
  filenames.push(newFilename);
50
52
  return newFilename;
51
53
  }