nuxt-content-assets 1.1.0 → 1.1.1
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/README.md +23 -19
- package/dist/module.json +1 -1
- package/dist/module.mjs +7 -28
- package/dist/runtime/plugin.mjs +17 -14
- package/dist/runtime/services/assets.d.ts +3 -10
- package/dist/runtime/services/assets.mjs +4 -23
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -65,17 +65,22 @@ Developer experience:
|
|
|
65
65
|
|
|
66
66
|
## Demo
|
|
67
67
|
|
|
68
|
-
To
|
|
68
|
+
To clone and run the demo locally:
|
|
69
69
|
|
|
70
|
-
```
|
|
70
|
+
```bash
|
|
71
|
+
git clone https://github.com/davestewart/nuxt-content-assets.git
|
|
72
|
+
cd nuxt-content-assets
|
|
73
|
+
npm install
|
|
71
74
|
npm run dev
|
|
72
75
|
```
|
|
73
76
|
|
|
74
|
-
|
|
77
|
+
Then open the demo in your browser at <a href="http://localhost:3000" target="_blank">localhost:3000</a>.
|
|
78
|
+
|
|
79
|
+
To run the demo online, visit:
|
|
75
80
|
|
|
76
81
|
- https://stackblitz.com/github/davestewart/nuxt-content-assets?file=demo%2Fapp.vue
|
|
77
82
|
|
|
78
|
-
|
|
83
|
+
To browse the demo folder:
|
|
79
84
|
|
|
80
85
|
- https://github.com/davestewart/nuxt-content-assets/tree/main/demo
|
|
81
86
|
|
|
@@ -162,11 +167,11 @@ The module is [preconfigured](#image-size) to pass image size hints (by default
|
|
|
162
167
|
<img src="/image.jpg" width="640" height="480">
|
|
163
168
|
```
|
|
164
169
|
|
|
165
|
-
|
|
170
|
+
Keeping this on prevents content jumps as your page loads.
|
|
166
171
|
|
|
167
172
|
#### Prose components
|
|
168
173
|
|
|
169
|
-
If you use [ProseImg](https://content.nuxtjs.org/api/components/prose) components, you can
|
|
174
|
+
If you use [ProseImg](https://content.nuxtjs.org/api/components/prose) components, you can [hook into](demo/components/temp/ProseImg.vue) image size hints via the `$attrs` property:
|
|
170
175
|
|
|
171
176
|
```vue
|
|
172
177
|
<template>
|
|
@@ -184,13 +189,13 @@ export default {
|
|
|
184
189
|
|
|
185
190
|
#### Frontmatter
|
|
186
191
|
|
|
187
|
-
If you pass [frontmatter](demo/content/advanced/gallery.md) to [custom components](demo/components/content/ContentImage.vue)
|
|
192
|
+
If you pass [frontmatter](demo/content/advanced/gallery.md) to [custom components](demo/components/content/ContentImage.vue) set `imageSize` to `'src'` to encode values in `src`:
|
|
188
193
|
|
|
189
194
|
```
|
|
190
195
|
:image-content{:src="image"}
|
|
191
196
|
```
|
|
192
197
|
|
|
193
|
-
The component will receive the
|
|
198
|
+
The component will receive the size information as a query string which you can extract and apply:
|
|
194
199
|
|
|
195
200
|
```html
|
|
196
201
|
<img class="image-content" src="/image.jpg?width=640&height=480">
|
|
@@ -200,7 +205,7 @@ See demo component [here](demo/components/content/ContentImage.vue).
|
|
|
200
205
|
|
|
201
206
|
## Configuration
|
|
202
207
|
|
|
203
|
-
The module
|
|
208
|
+
The module has the following options:
|
|
204
209
|
|
|
205
210
|
```ts
|
|
206
211
|
// nuxt.config.ts
|
|
@@ -230,13 +235,14 @@ You can add one or more image size hints to the generated images:
|
|
|
230
235
|
|
|
231
236
|
Pick from the following switches:
|
|
232
237
|
|
|
233
|
-
| Switch
|
|
234
|
-
|
|
|
235
|
-
| `style` | Adds `style="aspect-ratio:..."` to any `<img>` tag |
|
|
236
|
-
| `attrs` | Adds `width` and `height` attributes to any `<img>` tag |
|
|
237
|
-
| `src` | Adds `?width=...&height=...` to `src` attribute (frontmatter only) |
|
|
238
|
+
| Switch | What it does |
|
|
239
|
+
| --------- | ------------------------------------------------------------ |
|
|
240
|
+
| `'style'` | Adds `style="aspect-ratio:..."` to any `<img>` tag |
|
|
241
|
+
| `'attrs'` | Adds `width` and `height` attributes to any `<img>` tag |
|
|
242
|
+
| `'src'` | Adds `?width=...&height=...` to `src` attribute (frontmatter only) |
|
|
243
|
+
| `false` | Disable image size hints |
|
|
238
244
|
|
|
239
|
-
Note: if you add *only* `attrs
|
|
245
|
+
Note: if you add *only* `attrs`, include the following CSS in your app:
|
|
240
246
|
|
|
241
247
|
```css
|
|
242
248
|
img {
|
|
@@ -245,10 +251,10 @@ img {
|
|
|
245
251
|
}
|
|
246
252
|
```
|
|
247
253
|
|
|
248
|
-
To disable, pass `false`.
|
|
249
|
-
|
|
250
254
|
### Content extensions
|
|
251
255
|
|
|
256
|
+
> Generally, you shouldn't need to touch this setting
|
|
257
|
+
|
|
252
258
|
This setting tells Nuxt Content to ignore anything that is **not** one of these file extensions:
|
|
253
259
|
|
|
254
260
|
```
|
|
@@ -257,8 +263,6 @@ md csv ya?ml json
|
|
|
257
263
|
|
|
258
264
|
This way, you can use any **other** file type as an asset, without needing to explicitly configure extensions.
|
|
259
265
|
|
|
260
|
-
Generally, you shouldn't need to touch this setting.
|
|
261
|
-
|
|
262
266
|
### Debug
|
|
263
267
|
|
|
264
268
|
If you want to see what the module does as it runs, set `debug` to true:
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -119,34 +119,15 @@ function getAssetPaths(srcDir, srcAbs) {
|
|
|
119
119
|
srcAttr
|
|
120
120
|
};
|
|
121
121
|
}
|
|
122
|
-
function getAssetSizes(srcAbs
|
|
123
|
-
|
|
124
|
-
let height = void 0;
|
|
125
|
-
let ratio = void 0;
|
|
126
|
-
let query = void 0;
|
|
127
|
-
if (hints.length && isImage(srcAbs)) {
|
|
122
|
+
function getAssetSizes(srcAbs) {
|
|
123
|
+
if (isImage(srcAbs)) {
|
|
128
124
|
try {
|
|
129
|
-
|
|
130
|
-
if (hints.includes("attrs")) {
|
|
131
|
-
width = size.width;
|
|
132
|
-
height = size.height;
|
|
133
|
-
}
|
|
134
|
-
if (hints.includes("style")) {
|
|
135
|
-
ratio = `${size.width}/${size.height}`;
|
|
136
|
-
}
|
|
137
|
-
if (hints.includes("src") || hints.includes("url")) {
|
|
138
|
-
query = `width=${size.width}&height=${size.height}`;
|
|
139
|
-
}
|
|
125
|
+
return getImageSize(srcAbs);
|
|
140
126
|
} catch (err) {
|
|
141
127
|
warn(`could not read image "${srcAbs}`);
|
|
142
128
|
}
|
|
143
129
|
}
|
|
144
|
-
return {
|
|
145
|
-
width,
|
|
146
|
-
height,
|
|
147
|
-
ratio,
|
|
148
|
-
query
|
|
149
|
-
};
|
|
130
|
+
return {};
|
|
150
131
|
}
|
|
151
132
|
|
|
152
133
|
function isAssetId(id) {
|
|
@@ -395,14 +376,11 @@ const module = defineNuxtModule({
|
|
|
395
376
|
}
|
|
396
377
|
function updateAsset(src) {
|
|
397
378
|
const { srcRel, srcAttr } = getAssetPaths(publicPath, src);
|
|
398
|
-
const { width, height
|
|
379
|
+
const { width, height } = getAssetSizes(src);
|
|
399
380
|
assets[srcRel] = {
|
|
400
|
-
srcRel,
|
|
401
381
|
srcAttr,
|
|
402
382
|
width,
|
|
403
|
-
height
|
|
404
|
-
ratio,
|
|
405
|
-
query
|
|
383
|
+
height
|
|
406
384
|
};
|
|
407
385
|
saveAssets();
|
|
408
386
|
return srcAttr;
|
|
@@ -444,6 +422,7 @@ const module = defineNuxtModule({
|
|
|
444
422
|
const makeVar = (name, value) => `export const ${name} = ${JSON.stringify(value)};`;
|
|
445
423
|
const virtualConfig = [
|
|
446
424
|
makeVar("cachePath", cachePath),
|
|
425
|
+
makeVar("imageFlags", imageFlags),
|
|
447
426
|
makeVar("debug", options.debug)
|
|
448
427
|
].join("\n");
|
|
449
428
|
nuxt.hook("nitro:config", async (config) => {
|
package/dist/runtime/plugin.mjs
CHANGED
|
@@ -12,7 +12,7 @@ import {
|
|
|
12
12
|
buildQuery,
|
|
13
13
|
parseQuery
|
|
14
14
|
} from "./utils/index.mjs";
|
|
15
|
-
import {
|
|
15
|
+
import { cachePath, imageFlags, debug } from "#nuxt-content-assets";
|
|
16
16
|
import { makeStorage } from "./services/index.mjs";
|
|
17
17
|
async function updateAssets() {
|
|
18
18
|
assets = await storage.getItem("assets.json");
|
|
@@ -62,8 +62,9 @@ const plugin = async (nitro) => {
|
|
|
62
62
|
const filter = (value, key) => !(String(key).startsWith("_") || key === "body");
|
|
63
63
|
walk(file, (value, parent, key) => {
|
|
64
64
|
if (isValidAsset(value)) {
|
|
65
|
-
const { srcAttr,
|
|
65
|
+
const { srcAttr, width, height } = getAsset(removeQuery(value));
|
|
66
66
|
if (srcAttr) {
|
|
67
|
+
const query = width && height && (imageFlags.includes("src") || imageFlags.includes("url")) ? `width=${width}&height=${height}` : "";
|
|
67
68
|
const srcUrl = query ? buildQuery(srcAttr, parseQuery(value), query) : srcAttr;
|
|
68
69
|
parent[key] = srcUrl;
|
|
69
70
|
updated.push(`meta: ${key} to "${srcUrl}"`);
|
|
@@ -84,24 +85,26 @@ const plugin = async (nitro) => {
|
|
|
84
85
|
if (typeof value !== "string") {
|
|
85
86
|
return;
|
|
86
87
|
}
|
|
87
|
-
const { srcAttr, width, height
|
|
88
|
+
const { srcAttr, width, height } = getAsset(value);
|
|
88
89
|
if (srcAttr) {
|
|
89
90
|
node.props[prop] = srcAttr;
|
|
90
91
|
if (node.tag === "img") {
|
|
91
92
|
if (width && height) {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
if (
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
93
|
+
if (imageFlags.includes("attrs")) {
|
|
94
|
+
node.props.width ||= width;
|
|
95
|
+
node.props.height ||= height;
|
|
96
|
+
}
|
|
97
|
+
if (imageFlags.includes("style")) {
|
|
98
|
+
const ratio = `${width}/${height}`;
|
|
99
|
+
if (typeof node.props.style === "string") {
|
|
100
|
+
node.props.style = buildStyle(node.props.style, `aspect-ratio: ${ratio}`);
|
|
101
|
+
} else {
|
|
102
|
+
node.props.style ||= {};
|
|
103
|
+
node.props.style.aspectRatio = ratio;
|
|
104
|
+
}
|
|
101
105
|
}
|
|
102
106
|
}
|
|
103
|
-
}
|
|
104
|
-
if (node.tag === "a") {
|
|
107
|
+
} else if (node.tag === "a") {
|
|
105
108
|
node.props.target ||= "_blank";
|
|
106
109
|
}
|
|
107
110
|
updated.push(`page: ${tag}[${prop}] to "${srcAttr}"`);
|
|
@@ -1,11 +1,7 @@
|
|
|
1
1
|
export type AssetConfig = {
|
|
2
|
-
id?: string;
|
|
3
|
-
srcRel: string;
|
|
4
2
|
srcAttr: string;
|
|
5
3
|
width?: number;
|
|
6
4
|
height?: number;
|
|
7
|
-
ratio?: string;
|
|
8
|
-
query?: string;
|
|
9
5
|
};
|
|
10
6
|
/**
|
|
11
7
|
* Parse asset paths from absolute path
|
|
@@ -22,11 +18,8 @@ export declare function getAssetPaths(srcDir: string, srcAbs: string): {
|
|
|
22
18
|
* Get asset image sizes
|
|
23
19
|
*
|
|
24
20
|
* @param srcAbs The absolute path to the asset itself
|
|
25
|
-
* @param hints A list of named image size hints, one of 'attrs', 'style', or 'src'
|
|
26
21
|
*/
|
|
27
|
-
export declare function getAssetSizes(srcAbs: string
|
|
28
|
-
width
|
|
29
|
-
height
|
|
30
|
-
ratio: string | undefined;
|
|
31
|
-
query: string | undefined;
|
|
22
|
+
export declare function getAssetSizes(srcAbs: string): {
|
|
23
|
+
width?: number;
|
|
24
|
+
height?: number;
|
|
32
25
|
};
|
|
@@ -11,32 +11,13 @@ export function getAssetPaths(srcDir, srcAbs) {
|
|
|
11
11
|
srcAttr
|
|
12
12
|
};
|
|
13
13
|
}
|
|
14
|
-
export function getAssetSizes(srcAbs
|
|
15
|
-
|
|
16
|
-
let height = void 0;
|
|
17
|
-
let ratio = void 0;
|
|
18
|
-
let query = void 0;
|
|
19
|
-
if (hints.length && isImage(srcAbs)) {
|
|
14
|
+
export function getAssetSizes(srcAbs) {
|
|
15
|
+
if (isImage(srcAbs)) {
|
|
20
16
|
try {
|
|
21
|
-
|
|
22
|
-
if (hints.includes("attrs")) {
|
|
23
|
-
width = size.width;
|
|
24
|
-
height = size.height;
|
|
25
|
-
}
|
|
26
|
-
if (hints.includes("style")) {
|
|
27
|
-
ratio = `${size.width}/${size.height}`;
|
|
28
|
-
}
|
|
29
|
-
if (hints.includes("src") || hints.includes("url")) {
|
|
30
|
-
query = `width=${size.width}&height=${size.height}`;
|
|
31
|
-
}
|
|
17
|
+
return getImageSize(srcAbs);
|
|
32
18
|
} catch (err) {
|
|
33
19
|
warn(`could not read image "${srcAbs}`);
|
|
34
20
|
}
|
|
35
21
|
}
|
|
36
|
-
return {
|
|
37
|
-
width,
|
|
38
|
-
height,
|
|
39
|
-
ratio,
|
|
40
|
-
query
|
|
41
|
-
};
|
|
22
|
+
return {};
|
|
42
23
|
}
|