@libsrcdev/gatsby-remark-images-anywhere 0.1.0
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/LICENSE +21 -0
- package/README.md +215 -0
- package/dist/index.d.ts +74 -0
- package/dist/index.js +71004 -0
- package/package.json +65 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Derek Nguyen, @libsrcdev
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
# gatsby-remark-structured-content
|
|
2
|
+
|
|
3
|
+
This plugin processes images from markdown files that are parsed with [`gatsby-transformer-remark`](https://www.gatsbyjs.org/packages/gatsby-transformer-remark/). It supports images from multiple sources including relative paths, remote URLs, protocol-relative URLs, and CMS-generated paths.
|
|
4
|
+
|
|
5
|
+
## Fork Notice
|
|
6
|
+
|
|
7
|
+
This is a fork of [gatsby-remark-images-anywhere](https://github.com/d4rekanguok/gatsby-remark-images-anywhere) with the following improvements:
|
|
8
|
+
|
|
9
|
+
- Updated dependencies to latest versions
|
|
10
|
+
- Added support for custom HTTP headers in image requests (useful for authenticated image sources)
|
|
11
|
+
- Enhanced URL validation and security features
|
|
12
|
+
|
|
13
|
+
## Why use this?
|
|
14
|
+
|
|
15
|
+
`gatsby-remark-images` is great, but if you use a CMS that pastes remote URLs or paths that aren't relative to the markdown file itself, it won't work. This plugin provides a more flexible solution that:
|
|
16
|
+
|
|
17
|
+
- Takes any image path (relative, absolute, remote, protocol-relative) and feeds them to Sharp
|
|
18
|
+
- Allows customized Sharp methods (`fluid`, `fixed`, `resize`)
|
|
19
|
+
- Supports custom image templates
|
|
20
|
+
- Works with NetlifyCMS and other headless CMS platforms
|
|
21
|
+
- Extracts images based on custom logic for thumbnails or galleries
|
|
22
|
+
|
|
23
|
+
## Supported image paths
|
|
24
|
+
|
|
25
|
+
```markdown
|
|
26
|
+
# Regular relative path
|
|
27
|
+

|
|
28
|
+
|
|
29
|
+
# NetlifyCMS path (absolute from root)
|
|
30
|
+

|
|
31
|
+
|
|
32
|
+
# Remote path
|
|
33
|
+

|
|
34
|
+
|
|
35
|
+
# Protocol relative path
|
|
36
|
+

|
|
37
|
+
|
|
38
|
+
# Also works with <img /> tags
|
|
39
|
+
<img src="./image.png" alt="hey" title="hello" />
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
### Protocol relative paths
|
|
43
|
+
See the whitelisted domains [here](./src/relative-protocol-whitelist.ts)
|
|
44
|
+
|
|
45
|
+
## How to install
|
|
46
|
+
|
|
47
|
+
```sh
|
|
48
|
+
npm i --save @libsrcdev/gatsby-remark-structured-content
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Requirements
|
|
52
|
+
Your project needs to have:
|
|
53
|
+
- gatsby
|
|
54
|
+
- gatsby-source-filesystem
|
|
55
|
+
- gatsby-transformer-remark
|
|
56
|
+
- gatsby-transformer-sharp
|
|
57
|
+
- gatsby-plugin-sharp
|
|
58
|
+
|
|
59
|
+
## Capabilities
|
|
60
|
+
|
|
61
|
+
- List embedded images of markdown content based on custom logic
|
|
62
|
+
- Remove embedded images of markdown content based on custom logic
|
|
63
|
+
- Process images from any source (local, remote, CMS)
|
|
64
|
+
- Generate optimized Sharp images with custom configurations
|
|
65
|
+
|
|
66
|
+
## Use cases
|
|
67
|
+
|
|
68
|
+
- Extract the first embedded image to use it as a thumbnail
|
|
69
|
+
- Create a gallery of all images used in a post
|
|
70
|
+
- Process remote images from a headless CMS
|
|
71
|
+
- Handle NetlifyCMS absolute paths
|
|
72
|
+
|
|
73
|
+
## Usage
|
|
74
|
+
|
|
75
|
+
Basic example:
|
|
76
|
+
|
|
77
|
+
```javascript
|
|
78
|
+
// In your gatsby-config.js
|
|
79
|
+
plugins: [
|
|
80
|
+
{
|
|
81
|
+
resolve: `gatsby-transformer-remark`,
|
|
82
|
+
options: {
|
|
83
|
+
plugins: [
|
|
84
|
+
{
|
|
85
|
+
resolve: `gatsby-remark-structured-content`,
|
|
86
|
+
options: {
|
|
87
|
+
// Optional: provide a function to extract images based on custom logic
|
|
88
|
+
shouldExtractImage: async (code) => { ... },
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
],
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
];
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Configuration
|
|
98
|
+
|
|
99
|
+
Full configuration options:
|
|
100
|
+
|
|
101
|
+
```js
|
|
102
|
+
{
|
|
103
|
+
resolve: `gatsby-remark-structured-content`,
|
|
104
|
+
options: {
|
|
105
|
+
/**
|
|
106
|
+
* @param {string} staticDir
|
|
107
|
+
* Root folder for images. For example,
|
|
108
|
+
* if your image path is `/assets/image.png`,
|
|
109
|
+
* your image is located in `static/assets/image.png`,
|
|
110
|
+
* then the staticDir is `static`.
|
|
111
|
+
*/
|
|
112
|
+
staticDir: 'static',
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* @param {Function} createMarkup
|
|
116
|
+
* A function that returns string template for image
|
|
117
|
+
* All sharp result will be passed in as arguments
|
|
118
|
+
*/
|
|
119
|
+
createMarkup: ({ src, srcSet }) => `<img src="${src}" srcSet="${srcSet}" class="custom-class" />`,
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* @param {'lazy' | 'eager' | 'auto'} loading
|
|
123
|
+
* Set the output markup's 'loading' attribute. Default: 'lazy'
|
|
124
|
+
*/
|
|
125
|
+
loading: 'lazy',
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* @param {string} backgroundColor
|
|
129
|
+
* Background color. Default: '#fff'
|
|
130
|
+
*/
|
|
131
|
+
backgroundColor: '#fff',
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* @param {boolean} linkImagesToOriginal
|
|
135
|
+
* If enabled, wraps the default markup with an <a> tag pointing to the original image.
|
|
136
|
+
* Default: false
|
|
137
|
+
*/
|
|
138
|
+
linkImagesToOriginal: true,
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* @param {string | Function} wrapperStyle
|
|
142
|
+
* Inject styles to the image wrapper.
|
|
143
|
+
* Also accepts a function that receives all image data as arguments:
|
|
144
|
+
* ({ aspectRatio, width, height }) => `padding-bottom: ${height/2}px;`
|
|
145
|
+
*/
|
|
146
|
+
wrapperStyle: 'padding-bottom: 0.5rem;',
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* @param {'fluid' | 'fixed' | 'resize'} sharpMethod
|
|
150
|
+
* Default: 'fluid'.
|
|
151
|
+
*/
|
|
152
|
+
sharpMethod: 'fluid',
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Sharp image options
|
|
156
|
+
* Any sharp image arguments (quality, maxWidth, etc.)
|
|
157
|
+
*/
|
|
158
|
+
maxWidth: 650,
|
|
159
|
+
quality: 50,
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
## Writing your own markup
|
|
165
|
+
|
|
166
|
+
You can customize the image markup using the `createMarkup` function:
|
|
167
|
+
|
|
168
|
+
```ts
|
|
169
|
+
type CreateMarkup = (args: CreateMarkupArgs, options?: MarkupOptions) => string;
|
|
170
|
+
|
|
171
|
+
interface CreateMarkupArgs {
|
|
172
|
+
sharpMethod: SharpMethod;
|
|
173
|
+
originSrc: string;
|
|
174
|
+
title?: string;
|
|
175
|
+
alt?: string;
|
|
176
|
+
|
|
177
|
+
aspectRatio: number;
|
|
178
|
+
src: string;
|
|
179
|
+
srcSet?: string;
|
|
180
|
+
srcWebp?: string;
|
|
181
|
+
srcSetWebp?: string;
|
|
182
|
+
base64?: string;
|
|
183
|
+
tracedSVG?: string;
|
|
184
|
+
|
|
185
|
+
// fixed, resize
|
|
186
|
+
width?: number;
|
|
187
|
+
height?: number;
|
|
188
|
+
|
|
189
|
+
// fluid
|
|
190
|
+
presentationHeight?: number;
|
|
191
|
+
presentationWidth?: number;
|
|
192
|
+
sizes?: string;
|
|
193
|
+
originalImg?: string;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
interface MarkupOptions {
|
|
197
|
+
loading: 'lazy' | 'eager' | 'auto';
|
|
198
|
+
linkImagesToOriginal: boolean;
|
|
199
|
+
showCaptions: boolean;
|
|
200
|
+
wrapperStyle: string | Function;
|
|
201
|
+
backgroundColor: string;
|
|
202
|
+
tracedSVG: boolean | Object;
|
|
203
|
+
blurUp: boolean;
|
|
204
|
+
}
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
## Example
|
|
208
|
+
|
|
209
|
+
[Codesandbox demo](https://codesandbox.io/s/gatsby-remark-images-anywhere-remark-custom-component-lazy-load-007vo) showing this plugin combined with [`gatsby-transformer-remark` custom components](https://using-remark.gatsbyjs.org/custom-components/) to achieve `gatsby-image`-like benefits (blur up, lazy loading, aspect-ratio).
|
|
210
|
+
|
|
211
|
+
## Should I use this?
|
|
212
|
+
|
|
213
|
+
- If you don't use remote images or a CMS, use [`gatsby-remark-images`](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-remark-images)
|
|
214
|
+
- If you're using vanilla NetlifyCMS, use [`gatsby-remark-relative-images`](https://github.com/danielmahon/gatsby-remark-relative-images)
|
|
215
|
+
- If you need remote images or more flexibility, try this plugin!
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { Node, NodePluginArgs } from "gatsby";
|
|
2
|
+
import { FileSystemNode } from "gatsby-source-filesystem";
|
|
3
|
+
|
|
4
|
+
//#region src/custom-http-headers/http-request-header-options.d.ts
|
|
5
|
+
type HttpRequestHeaderOptions = {
|
|
6
|
+
dangerouslyBuildRequestHttpHeaders?: (url: string) => Record<string, string> | undefined;
|
|
7
|
+
httpHeaderProviders: ((url: string) => Record<string, string> | undefined)[];
|
|
8
|
+
};
|
|
9
|
+
//#endregion
|
|
10
|
+
//#region src/type.d.ts
|
|
11
|
+
type SharpMethod = 'fluid' | 'fixed' | 'resize';
|
|
12
|
+
interface RemarkNode {
|
|
13
|
+
type: string;
|
|
14
|
+
[key: string]: any;
|
|
15
|
+
}
|
|
16
|
+
interface Args extends NodePluginArgs {
|
|
17
|
+
markdownAST: RemarkNode;
|
|
18
|
+
markdownNode: Node;
|
|
19
|
+
files: FileSystemNode[];
|
|
20
|
+
}
|
|
21
|
+
interface SharpResult {
|
|
22
|
+
aspectRatio: number;
|
|
23
|
+
src: string;
|
|
24
|
+
srcSet?: string;
|
|
25
|
+
srcWebp?: string;
|
|
26
|
+
srcSetWebp?: string;
|
|
27
|
+
base64?: string;
|
|
28
|
+
tracedSVG?: string;
|
|
29
|
+
width?: number;
|
|
30
|
+
height?: number;
|
|
31
|
+
presentationHeight?: number;
|
|
32
|
+
presentationWidth?: number;
|
|
33
|
+
sizes?: string;
|
|
34
|
+
originalImg?: string;
|
|
35
|
+
}
|
|
36
|
+
interface CreateMarkupArgs extends SharpResult {
|
|
37
|
+
sharpMethod: SharpMethod;
|
|
38
|
+
originSrc: string;
|
|
39
|
+
title?: string;
|
|
40
|
+
alt?: string;
|
|
41
|
+
}
|
|
42
|
+
interface MarkupOptions {
|
|
43
|
+
loading: 'lazy' | 'eager' | 'auto';
|
|
44
|
+
linkImagesToOriginal: boolean;
|
|
45
|
+
showCaptions: boolean;
|
|
46
|
+
wrapperStyle: string | Function;
|
|
47
|
+
backgroundColor: string;
|
|
48
|
+
tracedSVG: boolean | Object;
|
|
49
|
+
blurUp: boolean;
|
|
50
|
+
}
|
|
51
|
+
type CreateMarkup = (args: CreateMarkupArgs, options?: MarkupOptions) => string;
|
|
52
|
+
interface Options extends Partial<MarkupOptions>, HttpRequestHeaderOptions {
|
|
53
|
+
plugins: unknown[];
|
|
54
|
+
staticDir?: string;
|
|
55
|
+
createMarkup?: CreateMarkup;
|
|
56
|
+
sharpMethod: SharpMethod;
|
|
57
|
+
[key: string]: unknown;
|
|
58
|
+
}
|
|
59
|
+
//#endregion
|
|
60
|
+
//#region src/index.d.ts
|
|
61
|
+
declare const addImage: ({
|
|
62
|
+
markdownAST: mdast,
|
|
63
|
+
markdownNode,
|
|
64
|
+
actions,
|
|
65
|
+
store,
|
|
66
|
+
files,
|
|
67
|
+
getNode,
|
|
68
|
+
getCache,
|
|
69
|
+
createNodeId,
|
|
70
|
+
reporter,
|
|
71
|
+
cache,
|
|
72
|
+
pathPrefix
|
|
73
|
+
}: Args, pluginOptions: Options) => Promise<(null | undefined)[]>;
|
|
74
|
+
export = addImage;
|