@libsrcdev/gatsby-remark-images-anywhere 0.1.2 → 0.1.3

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/dist/index.d.ts CHANGED
@@ -5,8 +5,9 @@ import { Literal } from "mdast";
5
5
  //#region src/custom-http-headers/http-request-header-options.d.ts
6
6
  type HttpRequestHeaderOptions = {
7
7
  dangerouslyBuildRequestHttpHeaders?: (url: string) => Record<string, string> | undefined;
8
- httpHeaderProviders: ((url: string) => Record<string, string> | undefined)[];
8
+ httpHeaderProviders?: HttpRequestHeaderProvider[];
9
9
  };
10
+ type HttpRequestHeaderProvider = (url: string) => Record<string, string> | undefined;
10
11
  //#endregion
11
12
  //#region src/type.d.ts
12
13
  type SharpMethod = 'fluid' | 'fixed' | 'resize';
package/dist/index.js CHANGED
@@ -32,6 +32,8 @@ let unist_util_select = require("unist-util-select");
32
32
  let slash = require("slash");
33
33
  slash = __toESM(slash);
34
34
  let parse5 = require("parse5");
35
+ let is_relative_url = require("is-relative-url");
36
+ is_relative_url = __toESM(is_relative_url);
35
37
 
36
38
  //#region node_modules/universalify/index.js
37
39
  var require_universalify = /* @__PURE__ */ __commonJSMin(((exports) => {
@@ -70917,21 +70919,49 @@ const isWhitelisted = makeWhitelistTest(rpWhitelist);
70917
70919
 
70918
70920
  //#endregion
70919
70921
  //#region src/custom-http-headers/http-header-trusted-provider.ts
70920
- function createRequestHttpHeaderBuilder({ dangerouslyBuildRequestHttpHeaders, httpHeaderProviders }) {
70921
- if (dangerouslyBuildRequestHttpHeaders) return dangerouslyBuildRequestHttpHeaders;
70922
- if (httpHeaderProviders) return (url$3) => {
70923
- for (const httpHeaderProvider of httpHeaderProviders) {
70924
- const httpHeaders = httpHeaderProvider(url$3);
70925
- if (httpHeaders) return httpHeaders;
70926
- }
70927
- };
70928
- return (_$2) => void 0;
70922
+ const buildRequestHttpHeadersWith = (httpHeaderProviders) => (url$3) => {
70923
+ for (const httpHeaderProvider of httpHeaderProviders ?? []) {
70924
+ const httpHeaders = httpHeaderProvider(url$3);
70925
+ if (httpHeaders) return httpHeaders;
70926
+ }
70927
+ };
70928
+
70929
+ //#endregion
70930
+ //#region src/utils.ts
70931
+ /**
70932
+ * Resolves a potentially incomplete URL to a full URL with protocol
70933
+ * @param url - The URL to resolve (can be protocol-relative or full)
70934
+ * @returns The full URL with protocol, or null if invalid or relative
70935
+ */
70936
+ function resolveFullUrl(url$3) {
70937
+ const trimmed = url$3.trim();
70938
+ if (!trimmed) return;
70939
+ if ((0, is_relative_url.default)(trimmed)) return;
70940
+ let resolvedUrl = trimmed;
70941
+ if (trimmed.startsWith("//")) resolvedUrl = `https:${trimmed}`;
70942
+ try {
70943
+ new URL(resolvedUrl);
70944
+ return resolvedUrl;
70945
+ } catch {
70946
+ return;
70947
+ }
70948
+ }
70949
+ /**
70950
+ * Resolves a relative URL by validating and returning it if it's relative
70951
+ * @param url - The URL to check and resolve
70952
+ * @returns The relative URL if valid, or undefined if not relative or invalid
70953
+ */
70954
+ function resolveRelativeUrl(url$3) {
70955
+ const trimmed = url$3.trim();
70956
+ if (!trimmed) return;
70957
+ if (!(0, is_relative_url.default)(trimmed)) return;
70958
+ return trimmed;
70929
70959
  }
70930
70960
 
70931
70961
  //#endregion
70932
70962
  //#region src/index.ts
70933
70963
  async function remarkImagesAnywhere({ markdownAST: mdast, markdownNode, actions: actions$1, store, files, getNode, getCache, createNodeId, reporter, cache: cache$3, pathPrefix }, pluginOptions$1) {
70934
- 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$1;
70964
+ 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$1;
70935
70965
  if ([
70936
70966
  "fluid",
70937
70967
  "fixed",
@@ -70948,27 +70978,28 @@ async function remarkImagesAnywhere({ markdownAST: mdast, markdownNode, actions:
70948
70978
  let url$3 = node.url;
70949
70979
  let gImgFileNode;
70950
70980
  if (isWhitelisted(url$3)) url$3 = `https:${url$3}`;
70951
- if (url$3.startsWith("http")) gImgFileNode = await downloadImage({
70952
- id: markdownNode.id,
70953
- url: url$3,
70954
- getCache,
70955
- getNode,
70956
- touchNode,
70957
- cache: cache$3,
70958
- createNode,
70959
- createNodeId,
70960
- reporter,
70961
- dangerouslyBuildImageRequestHttpHeaders: createRequestHttpHeaderBuilder({
70962
- dangerouslyBuildRequestHttpHeaders,
70963
- httpHeaderProviders
70964
- })
70965
- });
70966
- else {
70981
+ const remoteFullImageUrl = resolveFullUrl(url$3);
70982
+ const relativeImageUrl = resolveRelativeUrl(url$3);
70983
+ if (remoteFullImageUrl) {
70984
+ const buildRequestHttpHeaders = dangerouslyBuildRequestHttpHeaders ?? buildRequestHttpHeadersWith(httpHeaderProviders);
70985
+ gImgFileNode = await downloadImage({
70986
+ id: markdownNode.id,
70987
+ url: new URL(url$3).protocol,
70988
+ getCache,
70989
+ getNode,
70990
+ touchNode,
70991
+ cache: cache$3,
70992
+ createNode,
70993
+ createNodeId,
70994
+ reporter,
70995
+ dangerouslyBuildImageRequestHttpHeaders: buildRequestHttpHeaders
70996
+ });
70997
+ } else if (relativeImageUrl) {
70967
70998
  let filePath;
70968
70999
  if (dirPath && url$3[0] === ".") filePath = (0, slash.default)(path.default.join(dirPath, url$3));
70969
71000
  else filePath = path.default.join(directory, staticDir, url$3);
70970
71001
  gImgFileNode = files.find((fileNode) => fileNode.absolutePath && fileNode.absolutePath === filePath);
70971
- }
71002
+ } else reporter.warn(`Skipping invalid image URL ${url$3}`);
70972
71003
  if (!gImgFileNode) return;
70973
71004
  if (!SUPPORT_EXTS.includes(gImgFileNode.extension)) return;
70974
71005
  const imageResult = await processImage({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@libsrcdev/gatsby-remark-images-anywhere",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "Handle images with relative, absolute, remote path for gatsby-transformer-remark.",
5
5
  "keywords": [
6
6
  "gatsby",
@@ -35,25 +35,26 @@
35
35
  "prepare": "npm run build"
36
36
  },
37
37
  "dependencies": {
38
+ "is-relative-url": "^4.1.0",
38
39
  "parse5": "^8.0.0",
39
40
  "slash": "^3.0.0",
40
41
  "unist-util-select": "^5.1.0"
41
42
  },
42
43
  "devDependencies": {
43
44
  "@swc/core": "^1.15.3",
45
+ "@types/fs-extra": "^8.0.0",
44
46
  "@types/mdast": "^4.0.4",
45
47
  "eslint": "^9.39.1",
46
48
  "gatsby": "^5.15.0",
49
+ "gatsby-plugin-sharp": "^5.10.0",
50
+ "gatsby-source-filesystem": "^5.10.0",
47
51
  "husky": "^9.1.7",
48
52
  "mdast": "^2.3.2",
49
53
  "nodemon": "^3.1.11",
54
+ "tap": "^14.9.2",
50
55
  "tsdown": "^0.16.6",
51
56
  "tslib": "^2.8.1",
52
- "typescript": "^5.9.3",
53
- "@types/fs-extra": "^8.0.0",
54
- "gatsby-plugin-sharp": "^5.10.0",
55
- "gatsby-source-filesystem": "^5.10.0",
56
- "tap": "^14.9.2"
57
+ "typescript": "^5.9.3"
57
58
  },
58
59
  "typings": "dist/index.d.ts",
59
60
  "prettier": {
@@ -62,4 +63,4 @@
62
63
  "singleQuote": true,
63
64
  "trailingComma": "es5"
64
65
  }
65
- }
66
+ }