@libsrcdev/gatsby-remark-images-anywhere 0.1.6 → 0.1.8

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 (3) hide show
  1. package/dist/index.d.ts +14 -24
  2. package/dist/index.js +45 -11
  3. package/package.json +79 -70
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Node, NodePluginArgs } from "gatsby";
1
+ import { Node, NodePluginArgs, Reporter } from "gatsby";
2
2
  import { FileSystemNode } from "gatsby-source-filesystem";
3
3
  import { Literal, Literal as RemarkLiteral, Node as RemarkNode } from "mdast";
4
4
 
@@ -55,6 +55,15 @@ interface Options extends Partial<MarkupOptions>, HttpRequestHeaderOptions {
55
55
  [key: string]: unknown;
56
56
  }
57
57
  //#endregion
58
+ //#region src/util-html-to-md.d.ts
59
+ type RemarkImageNode = RemarkLiteral & {
60
+ url?: string;
61
+ title?: string;
62
+ alt?: string;
63
+ data: Record<string, any>;
64
+ };
65
+ declare const toMdNode: (node: RemarkLiteral) => RemarkImageNode | null;
66
+ //#endregion
58
67
  //#region src/constants.d.ts
59
68
  declare const CLASS_WRAPPER = "gria-image-wrapper";
60
69
  declare const CLASS_PADDING = "gria-image-padding";
@@ -113,15 +122,6 @@ declare const processImage: ({
113
122
  [key: string]: any;
114
123
  }) => Promise<SharpResult>;
115
124
  //#endregion
116
- //#region src/util-html-to-md.d.ts
117
- type RemarkImageNode = RemarkLiteral & {
118
- url?: string;
119
- title?: string;
120
- alt?: string;
121
- data: Record<string, any>;
122
- };
123
- declare const toMdNode: (node: RemarkLiteral) => RemarkImageNode | null;
124
- //#endregion
125
125
  //#region src/utils.d.ts
126
126
  /**
127
127
  * Resolves a potentially incomplete URL to a full URL with protocol
@@ -137,18 +137,8 @@ declare function resolveFullUrl(url: string): string | undefined;
137
137
  declare function resolveRelativeUrl(url: string): string | undefined;
138
138
  //#endregion
139
139
  //#region src/index.d.ts
140
- declare function remarkImagesAnywhere({
141
- markdownAST: mdast,
142
- markdownNode,
143
- actions,
144
- store,
145
- files,
146
- getNode,
147
- getCache,
148
- createNodeId,
149
- reporter,
150
- cache,
151
- pathPrefix
152
- }: Args, pluginOptions: Options): Promise<(null | undefined)[]>;
140
+ declare let localReporter: Reporter;
141
+ declare function extractAllImgNodesFromMdast(mdast: RemarkNode): RemarkImageNode[];
142
+ declare function remarkImagesAnywhere(gatsbyApis: Args, pluginOptions: Options): Promise<(null | undefined)[]>;
153
143
  //#endregion
154
- export { Args, CLASS_IMAGE, CLASS_LINK, CLASS_PADDING, CLASS_PLACEHOLDER, CLASS_WRAPPER, CreateMarkup, CreateMarkupArgs, HttpRequestHeaderOptions, HttpRequestHeaderProvider, MarkupOptions, Options, RemarkImageNode, type RemarkLiteral, type RemarkNode, SUPPORT_EXTS, SharpMethod, SharpResult, UrlCheckOptions, buildRequestHttpHeadersWith, remarkImagesAnywhere as default, defaultMarkup, downloadImage, forTrustedDomains, httpHeaderTrustedProvider, isTrustedUrl, isWhitelisted, makeWhitelistTest, processImage, resolveFullUrl, resolveRelativeUrl, toMdNode };
144
+ export { Args, CLASS_IMAGE, CLASS_LINK, CLASS_PADDING, CLASS_PLACEHOLDER, CLASS_WRAPPER, CreateMarkup, CreateMarkupArgs, HttpRequestHeaderOptions, HttpRequestHeaderProvider, MarkupOptions, Options, RemarkImageNode, type RemarkLiteral, type RemarkNode, SUPPORT_EXTS, SharpMethod, SharpResult, UrlCheckOptions, buildRequestHttpHeadersWith, remarkImagesAnywhere as default, defaultMarkup, downloadImage, extractAllImgNodesFromMdast, forTrustedDomains, httpHeaderTrustedProvider, isTrustedUrl, isWhitelisted, localReporter, makeWhitelistTest, processImage, resolveFullUrl, resolveRelativeUrl, toMdNode };
package/dist/index.js CHANGED
@@ -48,6 +48,7 @@ const downloadImage = async ({ id, url, getCache, getNode, touchNode, cache, cre
48
48
  const fileNode = getNode(fileNodeId);
49
49
  if (fileNode) {
50
50
  touchNode({ nodeId: fileNodeId });
51
+ reporter.verbose(`[gria] Using cached image for: ${url}`);
51
52
  imageFileNode = fileNode;
52
53
  }
53
54
  }
@@ -64,10 +65,11 @@ const downloadImage = async ({ id, url, getCache, getNode, touchNode, cache, cre
64
65
  });
65
66
  if (fileNode) {
66
67
  imageFileNode = fileNode;
68
+ reporter.verbose(`[gria] Downloaded and cached remote image: ${url}`);
67
69
  await cache.set(mediaDataCacheKey, { fileNodeId: fileNode.id });
68
70
  }
69
71
  } catch (e) {
70
- reporter.warn(`failed to download ${url} Error: ${e}`);
72
+ reporter.warn(`[gria] Failed to download ${url}: ${e}`);
71
73
  }
72
74
  return imageFileNode;
73
75
  };
@@ -341,7 +343,17 @@ function resolveRelativeUrl(url) {
341
343
 
342
344
  //#endregion
343
345
  //#region src/index.ts
344
- async function remarkImagesAnywhere({ markdownAST: mdast, markdownNode, actions, store, files, getNode, getCache, createNodeId, reporter, cache, pathPrefix }, pluginOptions) {
346
+ let localReporter;
347
+ function extractAllImgNodesFromMdast(mdast) {
348
+ const imgNodes = (0, unist_util_select.selectAll)("image[url]", mdast).filter((node) => "url" in node);
349
+ const htmlImgNodes = (0, unist_util_select.selectAll)("html, jsx", mdast).filter((node) => "value" in node).map((node, _, __) => toMdNode(node)).filter((node, _, __) => !!node);
350
+ const allImgNodes = [...imgNodes, ...htmlImgNodes];
351
+ if (localReporter) localReporter.info(`[gria] Processing ${allImgNodes.length} image(s) (${imgNodes.length} markdown, ${htmlImgNodes.length} html)`);
352
+ return allImgNodes;
353
+ }
354
+ async function remarkImagesAnywhere(gatsbyApis, pluginOptions) {
355
+ const { markdownAST: mdast, markdownNode, actions, store, files, getNode, getCache, createNodeId, reporter, cache, pathPrefix } = gatsbyApis;
356
+ localReporter = reporter;
345
357
  const { plugins, staticDir = "static", createMarkup = defaultMarkup, sharpMethod = "fluid", loading = "lazy", linkImagesToOriginal = false, showCaptions = false, wrapperStyle = "", backgroundColor = "#fff", tracedSVG = false, blurUp = true, dangerouslyBuildRequestHttpHeaders, httpHeaderProviders = [], ...imageOptions } = pluginOptions;
346
358
  if ([
347
359
  "fluid",
@@ -351,17 +363,18 @@ async function remarkImagesAnywhere({ markdownAST: mdast, markdownNode, actions,
351
363
  const { touchNode, createNode } = actions;
352
364
  const dirPath = markdownNode.parent && getNode(markdownNode.parent)?.dir;
353
365
  const { directory } = store.getState().program;
354
- const imgNodes = (0, unist_util_select.selectAll)("image[url]", mdast).filter((node) => "src" in node);
355
- const htmlImgNodes = (0, unist_util_select.selectAll)("html, jsx", mdast).filter((node) => "value" in node).map((node, _, __) => toMdNode(node)).filter((node, _, __) => !!node);
356
- imgNodes.push(...htmlImgNodes);
357
- const processPromises = [...imgNodes, ...htmlImgNodes].map(async (node) => {
366
+ const processPromises = extractAllImgNodesFromMdast(mdast).map(async (node) => {
358
367
  if (!node.url) return;
359
368
  let url = node.url;
360
369
  let gImgFileNode;
361
- if (isWhitelisted(url)) url = `https:${url}`;
370
+ if (isWhitelisted(url)) {
371
+ reporter.verbose(`[gria] Whitelisted protocol-relative URL, prepending https: ${url}`);
372
+ url = `https:${url}`;
373
+ }
362
374
  const remoteFullImageUrl = resolveFullUrl(url);
363
375
  const relativeImageUrl = resolveRelativeUrl(url);
364
376
  if (remoteFullImageUrl) {
377
+ reporter.verbose(`[gria] Downloading remote image: ${remoteFullImageUrl}`);
365
378
  const buildRequestHttpHeaders = dangerouslyBuildRequestHttpHeaders ?? buildRequestHttpHeadersWith(httpHeaderProviders);
366
379
  gImgFileNode = await downloadImage({
367
380
  id: markdownNode.id,
@@ -379,10 +392,20 @@ async function remarkImagesAnywhere({ markdownAST: mdast, markdownNode, actions,
379
392
  let filePath;
380
393
  if (dirPath && url[0] === ".") filePath = (0, slash.default)(path.default.join(dirPath, url));
381
394
  else filePath = path.default.join(directory, staticDir, url);
395
+ reporter.verbose(`[gria] Resolving local image: ${url} -> ${filePath}`);
382
396
  gImgFileNode = files.find((fileNode) => fileNode.absolutePath && fileNode.absolutePath === filePath);
383
- } else reporter.warn(`Skipping invalid image URL ${url}`);
384
- if (!gImgFileNode) return;
385
- if (!SUPPORT_EXTS.includes(gImgFileNode.extension)) return;
397
+ } else {
398
+ reporter.warn(`[gria] Skipping unrecognized image URL: ${url}`);
399
+ return;
400
+ }
401
+ if (!gImgFileNode) {
402
+ reporter.verbose(`[gria] No file node found for: ${url}`);
403
+ return;
404
+ }
405
+ if (!SUPPORT_EXTS.includes(gImgFileNode.extension)) {
406
+ reporter.verbose(`[gria] Unsupported extension "${gImgFileNode.extension}" for: ${url}`);
407
+ return;
408
+ }
386
409
  const imageResult = await processImage({
387
410
  file: gImgFileNode,
388
411
  reporter,
@@ -391,7 +414,11 @@ async function remarkImagesAnywhere({ markdownAST: mdast, markdownNode, actions,
391
414
  sharpMethod,
392
415
  imageOptions
393
416
  });
394
- if (!imageResult) return;
417
+ if (!imageResult) {
418
+ reporter.warn(`[gria] Sharp processing returned no result for: ${url}`);
419
+ return;
420
+ }
421
+ reporter.verbose(`[gria] Successfully processed image: ${url}`);
395
422
  const data = {
396
423
  title: node.title,
397
424
  alt: node.alt,
@@ -425,10 +452,17 @@ exports.buildRequestHttpHeadersWith = buildRequestHttpHeadersWith;
425
452
  exports.default = remarkImagesAnywhere;
426
453
  exports.defaultMarkup = defaultMarkup;
427
454
  exports.downloadImage = downloadImage;
455
+ exports.extractAllImgNodesFromMdast = extractAllImgNodesFromMdast;
428
456
  exports.forTrustedDomains = forTrustedDomains;
429
457
  exports.httpHeaderTrustedProvider = httpHeaderTrustedProvider;
430
458
  exports.isTrustedUrl = isTrustedUrl;
431
459
  exports.isWhitelisted = isWhitelisted;
460
+ Object.defineProperty(exports, 'localReporter', {
461
+ enumerable: true,
462
+ get: function () {
463
+ return localReporter;
464
+ }
465
+ });
432
466
  exports.makeWhitelistTest = makeWhitelistTest;
433
467
  exports.processImage = processImage;
434
468
  exports.resolveFullUrl = resolveFullUrl;
package/package.json CHANGED
@@ -1,71 +1,80 @@
1
1
  {
2
- "name": "@libsrcdev/gatsby-remark-images-anywhere",
3
- "version": "0.1.6",
4
- "description": "Handle images with relative, absolute, remote path for gatsby-transformer-remark.",
5
- "keywords": [
6
- "gatsby",
7
- "gatsby-plugin",
8
- "transformer",
9
- "remark",
10
- "images",
11
- "anywhere",
12
- "fork",
13
- "v5",
14
- "remark-images"
15
- ],
16
- "homepage": "https://github.com/libsrcdev/gatsby-remark-images-anywhere",
17
- "bugs": {
18
- "url": "https://github.com/libsrcdev/gatsby-remark-images-anywhere/issues"
19
- },
20
- "repository": {
21
- "type": "git",
22
- "url": "git+https://github.com/libsrcdev/gatsby-remark-images-anywhere.git"
23
- },
24
- "license": "MIT",
25
- "author": "Derek Nguyen <derek@penandpillow.com>, @libsrcdev <git@libsrc.dev>",
26
- "main": "./dist/index.js",
27
- "files": [
28
- "dist"
29
- ],
30
- "scripts": {
31
- "dev": "nodemon --watch src --ext js,ts,mjs,cjs,json --exec \"tsdown\"",
32
- "build": "tsdown",
33
- "lint": "eslint . --ext .ts",
34
- "test": "vitest",
35
- "prepare": "npm run build"
36
- },
37
- "dependencies": {
38
- "is-relative-url": "^4.1.0",
39
- "parse5": "^8.0.0",
40
- "slash": "^3.0.0",
41
- "unist-util-select": "^5.1.0"
42
- },
43
- "devDependencies": {
44
- "@swc/core": "^1.15.3",
45
- "@types/fs-extra": "^8.0.0",
46
- "@types/mdast": "^4.0.4",
47
- "eslint": "^9.39.1",
48
- "gatsby": "^5.15.0",
49
- "gatsby-plugin-sharp": "^5.15.0",
50
- "gatsby-source-filesystem": "^5.15.0",
51
- "husky": "^9.1.7",
52
- "mdast": "^2.3.2",
53
- "nodemon": "^3.1.11",
54
- "tap": "^14.9.2",
55
- "tsdown": "^0.16.6",
56
- "tslib": "^2.8.1",
57
- "typescript": "^5.9.3"
58
- },
59
- "peerDependencies": {
60
- "gatsby": "^5.15.0",
61
- "gatsby-source-filesystem": "^5.15.0",
62
- "gatsby-plugin-sharp": "^5.15.0"
63
- },
64
- "typings": "dist/index.d.ts",
65
- "prettier": {
66
- "printWidth": 80,
67
- "semi": true,
68
- "singleQuote": true,
69
- "trailingComma": "es5"
70
- }
71
- }
2
+ "name": "@libsrcdev/gatsby-remark-images-anywhere",
3
+ "version": "0.1.8",
4
+ "description": "Handle images with relative, absolute, remote path for gatsby-transformer-remark.",
5
+ "keywords": [
6
+ "gatsby",
7
+ "gatsby-plugin",
8
+ "transformer",
9
+ "remark",
10
+ "images",
11
+ "anywhere",
12
+ "fork",
13
+ "v5",
14
+ "remark-images"
15
+ ],
16
+ "homepage": "https://github.com/libsrcdev/gatsby-remark-images-anywhere",
17
+ "bugs": {
18
+ "url": "https://github.com/libsrcdev/gatsby-remark-images-anywhere/issues"
19
+ },
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "git+https://github.com/libsrcdev/gatsby-remark-images-anywhere.git"
23
+ },
24
+ "license": "MIT",
25
+ "author": "Derek Nguyen <derek@penandpillow.com>, @libsrcdev <git@libsrc.dev>",
26
+ "main": "./dist/index.js",
27
+ "files": [
28
+ "dist"
29
+ ],
30
+ "scripts": {
31
+ "dev": "nodemon --watch package.json --watch src --ext js,ts,mjs,cjs,json --exec \"tsdown && yalc publish\"",
32
+ "build": "tsdown",
33
+ "lint": "eslint . --ext .ts",
34
+ "prepare": "npm run build",
35
+ "test": "jest"
36
+ },
37
+ "dependencies": {
38
+ "gatsby-source-filesystem": "^5.16.0",
39
+ "is-relative-url": "3.0.0",
40
+ "parse5": "5.0.0",
41
+ "slash": "^3.0.0",
42
+ "unist-util-select": "3.0.4"
43
+ },
44
+ "devDependencies": {
45
+ "@babel/core": "^7.29.0",
46
+ "@babel/preset-env": "^7.29.0",
47
+ "@babel/preset-typescript": "^7.28.5",
48
+ "@swc/core": "^1.15.3",
49
+ "@types/fs-extra": "^8.0.0",
50
+ "@types/jest": "^30.0.0",
51
+ "@types/mdast": "^4.0.4",
52
+ "babel-preset-gatsby": "^3.16.0",
53
+ "eslint": "^9.39.1",
54
+ "gatsby": "^5.16.1",
55
+ "gatsby-plugin-sharp": "^5.16.0",
56
+ "husky": "^9.1.7",
57
+ "identity-obj-proxy": "^3.0.0",
58
+ "jest": "^30.2.0",
59
+ "mdast": "^2.3.2",
60
+ "nodemon": "^3.1.11",
61
+ "tap": "^14.9.2",
62
+ "ts-jest": "^29.4.6",
63
+ "ts-node-test": "^0.4.4",
64
+ "tsdown": "^0.16.6",
65
+ "tslib": "^2.8.1",
66
+ "typescript": "^5.9.3"
67
+ },
68
+ "peerDependencies": {
69
+ "gatsby": "^5.15.0",
70
+ "gatsby-plugin-sharp": "^5.15.0",
71
+ "gatsby-source-filesystem": "^5.15.0"
72
+ },
73
+ "typings": "dist/index.d.ts",
74
+ "prettier": {
75
+ "printWidth": 80,
76
+ "semi": true,
77
+ "singleQuote": true,
78
+ "trailingComma": "es5"
79
+ }
80
+ }