@libsrcdev/gatsby-remark-images-anywhere 0.1.0 → 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 +7 -9
- package/dist/index.js +81 -48
- package/package.json +8 -7
package/dist/index.d.ts
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
1
1
|
import { Node, NodePluginArgs } from "gatsby";
|
|
2
2
|
import { FileSystemNode } from "gatsby-source-filesystem";
|
|
3
|
+
import { Literal } from "mdast";
|
|
3
4
|
|
|
4
5
|
//#region src/custom-http-headers/http-request-header-options.d.ts
|
|
5
6
|
type HttpRequestHeaderOptions = {
|
|
6
7
|
dangerouslyBuildRequestHttpHeaders?: (url: string) => Record<string, string> | undefined;
|
|
7
|
-
httpHeaderProviders
|
|
8
|
+
httpHeaderProviders?: HttpRequestHeaderProvider[];
|
|
8
9
|
};
|
|
10
|
+
type HttpRequestHeaderProvider = (url: string) => Record<string, string> | undefined;
|
|
9
11
|
//#endregion
|
|
10
12
|
//#region src/type.d.ts
|
|
11
13
|
type SharpMethod = 'fluid' | 'fixed' | 'resize';
|
|
12
|
-
interface RemarkNode {
|
|
13
|
-
type: string;
|
|
14
|
-
[key: string]: any;
|
|
15
|
-
}
|
|
16
14
|
interface Args extends NodePluginArgs {
|
|
17
|
-
markdownAST:
|
|
15
|
+
markdownAST: Literal;
|
|
18
16
|
markdownNode: Node;
|
|
19
17
|
files: FileSystemNode[];
|
|
20
18
|
}
|
|
@@ -58,7 +56,7 @@ interface Options extends Partial<MarkupOptions>, HttpRequestHeaderOptions {
|
|
|
58
56
|
}
|
|
59
57
|
//#endregion
|
|
60
58
|
//#region src/index.d.ts
|
|
61
|
-
declare
|
|
59
|
+
declare function remarkImagesAnywhere({
|
|
62
60
|
markdownAST: mdast,
|
|
63
61
|
markdownNode,
|
|
64
62
|
actions,
|
|
@@ -70,5 +68,5 @@ declare const addImage: ({
|
|
|
70
68
|
reporter,
|
|
71
69
|
cache,
|
|
72
70
|
pathPrefix
|
|
73
|
-
}: Args, pluginOptions: Options)
|
|
74
|
-
export =
|
|
71
|
+
}: Args, pluginOptions: Options): Promise<(null | undefined)[]>;
|
|
72
|
+
export = remarkImagesAnywhere;
|
package/dist/index.js
CHANGED
|
@@ -29,11 +29,11 @@ var __toESM = (mod, isNodeMode, target$1) => (target$1 = mod != null ? __create(
|
|
|
29
29
|
let path = require("path");
|
|
30
30
|
path = __toESM(path);
|
|
31
31
|
let unist_util_select = require("unist-util-select");
|
|
32
|
-
unist_util_select = __toESM(unist_util_select);
|
|
33
32
|
let slash = require("slash");
|
|
34
33
|
slash = __toESM(slash);
|
|
35
34
|
let parse5 = require("parse5");
|
|
36
|
-
|
|
35
|
+
let is_relative_url = require("is-relative-url");
|
|
36
|
+
is_relative_url = __toESM(is_relative_url);
|
|
37
37
|
|
|
38
38
|
//#region node_modules/universalify/index.js
|
|
39
39
|
var require_universalify = /* @__PURE__ */ __commonJSMin(((exports) => {
|
|
@@ -70756,23 +70756,27 @@ const processImage = async ({ file: file$1, reporter, cache: cache$3, pathPrefix
|
|
|
70756
70756
|
//#region src/util-html-to-md.ts
|
|
70757
70757
|
const toMdNode = (node) => {
|
|
70758
70758
|
const value = node.value;
|
|
70759
|
-
const imgNode = parse5.
|
|
70759
|
+
const imgNode = (0, parse5.parseFragment)(value).childNodes.find((node$1) => {
|
|
70760
|
+
if ("tagName" in node$1) return node$1.tagName === "img";
|
|
70761
|
+
return false;
|
|
70762
|
+
});
|
|
70760
70763
|
if (!imgNode) return null;
|
|
70761
|
-
const attrs = imgNode.attrs.reduce((acc, cur) => {
|
|
70764
|
+
const attrs = "attrs" in imgNode ? imgNode.attrs.reduce((acc, cur) => {
|
|
70762
70765
|
const { name: name$1, value: value$1 } = cur;
|
|
70763
70766
|
acc[name$1] = value$1;
|
|
70764
70767
|
return acc;
|
|
70765
|
-
}, {});
|
|
70766
|
-
if (
|
|
70768
|
+
}, {}) : {};
|
|
70769
|
+
if ("src" in attrs) return null;
|
|
70767
70770
|
const original = { ...node };
|
|
70768
|
-
|
|
70769
|
-
|
|
70770
|
-
|
|
70771
|
-
|
|
70772
|
-
|
|
70773
|
-
|
|
70774
|
-
|
|
70775
|
-
|
|
70771
|
+
const remarkImageNode = node;
|
|
70772
|
+
remarkImageNode.type = "image";
|
|
70773
|
+
remarkImageNode.value = "";
|
|
70774
|
+
remarkImageNode.url = attrs.src;
|
|
70775
|
+
remarkImageNode.title = attrs.title;
|
|
70776
|
+
remarkImageNode.alt = attrs.alt;
|
|
70777
|
+
remarkImageNode.data = {};
|
|
70778
|
+
remarkImageNode.data.original = original;
|
|
70779
|
+
return remarkImageNode;
|
|
70776
70780
|
};
|
|
70777
70781
|
|
|
70778
70782
|
//#endregion
|
|
@@ -70915,21 +70919,49 @@ const isWhitelisted = makeWhitelistTest(rpWhitelist);
|
|
|
70915
70919
|
|
|
70916
70920
|
//#endregion
|
|
70917
70921
|
//#region src/custom-http-headers/http-header-trusted-provider.ts
|
|
70918
|
-
|
|
70919
|
-
|
|
70920
|
-
|
|
70921
|
-
|
|
70922
|
-
|
|
70923
|
-
|
|
70924
|
-
|
|
70925
|
-
|
|
70926
|
-
|
|
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;
|
|
70927
70959
|
}
|
|
70928
70960
|
|
|
70929
70961
|
//#endregion
|
|
70930
70962
|
//#region src/index.ts
|
|
70931
|
-
|
|
70932
|
-
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;
|
|
70963
|
+
async function remarkImagesAnywhere({ markdownAST: mdast, markdownNode, actions: actions$1, store, files, getNode, getCache, createNodeId, reporter, cache: cache$3, pathPrefix }, 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;
|
|
70933
70965
|
if ([
|
|
70934
70966
|
"fluid",
|
|
70935
70967
|
"fixed",
|
|
@@ -70938,35 +70970,36 @@ const addImage = async ({ markdownAST: mdast, markdownNode, actions: actions$1,
|
|
|
70938
70970
|
const { touchNode, createNode } = actions$1;
|
|
70939
70971
|
const dirPath = markdownNode.parent && getNode(markdownNode.parent)?.dir;
|
|
70940
70972
|
const { directory } = store.getState().program;
|
|
70941
|
-
const imgNodes = unist_util_select.
|
|
70942
|
-
const htmlImgNodes = unist_util_select.
|
|
70973
|
+
const imgNodes = (0, unist_util_select.selectAll)("image[url]", mdast).filter((node) => "src" in node);
|
|
70974
|
+
const htmlImgNodes = (0, unist_util_select.selectAll)("html, jsx", mdast).filter((node) => "value" in node).map((node, _$2, __) => toMdNode(node)).filter((node, _$2, __) => !!node);
|
|
70943
70975
|
imgNodes.push(...htmlImgNodes);
|
|
70944
70976
|
const processPromises = imgNodes.map(async (node) => {
|
|
70977
|
+
if (!node.url) return;
|
|
70945
70978
|
let url$3 = node.url;
|
|
70946
|
-
if (!url$3) return;
|
|
70947
70979
|
let gImgFileNode;
|
|
70948
70980
|
if (isWhitelisted(url$3)) url$3 = `https:${url$3}`;
|
|
70949
|
-
|
|
70950
|
-
|
|
70951
|
-
|
|
70952
|
-
|
|
70953
|
-
|
|
70954
|
-
|
|
70955
|
-
|
|
70956
|
-
|
|
70957
|
-
|
|
70958
|
-
|
|
70959
|
-
|
|
70960
|
-
|
|
70961
|
-
|
|
70962
|
-
|
|
70963
|
-
|
|
70964
|
-
|
|
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) {
|
|
70965
70998
|
let filePath;
|
|
70966
70999
|
if (dirPath && url$3[0] === ".") filePath = (0, slash.default)(path.default.join(dirPath, url$3));
|
|
70967
71000
|
else filePath = path.default.join(directory, staticDir, url$3);
|
|
70968
71001
|
gImgFileNode = files.find((fileNode) => fileNode.absolutePath && fileNode.absolutePath === filePath);
|
|
70969
|
-
}
|
|
71002
|
+
} else reporter.warn(`Skipping invalid image URL ${url$3}`);
|
|
70970
71003
|
if (!gImgFileNode) return;
|
|
70971
71004
|
if (!SUPPORT_EXTS.includes(gImgFileNode.extension)) return;
|
|
70972
71005
|
const imageResult = await processImage({
|
|
@@ -70998,7 +71031,7 @@ const addImage = async ({ markdownAST: mdast, markdownNode, actions: actions$1,
|
|
|
70998
71031
|
return null;
|
|
70999
71032
|
});
|
|
71000
71033
|
return Promise.all(processPromises);
|
|
71001
|
-
}
|
|
71002
|
-
module.exports = addImage;
|
|
71034
|
+
}
|
|
71003
71035
|
|
|
71004
|
-
//#endregion
|
|
71036
|
+
//#endregion
|
|
71037
|
+
module.exports = remarkImagesAnywhere;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libsrcdev/gatsby-remark-images-anywhere",
|
|
3
|
-
"version": "0.1.
|
|
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
|
+
}
|