nuxt-content-assets 1.1.1 → 1.2.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.
Files changed (42) hide show
  1. package/README.md +2 -0
  2. package/dist/module.d.ts +21 -2
  3. package/dist/module.json +1 -1
  4. package/dist/module.mjs +349 -168
  5. package/dist/runtime/assets/public.d.ts +41 -0
  6. package/dist/runtime/assets/public.mjs +111 -0
  7. package/dist/runtime/{services/sources.d.ts → assets/source.d.ts} +4 -4
  8. package/dist/runtime/{services/sources.mjs → assets/source.mjs} +4 -14
  9. package/dist/runtime/content/parsed.d.ts +5 -0
  10. package/dist/runtime/content/parsed.mjs +32 -0
  11. package/dist/runtime/content/plugin.mjs +65 -0
  12. package/dist/runtime/sockets/factory.d.ts +5 -1
  13. package/dist/runtime/sockets/factory.mjs +3 -10
  14. package/dist/runtime/sockets/plugin.d.ts +3 -0
  15. package/dist/runtime/sockets/plugin.mjs +31 -8
  16. package/dist/runtime/sockets/setup.d.ts +1 -1
  17. package/dist/runtime/sockets/setup.mjs +15 -2
  18. package/dist/runtime/utils/config.d.ts +11 -0
  19. package/dist/runtime/utils/config.mjs +12 -0
  20. package/dist/runtime/utils/content.d.ts +16 -0
  21. package/dist/runtime/utils/content.mjs +43 -0
  22. package/dist/runtime/utils/index.d.ts +2 -0
  23. package/dist/runtime/utils/index.mjs +2 -0
  24. package/dist/runtime/utils/object.d.ts +7 -3
  25. package/dist/runtime/utils/object.mjs +5 -2
  26. package/dist/runtime/utils/path.mjs +1 -1
  27. package/dist/types.d.ts +2 -6
  28. package/package.json +2 -4
  29. package/dist/runtime/config.d.ts +0 -2
  30. package/dist/runtime/config.mjs +0 -2
  31. package/dist/runtime/options.d.ts +0 -10
  32. package/dist/runtime/options.mjs +0 -18
  33. package/dist/runtime/plugin.mjs +0 -121
  34. package/dist/runtime/services/assets.d.ts +0 -25
  35. package/dist/runtime/services/assets.mjs +0 -23
  36. package/dist/runtime/services/index.d.ts +0 -3
  37. package/dist/runtime/services/index.mjs +0 -3
  38. package/dist/runtime/services/paths.d.ts +0 -8
  39. package/dist/runtime/services/paths.mjs +0 -26
  40. package/dist/runtime/sockets/composable.d.ts +0 -2
  41. package/dist/runtime/sockets/composable.mjs +0 -12
  42. /package/dist/runtime/{plugin.d.ts → content/plugin.d.ts} +0 -0
@@ -1,18 +0,0 @@
1
- import { matchTokens } from "./utils/string.mjs";
2
- export const defaults = {
3
- // inject image size into the rendered html
4
- imageSize: "style",
5
- // treat these extensions as content
6
- contentExtensions: "md csv ya?ml json",
7
- // output debug messages
8
- debug: false
9
- };
10
- export const extensions = {
11
- // used to get image size
12
- image: matchTokens("png jpg jpeg gif svg webp ico"),
13
- // unused for now
14
- media: matchTokens("mp3 m4a wav mp4 mov webm ogg avi flv avchd")
15
- };
16
- export function getIgnores(extensions2) {
17
- return `^((?!(${matchTokens(extensions2).join("|")})).)*$`;
18
- }
@@ -1,121 +0,0 @@
1
- import Path from "path";
2
- import { visit, SKIP, CONTINUE } from "unist-util-visit";
3
- import {
4
- buildStyle,
5
- deKey,
6
- isValidAsset,
7
- list,
8
- matchTokens,
9
- toPath,
10
- walk,
11
- removeQuery,
12
- buildQuery,
13
- parseQuery
14
- } from "./utils/index.mjs";
15
- import { cachePath, imageFlags, debug } from "#nuxt-content-assets";
16
- import { makeStorage } from "./services/index.mjs";
17
- async function updateAssets() {
18
- assets = await storage.getItem("assets.json");
19
- }
20
- const storage = makeStorage(cachePath);
21
- storage.watch(async (event, key) => {
22
- if (event === "update" && key === "assets.json") {
23
- await updateAssets();
24
- }
25
- });
26
- let assets = {};
27
- void updateAssets();
28
- const tags = {
29
- // unlikely to contain assets
30
- exclude: matchTokens({
31
- container: "pre code code-inline",
32
- 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",
33
- headers: "h1 h2 h3 h4 h5 h6",
34
- controls: "input textarea button select optgroup option label legend datalist output",
35
- media: "map area canvas svg",
36
- other: "style script noscript template",
37
- empty: "hr br"
38
- }),
39
- // may contain assets
40
- include: matchTokens({
41
- content: "main header footer section article aside details dialog summary data object nav blockquote div span p",
42
- table: "table caption th tr td thead tbody tfoot col colgroup",
43
- media: "figcaption figure picture",
44
- form: "form fieldset",
45
- list: "ul ol li dir dl dt dd",
46
- formatting: "strong b em i"
47
- }),
48
- // assets
49
- assets: "a img audio source track video embed"
50
- };
51
- const plugin = async (nitro) => {
52
- nitro.hooks.hook("content:file:afterParse", async (file) => {
53
- const { _id } = file;
54
- if (_id.endsWith(".md")) {
55
- const srcDoc = toPath(deKey(_id));
56
- const srcDir = Path.dirname(srcDoc);
57
- const updated = [];
58
- const getAsset = (relAsset) => {
59
- const srcAsset = Path.join(srcDir, relAsset);
60
- return assets[srcAsset] || {};
61
- };
62
- const filter = (value, key) => !(String(key).startsWith("_") || key === "body");
63
- walk(file, (value, parent, key) => {
64
- if (isValidAsset(value)) {
65
- const { srcAttr, width, height } = getAsset(removeQuery(value));
66
- if (srcAttr) {
67
- const query = width && height && (imageFlags.includes("src") || imageFlags.includes("url")) ? `width=${width}&height=${height}` : "";
68
- const srcUrl = query ? buildQuery(srcAttr, parseQuery(value), query) : srcAttr;
69
- parent[key] = srcUrl;
70
- updated.push(`meta: ${key} to "${srcUrl}"`);
71
- }
72
- }
73
- }, filter);
74
- visit(file.body, (node) => node.type === "element", (node) => {
75
- const { tag, props } = node;
76
- const excluded = tags.exclude.includes(tag);
77
- if (excluded) {
78
- return SKIP;
79
- }
80
- const included = tags.include.includes(tag);
81
- if (included || !props) {
82
- return CONTINUE;
83
- }
84
- for (const [prop, value] of Object.entries(props)) {
85
- if (typeof value !== "string") {
86
- return;
87
- }
88
- const { srcAttr, width, height } = getAsset(value);
89
- if (srcAttr) {
90
- node.props[prop] = srcAttr;
91
- if (node.tag === "img") {
92
- if (width && height) {
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
- }
105
- }
106
- }
107
- } else if (node.tag === "a") {
108
- node.props.target ||= "_blank";
109
- }
110
- updated.push(`page: ${tag}[${prop}] to "${srcAttr}"`);
111
- }
112
- }
113
- });
114
- if (debug && updated.length) {
115
- list(`Processed "/${srcDoc}"`, updated);
116
- console.log();
117
- }
118
- }
119
- });
120
- };
121
- export default plugin;
@@ -1,25 +0,0 @@
1
- export type AssetConfig = {
2
- srcAttr: string;
3
- width?: number;
4
- height?: number;
5
- };
6
- /**
7
- * Parse asset paths from absolute path
8
- *
9
- * @param srcDir The absolute path to the asset's source folder
10
- * @param srcAbs The absolute path to the asset itself
11
- */
12
- export declare function getAssetPaths(srcDir: string, srcAbs: string): {
13
- id: any;
14
- srcRel: string;
15
- srcAttr: string;
16
- };
17
- /**
18
- * Get asset image sizes
19
- *
20
- * @param srcAbs The absolute path to the asset itself
21
- */
22
- export declare function getAssetSizes(srcAbs: string): {
23
- width?: number;
24
- height?: number;
25
- };
@@ -1,23 +0,0 @@
1
- import * as Path from "path";
2
- import getImageSize from "image-size";
3
- import { isImage, warn } from "../utils/index.mjs";
4
- export function getAssetPaths(srcDir, srcAbs) {
5
- const srcRel = Path.relative(srcDir, srcAbs);
6
- const srcAttr = "/" + srcRel;
7
- const id = srcRel.replaceAll("/", ":");
8
- return {
9
- id,
10
- srcRel,
11
- srcAttr
12
- };
13
- }
14
- export function getAssetSizes(srcAbs) {
15
- if (isImage(srcAbs)) {
16
- try {
17
- return getImageSize(srcAbs);
18
- } catch (err) {
19
- warn(`could not read image "${srcAbs}`);
20
- }
21
- }
22
- return {};
23
- }
@@ -1,3 +0,0 @@
1
- export * from './assets';
2
- export * from './sources';
3
- export * from './paths';
@@ -1,3 +0,0 @@
1
- export * from "./assets.mjs";
2
- export * from "./sources.mjs";
3
- export * from "./paths.mjs";
@@ -1,8 +0,0 @@
1
- /**
2
- * Interpolate assets path pattern
3
- *
4
- * @param pattern A path pattern with tokens
5
- * @param src The relative path to a src asset
6
- * @param warn An optional flag to warn for unknown tokens
7
- */
8
- export declare function interpolatePattern(pattern: string, src: string, warn?: boolean): string;
@@ -1,26 +0,0 @@
1
- import Path from "path";
2
- import { hash } from "ohash";
3
- import { log } from "../utils/index.mjs";
4
- const replacers = {
5
- key: (src) => Path.dirname(src).split("/").filter((e) => e).shift() || "",
6
- path: (src) => Path.dirname(src),
7
- folder: (src) => Path.dirname(src).replace(/[^/]+\//, ""),
8
- file: (src) => Path.basename(src),
9
- name: (src) => Path.basename(src, Path.extname(src)),
10
- extname: (src) => Path.extname(src),
11
- ext: (src) => Path.extname(src).substring(1),
12
- hash: (src) => hash({ src })
13
- };
14
- export function interpolatePattern(pattern, src, warn = false) {
15
- return Path.join(pattern.replace(/\[\w+]/g, (match) => {
16
- const name = match.substring(1, match.length - 1);
17
- const fn = replacers[name];
18
- if (fn) {
19
- return fn(src);
20
- }
21
- if (warn) {
22
- log(`Unknown output token ${match}`, true);
23
- }
24
- return match;
25
- }));
26
- }
@@ -1,2 +0,0 @@
1
- import { Callback, SocketInstance } from '../../types';
2
- export declare function useSockets(channel: string, callback?: Callback): Promise<SocketInstance | null>;
@@ -1,12 +0,0 @@
1
- import { useRuntimeConfig } from "#imports";
2
- export function useSockets(channel, callback) {
3
- const url = useRuntimeConfig().public.sockets?.wsUrl;
4
- return new Promise(function(resolve) {
5
- if (process.client && url) {
6
- return import("./setup").then(({ setupSocketClient }) => {
7
- return resolve(setupSocketClient(channel, callback));
8
- });
9
- }
10
- resolve(null);
11
- });
12
- }