nuxt-content-assets 1.4.4 → 1.5.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/dist/module.d.mts +1 -27
- package/dist/module.json +6 -2
- package/dist/module.mjs +10 -438
- package/dist/runtime/assets/public.d.ts +1 -1
- package/dist/runtime/assets/{public.mjs → public.js} +4 -4
- package/dist/runtime/assets/{source.mjs → source.js} +1 -1
- package/dist/runtime/content/parsed.d.ts +1 -1
- package/dist/runtime/content/{parsed.mjs → parsed.js} +1 -1
- package/dist/runtime/content/{plugin.mjs → plugin.js} +2 -2
- package/dist/runtime/sockets/factory.d.ts +1 -1
- package/dist/runtime/sockets/{factory.mjs → factory.js} +2 -2
- package/dist/runtime/sockets/plugin.d.ts +1 -1
- package/dist/runtime/sockets/{plugin.mjs → plugin.js} +1 -1
- package/dist/runtime/sockets/setup.d.ts +1 -1
- package/dist/runtime/sockets/{setup.mjs → setup.js} +1 -1
- package/dist/runtime/utils/build.d.ts +1 -1
- package/dist/runtime/utils/{config.mjs → config.js} +1 -1
- package/dist/runtime/utils/content.d.ts +2 -2
- package/dist/runtime/utils/{content.mjs → content.js} +2 -2
- package/dist/runtime/utils/fs.d.ts +1 -1
- package/dist/runtime/utils/index.d.ts +8 -8
- package/dist/runtime/utils/index.js +8 -0
- package/dist/runtime/utils/{path.mjs → path.js} +1 -1
- package/dist/runtime/utils/string.d.ts +1 -1
- package/dist/types.d.mts +4 -5
- package/package.json +29 -29
- package/dist/module.cjs +0 -5
- package/dist/module.d.ts +0 -29
- package/dist/runtime/utils/index.mjs +0 -8
- package/dist/types.d.ts +0 -8
- /package/dist/runtime/utils/{build.mjs → build.js} +0 -0
- /package/dist/runtime/utils/{debug.mjs → debug.js} +0 -0
- /package/dist/runtime/utils/{fs.mjs → fs.js} +0 -0
- /package/dist/runtime/utils/{object.mjs → object.js} +0 -0
- /package/dist/runtime/utils/{string.mjs → string.js} +0 -0
package/dist/module.d.mts
CHANGED
|
@@ -1,29 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
interface ModuleOptions {
|
|
4
|
-
/**
|
|
5
|
-
* Image size hints
|
|
6
|
-
*
|
|
7
|
-
* @example 'attrs style url'
|
|
8
|
-
* @default 'style'
|
|
9
|
-
*/
|
|
10
|
-
imageSize?: string | string[] | false;
|
|
11
|
-
/**
|
|
12
|
-
* List of content extensions; anything else as an asset
|
|
13
|
-
*
|
|
14
|
-
* @example 'md'
|
|
15
|
-
* @default 'md csv ya?ml json'
|
|
16
|
-
*/
|
|
17
|
-
contentExtensions?: string | string[];
|
|
18
|
-
/**
|
|
19
|
-
* Display debug messages
|
|
20
|
-
*
|
|
21
|
-
* @example true
|
|
22
|
-
* @default false
|
|
23
|
-
*/
|
|
24
|
-
debug?: boolean;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
|
|
1
|
+
declare const _default: any;
|
|
28
2
|
|
|
29
3
|
export { _default as default };
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -1,204 +1,12 @@
|
|
|
1
1
|
import * as Fs from 'fs';
|
|
2
|
-
import Fs__default from 'fs';
|
|
3
2
|
import Path from 'crosspath';
|
|
4
3
|
import { useNuxt, createResolver, defineNuxtModule, addPlugin } from '@nuxt/kit';
|
|
5
|
-
import {
|
|
4
|
+
import { isObject, log, warn, removeEntry, makeIgnores, matchTokens, list, isImage, toPath } from '../dist/runtime/utils/index.js';
|
|
6
5
|
import { listen } from 'listhen';
|
|
7
6
|
import { WebSocketServer, WebSocket } from 'ws';
|
|
8
|
-
import
|
|
9
|
-
import
|
|
10
|
-
import {
|
|
11
|
-
import getImageSize from 'image-size';
|
|
12
|
-
import debounce from 'debounce';
|
|
13
|
-
import 'ohash';
|
|
14
|
-
|
|
15
|
-
function matchTokens(value) {
|
|
16
|
-
let tokens = [];
|
|
17
|
-
if (typeof value === "string") {
|
|
18
|
-
tokens = value.match(/[^\s,|]+/g) || [];
|
|
19
|
-
} else if (Array.isArray(value)) {
|
|
20
|
-
tokens = value.filter((value2) => typeof value2 === "string").reduce((output, input) => {
|
|
21
|
-
return [...output, ...matchTokens(input)];
|
|
22
|
-
}, []);
|
|
23
|
-
} else if (!!value && typeof value === "object") {
|
|
24
|
-
tokens = Object.values(value).reduce((output, value2) => {
|
|
25
|
-
return [...output, ...matchTokens(value2)];
|
|
26
|
-
}, []);
|
|
27
|
-
}
|
|
28
|
-
return tokens.length ? Array.from(new Set(tokens)) : tokens;
|
|
29
|
-
}
|
|
30
|
-
function toPath(key) {
|
|
31
|
-
return key.replaceAll(":", "/");
|
|
32
|
-
}
|
|
33
|
-
function deKey(path) {
|
|
34
|
-
return path.replace(/^[^:]+:/, "");
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const extensions = {
|
|
38
|
-
// used to get image size
|
|
39
|
-
image: matchTokens("png jpg jpeg gif svg webp ico"),
|
|
40
|
-
// used to recognise content
|
|
41
|
-
content: matchTokens("md mdx json yml yaml csv"),
|
|
42
|
-
// unused for now
|
|
43
|
-
media: matchTokens("mp3 m4a wav mp4 mov webm ogg avi flv avchd")
|
|
44
|
-
};
|
|
45
|
-
function makeIgnores(extensions2) {
|
|
46
|
-
const included = matchTokens(extensions2).join("|");
|
|
47
|
-
return `^(?:(?!(${included})).)+$`;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function removeQuery(path) {
|
|
51
|
-
return path.replace(/\?.*$/, "");
|
|
52
|
-
}
|
|
53
|
-
function getExt(path) {
|
|
54
|
-
return Path.extname(removeQuery(path)).substring(1);
|
|
55
|
-
}
|
|
56
|
-
function isExcluded(path) {
|
|
57
|
-
return path.split("/").some((segment) => segment.startsWith(".") || segment.startsWith("_"));
|
|
58
|
-
}
|
|
59
|
-
function isImage(path) {
|
|
60
|
-
return extensions.image.includes(getExt(path));
|
|
61
|
-
}
|
|
62
|
-
function isArticle(path) {
|
|
63
|
-
return extensions.content.includes(getExt(path));
|
|
64
|
-
}
|
|
65
|
-
function isAsset(path) {
|
|
66
|
-
return !isArticle(path);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function walk(node, callback, filter) {
|
|
70
|
-
function visit(node2, callback2, parent, key) {
|
|
71
|
-
if (filter) {
|
|
72
|
-
const result = filter(node2, key);
|
|
73
|
-
if (result === false) {
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
if (Array.isArray(node2)) {
|
|
78
|
-
node2.forEach((value, index) => {
|
|
79
|
-
visit(value, callback2, node2, index);
|
|
80
|
-
});
|
|
81
|
-
} else if (isObject(node2)) {
|
|
82
|
-
Object.keys(node2).forEach((key2) => {
|
|
83
|
-
visit(node2[key2], callback2, node2, key2);
|
|
84
|
-
});
|
|
85
|
-
} else {
|
|
86
|
-
callback2(node2, parent, key);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
visit(node, callback, { node }, "node");
|
|
90
|
-
}
|
|
91
|
-
function isObject(data) {
|
|
92
|
-
return data && typeof data === "object" && !Array.isArray(data);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
function walkMeta(content, callback) {
|
|
96
|
-
walk(content, callback, (value, key) => !(String(key).startsWith("_") || key === "body"));
|
|
97
|
-
}
|
|
98
|
-
function walkBody(content, callback) {
|
|
99
|
-
visit(content.body, (node) => node.type === "element", (node) => {
|
|
100
|
-
const { tag, props } = node;
|
|
101
|
-
const excluded = tags.exclude.includes(tag);
|
|
102
|
-
if (excluded) {
|
|
103
|
-
return SKIP;
|
|
104
|
-
}
|
|
105
|
-
const included = tags.include.includes(tag);
|
|
106
|
-
if (included || !props) {
|
|
107
|
-
return CONTINUE;
|
|
108
|
-
}
|
|
109
|
-
callback(node);
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
const tags = {
|
|
113
|
-
// unlikely to contain assets
|
|
114
|
-
exclude: matchTokens({
|
|
115
|
-
container: "pre code code-inline",
|
|
116
|
-
formatting: "acronym abbr address bdi bdo big center cite del dfn font ins kbd mark meter progress q rp rt ruby s samp small strike sub sup time tt u var wbr",
|
|
117
|
-
headers: "h1 h2 h3 h4 h5 h6",
|
|
118
|
-
controls: "input textarea button select optgroup option label legend datalist output",
|
|
119
|
-
media: "map area canvas svg",
|
|
120
|
-
other: "style script noscript template",
|
|
121
|
-
empty: "hr br"
|
|
122
|
-
}),
|
|
123
|
-
// may contain assets
|
|
124
|
-
include: matchTokens({
|
|
125
|
-
content: "main header footer section article aside details dialog summary data object nav blockquote div span p",
|
|
126
|
-
table: "table caption th tr td thead tbody tfoot col colgroup",
|
|
127
|
-
media: "figcaption figure picture",
|
|
128
|
-
form: "form fieldset",
|
|
129
|
-
list: "ul ol li dir dl dt dd",
|
|
130
|
-
formatting: "strong b em i"
|
|
131
|
-
}),
|
|
132
|
-
// assets
|
|
133
|
-
assets: "a img audio source track video embed"
|
|
134
|
-
};
|
|
135
|
-
|
|
136
|
-
const label = "[content-assets]";
|
|
137
|
-
function log(...data) {
|
|
138
|
-
console.info(label, ...data);
|
|
139
|
-
}
|
|
140
|
-
function warn(...data) {
|
|
141
|
-
console.warn(label, ...data);
|
|
142
|
-
}
|
|
143
|
-
function list(message, items) {
|
|
144
|
-
log(`${message}:
|
|
145
|
-
|
|
146
|
-
${items.map((item) => ` - ${item}`).join("\n")}
|
|
147
|
-
`);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
function buildQuery(...expr) {
|
|
151
|
-
const output = expr.map((expr2) => expr2.replace(/^[?&]+|&+$/g, "")).filter((s) => s);
|
|
152
|
-
if (output.length) {
|
|
153
|
-
const [first, ...rest] = output;
|
|
154
|
-
const isParam = (expr2) => /^[^?]+=[^=]+$/.test(expr2);
|
|
155
|
-
return !isParam(first) ? rest.length > 0 ? first + (first.includes("?") ? "&" : "?") + rest.join("&") : first : "?" + output.join("&");
|
|
156
|
-
}
|
|
157
|
-
return "";
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
function readFile(path, asJson = false) {
|
|
161
|
-
const text = Fs__default.readFileSync(path, { encoding: "utf8" });
|
|
162
|
-
return asJson ? JSON.parse(text) : text;
|
|
163
|
-
}
|
|
164
|
-
function writeFile(path, data) {
|
|
165
|
-
const text = typeof data === "object" ? JSON.stringify(data, null, " ") : String(data);
|
|
166
|
-
createFolder(Path.dirname(path));
|
|
167
|
-
Fs__default.writeFileSync(path, text, { encoding: "utf8" });
|
|
168
|
-
}
|
|
169
|
-
async function writeBlob(path, data) {
|
|
170
|
-
const buffer = Buffer.from(await data.arrayBuffer());
|
|
171
|
-
createFolder(Path.dirname(path));
|
|
172
|
-
Fs__default.writeFileSync(path, buffer);
|
|
173
|
-
}
|
|
174
|
-
function copyFile(src, trg) {
|
|
175
|
-
createFolder(Path.dirname(trg));
|
|
176
|
-
Fs__default.copyFileSync(src, trg);
|
|
177
|
-
}
|
|
178
|
-
function removeFile(src) {
|
|
179
|
-
Fs__default.rmSync(src);
|
|
180
|
-
}
|
|
181
|
-
function createFolder(path) {
|
|
182
|
-
Fs__default.mkdirSync(path, { recursive: true });
|
|
183
|
-
}
|
|
184
|
-
function removeFolder(path) {
|
|
185
|
-
const isDownstream = path.startsWith(Path.resolve());
|
|
186
|
-
if (isDownstream) {
|
|
187
|
-
Fs__default.rmSync(path, { recursive: true, force: true });
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
function removeEntry(path) {
|
|
191
|
-
if (Fs__default.existsSync(path)) {
|
|
192
|
-
if (isFile(path)) {
|
|
193
|
-
removeFile(path);
|
|
194
|
-
} else {
|
|
195
|
-
removeFolder(path);
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
function isFile(path) {
|
|
200
|
-
return Fs__default.lstatSync(path).isFile();
|
|
201
|
-
}
|
|
7
|
+
import { makeSourceManager } from '../dist/runtime/assets/source.js';
|
|
8
|
+
import { makeAssetsManager } from '../dist/runtime/assets/public.js';
|
|
9
|
+
import { rewriteContent } from '../dist/runtime/content/parsed.js';
|
|
202
10
|
|
|
203
11
|
function createWebSocket() {
|
|
204
12
|
const wss = new WebSocketServer({ noServer: true });
|
|
@@ -220,7 +28,7 @@ function createWebSocket() {
|
|
|
220
28
|
let data;
|
|
221
29
|
try {
|
|
222
30
|
data = JSON.parse(event.data || "{}");
|
|
223
|
-
} catch
|
|
31
|
+
} catch {
|
|
224
32
|
}
|
|
225
33
|
if (data) {
|
|
226
34
|
handlers.forEach((callback) => callback(data));
|
|
@@ -300,254 +108,18 @@ async function setupSocketServer(channel, handler) {
|
|
|
300
108
|
return this;
|
|
301
109
|
}
|
|
302
110
|
};
|
|
303
|
-
if (handler) {
|
|
304
|
-
instance.addHandler(handler);
|
|
305
|
-
}
|
|
306
111
|
return instance;
|
|
307
112
|
}
|
|
308
113
|
|
|
309
|
-
function isAssetId(id) {
|
|
310
|
-
const path = toPath(id);
|
|
311
|
-
return !isExcluded(path) && isAsset(path);
|
|
312
|
-
}
|
|
313
|
-
function makeSourceStorage(source, key = "") {
|
|
314
|
-
const storage = createStorage();
|
|
315
|
-
const options = typeof source === "string" ? { driver: "fs", base: source } : source;
|
|
316
|
-
switch (options.driver) {
|
|
317
|
-
case "fs":
|
|
318
|
-
storage.mount(key, fsDriver({
|
|
319
|
-
...options,
|
|
320
|
-
ignore: [
|
|
321
|
-
"[^:]+?\\.md",
|
|
322
|
-
"_dir\\.yml"
|
|
323
|
-
]
|
|
324
|
-
}));
|
|
325
|
-
break;
|
|
326
|
-
case "github":
|
|
327
|
-
storage.mount(key, githubDriver({
|
|
328
|
-
branch: "main",
|
|
329
|
-
dir: "/",
|
|
330
|
-
...options
|
|
331
|
-
}));
|
|
332
|
-
break;
|
|
333
|
-
}
|
|
334
|
-
return storage;
|
|
335
|
-
}
|
|
336
|
-
function makeSourceManager(key, source, publicPath, callback) {
|
|
337
|
-
async function onWatch(event, key2) {
|
|
338
|
-
if (isAssetId(key2)) {
|
|
339
|
-
const path = event === "update" ? await copyItem(key2) : removeItem(key2);
|
|
340
|
-
if (callback) {
|
|
341
|
-
callback(event, path);
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
}
|
|
345
|
-
function getRelSrc(key2) {
|
|
346
|
-
return toPath(key2).replace(/\w+/, "").replace(source.prefix || "", "");
|
|
347
|
-
}
|
|
348
|
-
function getAbsSrc(key2) {
|
|
349
|
-
return Path.join(source.base, getRelSrc(key2));
|
|
350
|
-
}
|
|
351
|
-
function getRelTrg(key2) {
|
|
352
|
-
return Path.join(source.prefix || "", toPath(deKey(key2)));
|
|
353
|
-
}
|
|
354
|
-
function getAbsTrg(key2) {
|
|
355
|
-
return Path.join(publicPath, getRelTrg(key2));
|
|
356
|
-
}
|
|
357
|
-
function removeItem(key2) {
|
|
358
|
-
const absTrg = getAbsTrg(key2);
|
|
359
|
-
removeFile(absTrg);
|
|
360
|
-
return absTrg;
|
|
361
|
-
}
|
|
362
|
-
async function copyItem(key2) {
|
|
363
|
-
const absTrg = getAbsTrg(key2);
|
|
364
|
-
const driver = source.driver;
|
|
365
|
-
if (driver === "fs") {
|
|
366
|
-
const absSrc = getAbsSrc(key2);
|
|
367
|
-
copyFile(absSrc, absTrg);
|
|
368
|
-
} else if (driver === "github") {
|
|
369
|
-
try {
|
|
370
|
-
const data = await storage.getItem(key2);
|
|
371
|
-
if (data) {
|
|
372
|
-
data?.constructor.name === "Blob" ? await writeBlob(absTrg, data) : writeFile(absTrg, data);
|
|
373
|
-
} else {
|
|
374
|
-
warn(`No data for key "${key2}"`);
|
|
375
|
-
}
|
|
376
|
-
} catch (err) {
|
|
377
|
-
warn(err.message);
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
return absTrg;
|
|
381
|
-
}
|
|
382
|
-
async function getKeys() {
|
|
383
|
-
const keys = await storage.getKeys();
|
|
384
|
-
return keys.filter(isAssetId);
|
|
385
|
-
}
|
|
386
|
-
async function init() {
|
|
387
|
-
const keys = await getKeys();
|
|
388
|
-
const paths = [];
|
|
389
|
-
for (const key2 of keys) {
|
|
390
|
-
const path = await copyItem(key2);
|
|
391
|
-
paths.push(path);
|
|
392
|
-
}
|
|
393
|
-
return paths;
|
|
394
|
-
}
|
|
395
|
-
const storage = makeSourceStorage(source, key);
|
|
396
|
-
void storage.watch(onWatch);
|
|
397
|
-
async function dispose() {
|
|
398
|
-
await storage.unwatch();
|
|
399
|
-
await storage.dispose();
|
|
400
|
-
}
|
|
401
|
-
return {
|
|
402
|
-
storage,
|
|
403
|
-
init,
|
|
404
|
-
keys: getKeys,
|
|
405
|
-
dispose
|
|
406
|
-
};
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
function makeAssetsManager(publicPath, shouldWatch = true) {
|
|
410
|
-
const assetsKey = "assets.json";
|
|
411
|
-
const assetsPath = Path.join(publicPath, "..");
|
|
412
|
-
const storage = makeSourceStorage(assetsPath);
|
|
413
|
-
if (shouldWatch) {
|
|
414
|
-
void storage.watch(async (event, key) => {
|
|
415
|
-
if (event === "update" && key === assetsKey) {
|
|
416
|
-
await load();
|
|
417
|
-
}
|
|
418
|
-
});
|
|
419
|
-
}
|
|
420
|
-
const assets = {};
|
|
421
|
-
async function load() {
|
|
422
|
-
const data = await storage.getItem(assetsKey);
|
|
423
|
-
Object.assign(assets, data || {});
|
|
424
|
-
}
|
|
425
|
-
const save = debounce(function() {
|
|
426
|
-
void storage.setItem(assetsKey, assets);
|
|
427
|
-
}, 50);
|
|
428
|
-
function resolveAsset(content, relAsset, registerContent = false) {
|
|
429
|
-
const srcDir = Path.dirname(content._file);
|
|
430
|
-
const srcAsset = Path.join(srcDir, relAsset);
|
|
431
|
-
const asset = assets[srcAsset];
|
|
432
|
-
if (asset && registerContent) {
|
|
433
|
-
const { _id } = content;
|
|
434
|
-
if (!asset.content.includes(_id)) {
|
|
435
|
-
asset.content.push(_id);
|
|
436
|
-
save();
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
return asset || {};
|
|
440
|
-
}
|
|
441
|
-
function setAsset(path) {
|
|
442
|
-
const { srcRel, srcAttr } = getAssetPaths(publicPath, path);
|
|
443
|
-
const { width, height } = getAssetSize(path);
|
|
444
|
-
const oldAsset = assets[srcRel];
|
|
445
|
-
const newAsset = {
|
|
446
|
-
srcAttr,
|
|
447
|
-
content: oldAsset?.content || [],
|
|
448
|
-
width,
|
|
449
|
-
height
|
|
450
|
-
};
|
|
451
|
-
assets[srcRel] = newAsset;
|
|
452
|
-
save();
|
|
453
|
-
return newAsset;
|
|
454
|
-
}
|
|
455
|
-
function getAsset(path) {
|
|
456
|
-
const { srcRel } = getAssetPaths(publicPath, path);
|
|
457
|
-
return srcRel ? { ...assets[srcRel] } : void 0;
|
|
458
|
-
}
|
|
459
|
-
function removeAsset(path) {
|
|
460
|
-
const { srcRel } = getAssetPaths(publicPath, path);
|
|
461
|
-
const asset = assets[srcRel];
|
|
462
|
-
if (asset) {
|
|
463
|
-
delete assets[srcRel];
|
|
464
|
-
save();
|
|
465
|
-
}
|
|
466
|
-
return asset;
|
|
467
|
-
}
|
|
468
|
-
const init = () => {
|
|
469
|
-
if (Fs.existsSync(publicPath)) {
|
|
470
|
-
const names = Fs.readdirSync(publicPath);
|
|
471
|
-
for (const name of names) {
|
|
472
|
-
if (!/^\.git(ignore|keep)$/.test(name)) {
|
|
473
|
-
removeEntry(Path.join(publicPath, name));
|
|
474
|
-
}
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
};
|
|
478
|
-
void load();
|
|
479
|
-
return {
|
|
480
|
-
init,
|
|
481
|
-
setAsset,
|
|
482
|
-
getAsset,
|
|
483
|
-
removeAsset,
|
|
484
|
-
resolveAsset,
|
|
485
|
-
dispose: async () => {
|
|
486
|
-
await storage.unwatch();
|
|
487
|
-
await storage.dispose();
|
|
488
|
-
}
|
|
489
|
-
};
|
|
490
|
-
}
|
|
491
|
-
function getAssetPaths(srcDir, srcAbs) {
|
|
492
|
-
const srcRel = Path.relative(srcDir, srcAbs);
|
|
493
|
-
const srcAttr = "/" + srcRel;
|
|
494
|
-
return {
|
|
495
|
-
srcRel,
|
|
496
|
-
srcAttr
|
|
497
|
-
};
|
|
498
|
-
}
|
|
499
|
-
function getAssetSize(srcAbs) {
|
|
500
|
-
if (isImage(srcAbs)) {
|
|
501
|
-
try {
|
|
502
|
-
return getImageSize(srcAbs);
|
|
503
|
-
} catch (err) {
|
|
504
|
-
warn(`could not read image "${srcAbs}`);
|
|
505
|
-
}
|
|
506
|
-
}
|
|
507
|
-
return {};
|
|
508
|
-
}
|
|
509
|
-
|
|
510
|
-
function rewriteContent(path, asset) {
|
|
511
|
-
const { parsed } = readFile(path, true);
|
|
512
|
-
const { srcAttr, width, height } = asset;
|
|
513
|
-
walkMeta(parsed, (value, parent, key) => {
|
|
514
|
-
if (value.startsWith(srcAttr)) {
|
|
515
|
-
parent[key] = parent[key].replace(/width=\d+&height=\d+/, `width=${width}&height=${height}`);
|
|
516
|
-
}
|
|
517
|
-
});
|
|
518
|
-
walkBody(parsed, function(node) {
|
|
519
|
-
const { tag, props } = node;
|
|
520
|
-
if (tag === "img" && props?.src?.startsWith(srcAttr)) {
|
|
521
|
-
props.src = buildQuery(srcAttr, `time=${Date.now()}`);
|
|
522
|
-
if (props.width) {
|
|
523
|
-
props.width = width;
|
|
524
|
-
}
|
|
525
|
-
if (props.height) {
|
|
526
|
-
props.height = height;
|
|
527
|
-
}
|
|
528
|
-
if (props.style) {
|
|
529
|
-
const ratio = `${width}/${height}`;
|
|
530
|
-
if (typeof props.style === "string") {
|
|
531
|
-
props.style = props.style.replace(/aspect-ratio: \d+\/\d+/, `aspect-ratio: ${ratio}`);
|
|
532
|
-
} else if (props.style.aspectRatio) {
|
|
533
|
-
props.style.aspectRatio = ratio;
|
|
534
|
-
}
|
|
535
|
-
}
|
|
536
|
-
}
|
|
537
|
-
});
|
|
538
|
-
writeFile(path, { module: true, parsed });
|
|
539
|
-
return parsed;
|
|
540
|
-
}
|
|
541
|
-
|
|
542
114
|
const resolve = createResolver(import.meta.url).resolve;
|
|
543
115
|
const meta = {
|
|
544
116
|
name: "nuxt-content-assets",
|
|
545
117
|
configKey: "contentAssets",
|
|
546
118
|
compatibility: {
|
|
547
|
-
nuxt: "
|
|
119
|
+
nuxt: ">=3.0.0"
|
|
548
120
|
}
|
|
549
121
|
};
|
|
550
|
-
const module = defineNuxtModule({
|
|
122
|
+
const module$1 = defineNuxtModule({
|
|
551
123
|
meta,
|
|
552
124
|
defaults: {
|
|
553
125
|
imageSize: "",
|
|
@@ -559,7 +131,7 @@ const module = defineNuxtModule({
|
|
|
559
131
|
const modulesPath = nuxt.options.modulesDir.find((path) => Fs.existsSync(`${path}/nuxt-content-assets/cache`)) || "";
|
|
560
132
|
if (!modulesPath) {
|
|
561
133
|
warn("Unable to find cache folder!");
|
|
562
|
-
if (nuxt.options.
|
|
134
|
+
if (nuxt.options.rootDir.endsWith("/playground")) {
|
|
563
135
|
warn('Run "npm run dev:setup" to generate a new cache folder');
|
|
564
136
|
}
|
|
565
137
|
}
|
|
@@ -590,7 +162,7 @@ const module = defineNuxtModule({
|
|
|
590
162
|
return output;
|
|
591
163
|
}, {});
|
|
592
164
|
if (Object.keys(sources).length === 0 || !sources.content) {
|
|
593
|
-
const content = nuxt.options.
|
|
165
|
+
const content = nuxt.options.rootDir + "/content";
|
|
594
166
|
if (Fs.existsSync(content)) {
|
|
595
167
|
sources.content = {
|
|
596
168
|
driver: "fs",
|
|
@@ -676,4 +248,4 @@ const module = defineNuxtModule({
|
|
|
676
248
|
}
|
|
677
249
|
});
|
|
678
250
|
|
|
679
|
-
export { module as default };
|
|
251
|
+
export { module$1 as default };
|
|
@@ -3,8 +3,8 @@ import Path from "crosspath";
|
|
|
3
3
|
import getImageSize from "image-size";
|
|
4
4
|
import debounce from "debounce";
|
|
5
5
|
import { hash } from "ohash";
|
|
6
|
-
import { makeSourceStorage } from "./source.
|
|
7
|
-
import { isImage, warn, log, removeEntry } from "../utils/index.
|
|
6
|
+
import { makeSourceStorage } from "./source.js";
|
|
7
|
+
import { isImage, warn, log, removeEntry } from "../utils/index.js";
|
|
8
8
|
export function makeAssetsManager(publicPath, shouldWatch = true) {
|
|
9
9
|
const assetsKey = "assets.json";
|
|
10
10
|
const assetsPath = Path.join(publicPath, "..");
|
|
@@ -53,7 +53,7 @@ export function makeAssetsManager(publicPath, shouldWatch = true) {
|
|
|
53
53
|
}
|
|
54
54
|
function getAsset(path) {
|
|
55
55
|
const { srcRel } = getAssetPaths(publicPath, path);
|
|
56
|
-
return srcRel ? { ...assets[srcRel] } : void 0;
|
|
56
|
+
return srcRel && assets[srcRel] ? { ...assets[srcRel] } : void 0;
|
|
57
57
|
}
|
|
58
58
|
function removeAsset(path) {
|
|
59
59
|
const { srcRel } = getAssetPaths(publicPath, path);
|
|
@@ -122,7 +122,7 @@ export function getAssetSize(srcAbs) {
|
|
|
122
122
|
if (isImage(srcAbs)) {
|
|
123
123
|
try {
|
|
124
124
|
return getImageSize(srcAbs);
|
|
125
|
-
} catch
|
|
125
|
+
} catch {
|
|
126
126
|
warn(`could not read image "${srcAbs}`);
|
|
127
127
|
}
|
|
128
128
|
}
|
|
@@ -2,7 +2,7 @@ import Path from "crosspath";
|
|
|
2
2
|
import githubDriver from "unstorage/drivers/github";
|
|
3
3
|
import fsDriver from "unstorage/drivers/fs";
|
|
4
4
|
import { createStorage } from "unstorage";
|
|
5
|
-
import { warn, isAsset, toPath, removeFile, copyFile, writeBlob, writeFile, deKey, isExcluded } from "../utils/index.
|
|
5
|
+
import { warn, isAsset, toPath, removeFile, copyFile, writeBlob, writeFile, deKey, isExcluded } from "../utils/index.js";
|
|
6
6
|
function isAssetId(id) {
|
|
7
7
|
const path = toPath(id);
|
|
8
8
|
return !isExcluded(path) && isAsset(path);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { walkBody, walkMeta, buildQuery, readFile, writeFile } from "../utils/index.
|
|
1
|
+
import { walkBody, walkMeta, buildQuery, readFile, writeFile } from "../utils/index.js";
|
|
2
2
|
export function rewriteContent(path, asset) {
|
|
3
3
|
const { parsed } = readFile(path, true);
|
|
4
4
|
const { srcAttr, width, height } = asset;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { buildQuery, buildStyle, isValidAsset, list, parseQuery, removeQuery, walkBody, walkMeta } from "../utils/index.
|
|
2
|
-
import { makeAssetsManager } from "../assets/public.
|
|
1
|
+
import { buildQuery, buildStyle, isValidAsset, list, parseQuery, removeQuery, walkBody, walkMeta } from "../utils/index.js";
|
|
2
|
+
import { makeAssetsManager } from "../assets/public.js";
|
|
3
3
|
import { debug, imageSizes, publicPath } from "#nuxt-content-assets";
|
|
4
4
|
const plugin = async (nitro) => {
|
|
5
5
|
function processMeta(content, imageSizes2 = [], updated = []) {
|
|
@@ -29,7 +29,7 @@ export function createWebSocket(url, logger = { log, warn: log }) {
|
|
|
29
29
|
let data;
|
|
30
30
|
try {
|
|
31
31
|
data = JSON.parse(message.data);
|
|
32
|
-
} catch
|
|
32
|
+
} catch {
|
|
33
33
|
logger.warn("Error parsing message:", message.data);
|
|
34
34
|
return;
|
|
35
35
|
}
|
|
@@ -55,7 +55,7 @@ export function createWebSocket(url, logger = { log, warn: log }) {
|
|
|
55
55
|
if (ws) {
|
|
56
56
|
try {
|
|
57
57
|
ws.close();
|
|
58
|
-
} catch
|
|
58
|
+
} catch {
|
|
59
59
|
}
|
|
60
60
|
ws = void 0;
|
|
61
61
|
}
|
|
@@ -2,7 +2,7 @@ import { defineNuxtPlugin, refreshNuxtData, useRuntimeConfig } from "#imports";
|
|
|
2
2
|
export default defineNuxtPlugin(async () => {
|
|
3
3
|
if (import.meta.client) {
|
|
4
4
|
const url = useRuntimeConfig().public.sockets?.wsUrl;
|
|
5
|
-
const socket = await import("./setup.
|
|
5
|
+
const socket = await import("./setup.js").then(({ setupSocketClient }) => {
|
|
6
6
|
return setupSocketClient(url, "content-assets");
|
|
7
7
|
});
|
|
8
8
|
if (socket) {
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { Callback, SocketInstance } from '../../types';
|
|
1
|
+
import type { Callback, SocketInstance } from '../../types.js';
|
|
2
2
|
export declare function setupSocketClient(url: string, channel: string, callback?: Callback): SocketInstance | null;
|
|
@@ -5,4 +5,4 @@ export declare function buildStyle(...expr: string[]): string;
|
|
|
5
5
|
/**
|
|
6
6
|
* Build a query string by passing multiple independent expressions
|
|
7
7
|
*/
|
|
8
|
-
export declare function buildQuery(...expr: string[]): string;
|
|
8
|
+
export declare function buildQuery(...expr: string[]): string | undefined;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { ParsedContent } from '../../types';
|
|
2
|
-
import { type WalkCallback } from './object';
|
|
1
|
+
import type { ParsedContent } from '../../types.js';
|
|
2
|
+
import { type WalkCallback } from './object.js';
|
|
3
3
|
/**
|
|
4
4
|
* Walk parsed content meta, only processing relevant properties
|
|
5
5
|
*
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { CONTINUE, SKIP, visit } from "unist-util-visit";
|
|
2
|
-
import { walk } from "./object.
|
|
3
|
-
import { matchTokens } from "./string.
|
|
2
|
+
import { walk } from "./object.js";
|
|
3
|
+
import { matchTokens } from "./string.js";
|
|
4
4
|
export function walkMeta(content, callback) {
|
|
5
5
|
walk(content, callback, (value, key) => !(String(key).startsWith("_") || key === "body"));
|
|
6
6
|
}
|
|
@@ -6,4 +6,4 @@ export declare function removeFile(src: string): void;
|
|
|
6
6
|
export declare function createFolder(path: string): void;
|
|
7
7
|
export declare function removeFolder(path: string): void;
|
|
8
8
|
export declare function removeEntry(path: string): void;
|
|
9
|
-
export declare function isFile(path: string):
|
|
9
|
+
export declare function isFile(path: string): boolean;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
export * from './path';
|
|
2
|
-
export * from './content';
|
|
3
|
-
export * from './config';
|
|
4
|
-
export * from './debug';
|
|
5
|
-
export * from './build';
|
|
6
|
-
export * from './fs';
|
|
7
|
-
export * from './string';
|
|
8
|
-
export * from './object';
|
|
1
|
+
export * from './path.js';
|
|
2
|
+
export * from './content.js';
|
|
3
|
+
export * from './config.js';
|
|
4
|
+
export * from './debug.js';
|
|
5
|
+
export * from './build.js';
|
|
6
|
+
export * from './fs.js';
|
|
7
|
+
export * from './string.js';
|
|
8
|
+
export * from './object.js';
|
|
@@ -5,5 +5,5 @@
|
|
|
5
5
|
*/
|
|
6
6
|
export declare function matchTokens(value: any): string[];
|
|
7
7
|
export declare function toPath(key: string): string;
|
|
8
|
-
export declare function toKey(path: string):
|
|
8
|
+
export declare function toKey(path: string): string;
|
|
9
9
|
export declare function deKey(path: string): string;
|
package/dist/types.d.mts
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
|
+
import type { NuxtModule } from '@nuxt/schema'
|
|
1
2
|
|
|
2
|
-
import type {
|
|
3
|
+
import type { default as Module } from './module.mjs'
|
|
3
4
|
|
|
5
|
+
export type ModuleOptions = typeof Module extends NuxtModule<infer O> ? Partial<O> : Record<string, any>
|
|
4
6
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export type { default } from './module.js'
|
|
7
|
+
export { default } from './module.mjs'
|
package/package.json
CHANGED
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nuxt-content-assets",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.1",
|
|
4
4
|
"description": "Enable locally-located assets in Nuxt Content",
|
|
5
|
-
"repository":
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/davestewart/nuxt-content-assets.git"
|
|
8
|
+
},
|
|
6
9
|
"license": "MIT",
|
|
7
10
|
"type": "module",
|
|
8
11
|
"exports": {
|
|
9
12
|
".": {
|
|
10
|
-
"types": "./dist/types.d.
|
|
11
|
-
"import": "./dist/module.mjs"
|
|
12
|
-
"require": "./dist/module.cjs"
|
|
13
|
+
"types": "./dist/types.d.mts",
|
|
14
|
+
"import": "./dist/module.mjs"
|
|
13
15
|
}
|
|
14
16
|
},
|
|
15
|
-
"main": "./dist/module.
|
|
16
|
-
"types": "./dist/types.d.ts",
|
|
17
|
+
"main": "./dist/module.mjs",
|
|
17
18
|
"files": [
|
|
18
19
|
"cache",
|
|
19
20
|
"dist"
|
|
@@ -37,33 +38,32 @@
|
|
|
37
38
|
"test:watch": "vitest watch"
|
|
38
39
|
},
|
|
39
40
|
"dependencies": {
|
|
40
|
-
"@nuxt/kit": "^3.
|
|
41
|
+
"@nuxt/kit": "^3.20.2",
|
|
41
42
|
"crosspath": "^2.0.0",
|
|
42
|
-
"debounce": "^
|
|
43
|
-
"image-size": "^1.
|
|
44
|
-
"listhen": "^1.
|
|
45
|
-
"ohash": "^
|
|
46
|
-
"unist-util-visit": "^
|
|
47
|
-
"unstorage": "^1.
|
|
48
|
-
"ws": "^8.
|
|
43
|
+
"debounce": "^3.0.0",
|
|
44
|
+
"image-size": "^1.1.1",
|
|
45
|
+
"listhen": "^1.9.0",
|
|
46
|
+
"ohash": "^2.0.11",
|
|
47
|
+
"unist-util-visit": "^5.0.0",
|
|
48
|
+
"unstorage": "^1.17.3",
|
|
49
|
+
"ws": "^8.19.0"
|
|
49
50
|
},
|
|
50
51
|
"peerDependencies": {
|
|
51
|
-
"@nuxt/content": "^2.
|
|
52
|
+
"@nuxt/content": "^2.13.0"
|
|
52
53
|
},
|
|
53
54
|
"devDependencies": {
|
|
54
|
-
"@nuxt/devtools": "
|
|
55
|
-
"@nuxt/eslint-config": "^0.
|
|
56
|
-
"@nuxt/module-builder": "^0.
|
|
57
|
-
"@nuxt/schema": "^3.
|
|
58
|
-
"@nuxt/test-utils": "^3.
|
|
59
|
-
"@types/debounce": "^1.2.
|
|
60
|
-
"@types/node": "^
|
|
61
|
-
"@types/ws": "^8.
|
|
62
|
-
"
|
|
63
|
-
"
|
|
64
|
-
"
|
|
65
|
-
"
|
|
66
|
-
"vitest": "^1.4.0"
|
|
55
|
+
"@nuxt/devtools": "^1.7.0",
|
|
56
|
+
"@nuxt/eslint-config": "^0.7.6",
|
|
57
|
+
"@nuxt/module-builder": "^1.0.2",
|
|
58
|
+
"@nuxt/schema": "^3.20.2",
|
|
59
|
+
"@nuxt/test-utils": "^3.23.0",
|
|
60
|
+
"@types/debounce": "^1.2.4",
|
|
61
|
+
"@types/node": "^25.0.3",
|
|
62
|
+
"@types/ws": "^8.18.1",
|
|
63
|
+
"cpy-cli": "^6.0.0",
|
|
64
|
+
"eslint": "^8.39.2",
|
|
65
|
+
"nuxt": "^3.20.2",
|
|
66
|
+
"vitest": "^3.2.4"
|
|
67
67
|
},
|
|
68
68
|
"engines": {
|
|
69
69
|
"node": ">=16.0.0"
|
package/dist/module.cjs
DELETED
package/dist/module.d.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import * as _nuxt_schema from '@nuxt/schema';
|
|
2
|
-
|
|
3
|
-
interface ModuleOptions {
|
|
4
|
-
/**
|
|
5
|
-
* Image size hints
|
|
6
|
-
*
|
|
7
|
-
* @example 'attrs style url'
|
|
8
|
-
* @default 'style'
|
|
9
|
-
*/
|
|
10
|
-
imageSize?: string | string[] | false;
|
|
11
|
-
/**
|
|
12
|
-
* List of content extensions; anything else as an asset
|
|
13
|
-
*
|
|
14
|
-
* @example 'md'
|
|
15
|
-
* @default 'md csv ya?ml json'
|
|
16
|
-
*/
|
|
17
|
-
contentExtensions?: string | string[];
|
|
18
|
-
/**
|
|
19
|
-
* Display debug messages
|
|
20
|
-
*
|
|
21
|
-
* @example true
|
|
22
|
-
* @default false
|
|
23
|
-
*/
|
|
24
|
-
debug?: boolean;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
|
|
28
|
-
|
|
29
|
-
export { _default as default };
|
package/dist/types.d.ts
DELETED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|