@nodeart/emdash-plugin-picture 0.0.0-stage → 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 +59 -2
- package/dist/astro.d.mts +26 -0
- package/dist/catalog.d.mts +165 -0
- package/dist/catalog.mjs +173 -0
- package/dist/index.d.mts +5 -0
- package/dist/index.mjs +35 -0
- package/dist/main.d.mts +26 -0
- package/dist/main.mjs +69 -0
- package/dist/manifest.json +27 -0
- package/dist/plugin.d.mts +11 -0
- package/dist/plugin.mjs +1 -0
- package/dist/resolve-media.d.mts +158 -0
- package/dist/resolve-media.mjs +505 -0
- package/emdash-plugin.jsonc +31 -0
- package/package.json +113 -5
- package/src/astro/MediaPicture.astro +91 -0
- package/src/astro/index.ts +1 -0
- package/src/catalog.ts +251 -0
- package/src/constants.ts +7 -0
- package/src/main.ts +75 -0
- package/src/plugin.ts +28 -0
- package/src/resolve-media.ts +906 -0
- package/src/svelte/MediaPicture.svelte +90 -0
- package/src/svelte/index.ts +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,114 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@nodeart/emdash-plugin-picture",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Responsive <picture> image catalog, AVIF/PNG variant resolver, and Astro/Svelte components for EmDash CMS.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": {
|
|
8
|
+
"name": "NodeArt",
|
|
9
|
+
"url": "https://nodeart.io"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/NodeArt/emdash-plugin-picture.git"
|
|
14
|
+
},
|
|
15
|
+
"homepage": "https://github.com/NodeArt/emdash-plugin-picture#readme",
|
|
16
|
+
"keywords": [
|
|
17
|
+
"emdash",
|
|
18
|
+
"emdash-plugin",
|
|
19
|
+
"astro",
|
|
20
|
+
"svelte",
|
|
21
|
+
"picture",
|
|
22
|
+
"avif",
|
|
23
|
+
"responsive-images"
|
|
24
|
+
],
|
|
25
|
+
"main": "./dist/main.mjs",
|
|
26
|
+
"types": "./dist/main.d.mts",
|
|
27
|
+
"sideEffects": [
|
|
28
|
+
"**/*.astro",
|
|
29
|
+
"**/*.svelte",
|
|
30
|
+
"**/*.css"
|
|
31
|
+
],
|
|
32
|
+
"exports": {
|
|
33
|
+
".": {
|
|
34
|
+
"types": "./dist/main.d.mts",
|
|
35
|
+
"import": "./dist/main.mjs",
|
|
36
|
+
"default": "./dist/main.mjs"
|
|
37
|
+
},
|
|
38
|
+
"./sandbox": {
|
|
39
|
+
"types": "./dist/plugin.d.mts",
|
|
40
|
+
"import": "./dist/plugin.mjs",
|
|
41
|
+
"default": "./dist/plugin.mjs"
|
|
42
|
+
},
|
|
43
|
+
"./descriptor": {
|
|
44
|
+
"types": "./dist/index.d.mts",
|
|
45
|
+
"import": "./dist/index.mjs",
|
|
46
|
+
"default": "./dist/index.mjs"
|
|
47
|
+
},
|
|
48
|
+
"./catalog": {
|
|
49
|
+
"types": "./dist/catalog.d.mts",
|
|
50
|
+
"import": "./dist/catalog.mjs",
|
|
51
|
+
"default": "./dist/catalog.mjs"
|
|
52
|
+
},
|
|
53
|
+
"./resolve-media": {
|
|
54
|
+
"types": "./dist/resolve-media.d.mts",
|
|
55
|
+
"import": "./dist/resolve-media.mjs",
|
|
56
|
+
"default": "./dist/resolve-media.mjs"
|
|
57
|
+
},
|
|
58
|
+
"./astro": {
|
|
59
|
+
"types": "./dist/astro.d.mts",
|
|
60
|
+
"default": "./src/astro/index.ts"
|
|
61
|
+
},
|
|
62
|
+
"./astro/MediaPicture.astro": "./src/astro/MediaPicture.astro",
|
|
63
|
+
"./svelte": {
|
|
64
|
+
"types": "./src/svelte/index.ts",
|
|
65
|
+
"svelte": "./src/svelte/index.ts",
|
|
66
|
+
"default": "./src/svelte/index.ts"
|
|
67
|
+
},
|
|
68
|
+
"./svelte/MediaPicture.svelte": "./src/svelte/MediaPicture.svelte"
|
|
69
|
+
},
|
|
70
|
+
"files": [
|
|
71
|
+
"dist",
|
|
72
|
+
"!dist/*.tar.gz",
|
|
73
|
+
"src",
|
|
74
|
+
"emdash-plugin.jsonc",
|
|
75
|
+
"README.md",
|
|
76
|
+
"LICENSE"
|
|
77
|
+
],
|
|
78
|
+
"publishConfig": {
|
|
79
|
+
"access": "public"
|
|
80
|
+
},
|
|
81
|
+
"scripts": {
|
|
82
|
+
"validate": "emdash-plugin validate",
|
|
83
|
+
"build": "rm -rf dist && emdash-plugin build && tsdown && node ./scripts/write-ui-types.mjs",
|
|
84
|
+
"prepack": "rm -rf dist && emdash-plugin build && tsdown && node ./scripts/write-ui-types.mjs",
|
|
85
|
+
"dev": "emdash-plugin dev",
|
|
86
|
+
"bundle": "emdash-plugin bundle",
|
|
87
|
+
"typecheck": "tsc --noEmit",
|
|
88
|
+
"test": "bun test"
|
|
89
|
+
},
|
|
90
|
+
"peerDependencies": {
|
|
91
|
+
"astro": ">=6.0.0",
|
|
92
|
+
"emdash": ">=1.3.0",
|
|
93
|
+
"svelte": ">=5.0.0"
|
|
94
|
+
},
|
|
95
|
+
"peerDependenciesMeta": {
|
|
96
|
+
"astro": {
|
|
97
|
+
"optional": true
|
|
98
|
+
},
|
|
99
|
+
"svelte": {
|
|
100
|
+
"optional": true
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
"devDependencies": {
|
|
104
|
+
"@emdash-cms/plugin-cli": "0.13.7",
|
|
105
|
+
"astro": "^7.4.13",
|
|
106
|
+
"emdash": "^1.3.0",
|
|
107
|
+
"svelte": "^5.56.7",
|
|
108
|
+
"tsdown": "0.20.3",
|
|
109
|
+
"typescript": "^6.0.3"
|
|
110
|
+
},
|
|
111
|
+
"engines": {
|
|
112
|
+
"node": ">=22.16"
|
|
113
|
+
}
|
|
114
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
---
|
|
2
|
+
import type { HTMLAttributes } from 'astro/types';
|
|
3
|
+
import type { ImageCatalog } from '../../dist/catalog.mjs';
|
|
4
|
+
import {
|
|
5
|
+
resolvePictureRenderProps,
|
|
6
|
+
type MediaProps,
|
|
7
|
+
type MediaRef,
|
|
8
|
+
type PictureFallbackFormat
|
|
9
|
+
} from '../../dist/resolve-media.mjs';
|
|
10
|
+
|
|
11
|
+
interface Props {
|
|
12
|
+
media: MediaProps | MediaRef | string | null | undefined;
|
|
13
|
+
preset: string | null;
|
|
14
|
+
catalog?: ImageCatalog;
|
|
15
|
+
sizes?: string;
|
|
16
|
+
alt?: string;
|
|
17
|
+
width?: number;
|
|
18
|
+
height?: number;
|
|
19
|
+
fallbackFormat?: PictureFallbackFormat;
|
|
20
|
+
class?: string;
|
|
21
|
+
pictureClass?: string;
|
|
22
|
+
style?: HTMLAttributes<'img'>['style'];
|
|
23
|
+
loading?: 'lazy' | 'eager';
|
|
24
|
+
decoding?: 'async' | 'auto' | 'sync';
|
|
25
|
+
fetchpriority?: 'high' | 'low' | 'auto';
|
|
26
|
+
title?: string;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const {
|
|
30
|
+
media,
|
|
31
|
+
preset,
|
|
32
|
+
catalog,
|
|
33
|
+
sizes,
|
|
34
|
+
alt,
|
|
35
|
+
width,
|
|
36
|
+
height,
|
|
37
|
+
fallbackFormat = 'png',
|
|
38
|
+
class: className,
|
|
39
|
+
pictureClass,
|
|
40
|
+
style,
|
|
41
|
+
loading = 'lazy',
|
|
42
|
+
decoding = 'async',
|
|
43
|
+
fetchpriority,
|
|
44
|
+
title
|
|
45
|
+
} = Astro.props;
|
|
46
|
+
|
|
47
|
+
const rendered = resolvePictureRenderProps({
|
|
48
|
+
media,
|
|
49
|
+
preset,
|
|
50
|
+
catalog,
|
|
51
|
+
sizes,
|
|
52
|
+
alt,
|
|
53
|
+
width,
|
|
54
|
+
height,
|
|
55
|
+
fallbackFormat
|
|
56
|
+
});
|
|
57
|
+
---
|
|
58
|
+
|
|
59
|
+
{
|
|
60
|
+
rendered && (
|
|
61
|
+
<picture class:list={['media-picture', pictureClass]}>
|
|
62
|
+
{rendered.sources.map((source) => (
|
|
63
|
+
<source type={source.type} srcset={source.srcset} sizes={source.sizes} />
|
|
64
|
+
))}
|
|
65
|
+
<img
|
|
66
|
+
src={rendered.img.src}
|
|
67
|
+
srcset={rendered.img.srcset}
|
|
68
|
+
sizes={rendered.img.sizes}
|
|
69
|
+
alt={rendered.img.alt}
|
|
70
|
+
title={title}
|
|
71
|
+
width={rendered.img.width}
|
|
72
|
+
height={rendered.img.height}
|
|
73
|
+
loading={loading}
|
|
74
|
+
decoding={decoding}
|
|
75
|
+
fetchpriority={fetchpriority}
|
|
76
|
+
class={className}
|
|
77
|
+
style={style}
|
|
78
|
+
/>
|
|
79
|
+
</picture>
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
<style>
|
|
84
|
+
.media-picture {
|
|
85
|
+
display: contents;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
.media-picture > source {
|
|
89
|
+
display: none;
|
|
90
|
+
}
|
|
91
|
+
</style>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as MediaPicture } from './MediaPicture.astro';
|
package/src/catalog.ts
ADDED
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
export const IMAGE_OUTPUT_FORMATS = ['avif', 'png'] as const;
|
|
2
|
+
export const TRANSFORMABLE_IMAGE_MIME_TYPES = [
|
|
3
|
+
'image/jpeg',
|
|
4
|
+
'image/png',
|
|
5
|
+
'image/webp',
|
|
6
|
+
'image/avif'
|
|
7
|
+
] as const;
|
|
8
|
+
|
|
9
|
+
export const MAX_IMAGE_DIMENSION = 4000;
|
|
10
|
+
|
|
11
|
+
export type ImageOutputFormat = (typeof IMAGE_OUTPUT_FORMATS)[number];
|
|
12
|
+
export type TransformableImageMimeType = (typeof TRANSFORMABLE_IMAGE_MIME_TYPES)[number];
|
|
13
|
+
export type ImageFit = 'contain' | 'cover';
|
|
14
|
+
|
|
15
|
+
export interface ImageFormatPolicy {
|
|
16
|
+
readonly preferred: readonly ImageOutputFormat[];
|
|
17
|
+
readonly fallback: ImageOutputFormat;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const TRANSFORMABLE_IMAGE_EXT_RE = /\.(?:jpe?g|png|webp|avif)(?:$|[?#])/i;
|
|
21
|
+
const NON_TRANSFORMABLE_EXT_RE = /\.(?:svg|gif)(?:$|[?#])/i;
|
|
22
|
+
|
|
23
|
+
/** Check whether a media item is a raster image suitable for `/_image` AVIF/PNG transforms. */
|
|
24
|
+
export function isTransformableImage(input: {
|
|
25
|
+
mimeType?: string | null;
|
|
26
|
+
url?: string | null;
|
|
27
|
+
filename?: string | null;
|
|
28
|
+
}): boolean {
|
|
29
|
+
const rawMime =
|
|
30
|
+
typeof input.mimeType === 'string'
|
|
31
|
+
? input.mimeType.split(';', 1)[0].trim().toLowerCase()
|
|
32
|
+
: '';
|
|
33
|
+
if (rawMime) {
|
|
34
|
+
if (rawMime.includes('svg') || rawMime === 'image/gif') return false;
|
|
35
|
+
if (
|
|
36
|
+
rawMime === 'image/jpg' ||
|
|
37
|
+
(TRANSFORMABLE_IMAGE_MIME_TYPES as readonly string[]).includes(rawMime)
|
|
38
|
+
) {
|
|
39
|
+
return true;
|
|
40
|
+
}
|
|
41
|
+
if (rawMime.startsWith('image/')) return false;
|
|
42
|
+
// Generic MIME types such as application/octet-stream fall through to filename/url checks.
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const candidates = [input.filename, input.url].filter(
|
|
46
|
+
(value): value is string => typeof value === 'string' && value.trim().length > 0
|
|
47
|
+
);
|
|
48
|
+
if (candidates.some((value) => NON_TRANSFORMABLE_EXT_RE.test(value.trim()))) {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
return candidates.some((value) => TRANSFORMABLE_IMAGE_EXT_RE.test(value.trim()));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface ImageCandidate {
|
|
55
|
+
readonly width: number;
|
|
56
|
+
readonly height?: number;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface ImageRoleDefinition {
|
|
60
|
+
readonly fit: ImageFit;
|
|
61
|
+
readonly candidates: readonly ImageCandidate[];
|
|
62
|
+
readonly defaultSizes: string;
|
|
63
|
+
readonly avifQuality?: number;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export type ImageRoleSpec = ImageRoleDefinition;
|
|
67
|
+
|
|
68
|
+
export interface ImageCatalog<
|
|
69
|
+
TRoles extends Record<string, ImageRoleDefinition> = Record<string, ImageRoleDefinition>
|
|
70
|
+
> {
|
|
71
|
+
readonly version: string;
|
|
72
|
+
readonly roles: { readonly [TRole in keyof TRoles]: TRoles[TRole] };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export type DefinedImageCatalog<
|
|
76
|
+
TRoles extends Record<string, ImageRoleDefinition> = Record<string, ImageRoleDefinition>
|
|
77
|
+
> = ImageCatalog<TRoles>;
|
|
78
|
+
|
|
79
|
+
export interface ImageCatalogInput<
|
|
80
|
+
TRoles extends Record<string, ImageRoleDefinition> = Record<string, ImageRoleDefinition>
|
|
81
|
+
> {
|
|
82
|
+
version: string;
|
|
83
|
+
roles: TRoles;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export type ImageRoleName<TCatalog extends ImageCatalog> = keyof TCatalog['roles'] & string;
|
|
87
|
+
|
|
88
|
+
function validatePositiveBoundedInt(value: number, max: number, label: string): void {
|
|
89
|
+
if (!Number.isInteger(value) || value < 1 || value > max) {
|
|
90
|
+
throw new Error(`${label} must be an integer between 1 and ${max}`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** Freeze and validate an image preset catalog for EmDash Picture. */
|
|
95
|
+
export function defineImageCatalog<const TRoles extends Record<string, ImageRoleDefinition>>(
|
|
96
|
+
input: ImageCatalogInput<TRoles>
|
|
97
|
+
): ImageCatalog<TRoles> {
|
|
98
|
+
const frozenRoles: Record<string, ImageRoleDefinition> = {};
|
|
99
|
+
|
|
100
|
+
for (const [role, definition] of Object.entries(input.roles)) {
|
|
101
|
+
if (definition.candidates.length === 0) {
|
|
102
|
+
throw new Error(`Image role must declare at least one candidate: ${role}`);
|
|
103
|
+
}
|
|
104
|
+
if (definition.avifQuality !== undefined) {
|
|
105
|
+
validatePositiveBoundedInt(definition.avifQuality, 100, `Image role avifQuality (${role})`);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const widths = new Set<number>();
|
|
109
|
+
const frozenCandidates: ImageCandidate[] = [];
|
|
110
|
+
for (const candidate of definition.candidates) {
|
|
111
|
+
validatePositiveBoundedInt(candidate.width, MAX_IMAGE_DIMENSION, `Image candidate width (${role})`);
|
|
112
|
+
if (candidate.height !== undefined) {
|
|
113
|
+
validatePositiveBoundedInt(
|
|
114
|
+
candidate.height,
|
|
115
|
+
MAX_IMAGE_DIMENSION,
|
|
116
|
+
`Image candidate height (${role})`
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
if (widths.has(candidate.width)) {
|
|
120
|
+
throw new Error(
|
|
121
|
+
`Image candidate widths must be unique within a role: ${role}/${candidate.width}`
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
widths.add(candidate.width);
|
|
125
|
+
frozenCandidates.push(Object.freeze({ ...candidate }));
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
frozenRoles[role] = Object.freeze({
|
|
129
|
+
...definition,
|
|
130
|
+
candidates: Object.freeze(frozenCandidates)
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return Object.freeze({
|
|
135
|
+
version: input.version,
|
|
136
|
+
roles: Object.freeze(frozenRoles) as { readonly [TRole in keyof TRoles]: TRoles[TRole] }
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Predict output pixel dimensions for a candidate without ever upscaling known source bounds.
|
|
142
|
+
* When source dimensions are omitted (e.g. legacy seed snapshots), emit the candidate width
|
|
143
|
+
* directly so responsive `<picture>` srcsets still work.
|
|
144
|
+
*/
|
|
145
|
+
export function computeVariantDimensions(
|
|
146
|
+
sourceWidth: number | null | undefined,
|
|
147
|
+
sourceHeight: number | null | undefined,
|
|
148
|
+
definition: { width: number; height?: number; fit: ImageFit }
|
|
149
|
+
): { width: number; height?: number } {
|
|
150
|
+
if (!sourceWidth && !sourceHeight) {
|
|
151
|
+
return {
|
|
152
|
+
width: definition.width,
|
|
153
|
+
...(definition.height === undefined ? {} : { height: definition.height })
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
if (sourceWidth && !sourceHeight) {
|
|
158
|
+
return {
|
|
159
|
+
width: Math.max(1, Math.min(sourceWidth, definition.width))
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (!sourceWidth && sourceHeight) {
|
|
164
|
+
return {
|
|
165
|
+
width: definition.width,
|
|
166
|
+
...(definition.height === undefined
|
|
167
|
+
? {}
|
|
168
|
+
: { height: Math.max(1, Math.min(sourceHeight, definition.height)) })
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// Both bounds are guaranteed positive here.
|
|
173
|
+
const knownWidth = sourceWidth as number;
|
|
174
|
+
const knownHeight = sourceHeight as number;
|
|
175
|
+
const widthRatio = definition.width / knownWidth;
|
|
176
|
+
const heightRatio =
|
|
177
|
+
definition.height === undefined ? widthRatio : definition.height / knownHeight;
|
|
178
|
+
const containScale = Math.min(1, widthRatio, heightRatio);
|
|
179
|
+
const coverScale = Math.max(widthRatio, heightRatio);
|
|
180
|
+
const scale = definition.fit === 'cover' ? Math.min(coverScale, 1) : containScale;
|
|
181
|
+
const scaledWidth = Math.max(1, Math.round(knownWidth * scale));
|
|
182
|
+
const scaledHeight = Math.max(1, Math.round(knownHeight * scale));
|
|
183
|
+
|
|
184
|
+
return definition.fit === 'cover' && definition.height !== undefined
|
|
185
|
+
? {
|
|
186
|
+
width: Math.min(scaledWidth, definition.width),
|
|
187
|
+
height: Math.min(scaledHeight, definition.height)
|
|
188
|
+
}
|
|
189
|
+
: { width: scaledWidth, height: scaledHeight };
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** Default general-purpose image presets for standard EmDash sites. */
|
|
193
|
+
export const DEFAULT_IMAGE_CATALOG = defineImageCatalog({
|
|
194
|
+
version: '1',
|
|
195
|
+
roles: {
|
|
196
|
+
hero: {
|
|
197
|
+
fit: 'contain',
|
|
198
|
+
defaultSizes: '(min-width: 1280px) 1200px, 100vw',
|
|
199
|
+
candidates: [{ width: 640 }, { width: 960 }, { width: 1200 }, { width: 1600 }]
|
|
200
|
+
},
|
|
201
|
+
wide: {
|
|
202
|
+
fit: 'contain',
|
|
203
|
+
defaultSizes: '(min-width: 870px) 840px, 100vw',
|
|
204
|
+
candidates: [
|
|
205
|
+
{ width: 480 },
|
|
206
|
+
{ width: 768 },
|
|
207
|
+
{ width: 840 },
|
|
208
|
+
{ width: 1200 },
|
|
209
|
+
{ width: 1680 }
|
|
210
|
+
]
|
|
211
|
+
},
|
|
212
|
+
narrow: {
|
|
213
|
+
fit: 'contain',
|
|
214
|
+
defaultSizes: '(min-width: 630px) 600px, calc(100vw - 30px)',
|
|
215
|
+
candidates: [
|
|
216
|
+
{ width: 320 },
|
|
217
|
+
{ width: 480 },
|
|
218
|
+
{ width: 600 },
|
|
219
|
+
{ width: 960 },
|
|
220
|
+
{ width: 1200 }
|
|
221
|
+
]
|
|
222
|
+
},
|
|
223
|
+
card: {
|
|
224
|
+
fit: 'contain',
|
|
225
|
+
defaultSizes: '(min-width: 1200px) 380px, (min-width: 768px) 50vw, 100vw',
|
|
226
|
+
candidates: [{ width: 320 }, { width: 480 }, { width: 640 }, { width: 768 }]
|
|
227
|
+
},
|
|
228
|
+
thumbnail: {
|
|
229
|
+
fit: 'contain',
|
|
230
|
+
defaultSizes: '(min-width: 768px) 200px, 120px',
|
|
231
|
+
candidates: [{ width: 120 }, { width: 200 }, { width: 320 }, { width: 400 }]
|
|
232
|
+
},
|
|
233
|
+
avatar: {
|
|
234
|
+
fit: 'contain',
|
|
235
|
+
defaultSizes: '96px',
|
|
236
|
+
candidates: [{ width: 48 }, { width: 96 }, { width: 192 }]
|
|
237
|
+
},
|
|
238
|
+
logo: {
|
|
239
|
+
fit: 'contain',
|
|
240
|
+
defaultSizes: '(min-width: 768px) 192px, 128px',
|
|
241
|
+
candidates: [{ width: 128 }, { width: 192 }, { width: 256 }, { width: 384 }]
|
|
242
|
+
},
|
|
243
|
+
icon: {
|
|
244
|
+
fit: 'contain',
|
|
245
|
+
defaultSizes: '32px',
|
|
246
|
+
candidates: [{ width: 18 }, { width: 32 }, { width: 36 }, { width: 64 }]
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
export type DefaultImageRoleName = keyof typeof DEFAULT_IMAGE_CATALOG.roles;
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export const PLUGIN_ID = 'emdash-picture';
|
|
2
|
+
export const PLUGIN_DISPLAY_NAME = 'EmDash Picture';
|
|
3
|
+
export const PLUGIN_DESCRIPTION =
|
|
4
|
+
'Responsive <picture> image catalog, AVIF/PNG variant resolver, and Astro/Svelte components for EmDash CMS.';
|
|
5
|
+
export const PLUGIN_VERSION = '0.1.0';
|
|
6
|
+
export const PACKAGE_NAME = '@nodeart/emdash-plugin-picture';
|
|
7
|
+
export const SANDBOX_ENTRYPOINT = `${PACKAGE_NAME}/sandbox`;
|
package/src/main.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { definePlugin, type PluginDescriptor, type ResolvedPlugin } from 'emdash';
|
|
2
|
+
import {
|
|
3
|
+
PLUGIN_DESCRIPTION,
|
|
4
|
+
PLUGIN_DISPLAY_NAME,
|
|
5
|
+
PLUGIN_ID,
|
|
6
|
+
PLUGIN_VERSION,
|
|
7
|
+
SANDBOX_ENTRYPOINT
|
|
8
|
+
} from './constants.js';
|
|
9
|
+
import { PictureRouteError, resolvePictureRouteQuery } from './resolve-media.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Standard EmDash plugin descriptor usable in both `emdash({ plugins: [...] })`
|
|
13
|
+
* and `emdash({ sandboxed: [...] })`.
|
|
14
|
+
*/
|
|
15
|
+
export function emdashPicture(): PluginDescriptor {
|
|
16
|
+
return {
|
|
17
|
+
id: PLUGIN_ID,
|
|
18
|
+
displayName: PLUGIN_DISPLAY_NAME,
|
|
19
|
+
description: PLUGIN_DESCRIPTION,
|
|
20
|
+
version: PLUGIN_VERSION,
|
|
21
|
+
format: 'standard',
|
|
22
|
+
entrypoint: SANDBOX_ENTRYPOINT,
|
|
23
|
+
capabilities: [],
|
|
24
|
+
allowedHosts: [],
|
|
25
|
+
storage: {},
|
|
26
|
+
hooks: [],
|
|
27
|
+
routes: [
|
|
28
|
+
{
|
|
29
|
+
name: 'resolve',
|
|
30
|
+
public: true,
|
|
31
|
+
methods: ['GET', 'HEAD'],
|
|
32
|
+
request: { body: 'none' },
|
|
33
|
+
cacheControl: 'public, max-age=3600'
|
|
34
|
+
}
|
|
35
|
+
]
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Instantiate an in-process `ResolvedPlugin` representation of EmDash Picture
|
|
41
|
+
* with the same capability contract and routes as the sandboxed build.
|
|
42
|
+
*/
|
|
43
|
+
export function createPlugin(): ResolvedPlugin {
|
|
44
|
+
return definePlugin({
|
|
45
|
+
id: PLUGIN_ID,
|
|
46
|
+
displayName: PLUGIN_DISPLAY_NAME,
|
|
47
|
+
description: PLUGIN_DESCRIPTION,
|
|
48
|
+
version: PLUGIN_VERSION,
|
|
49
|
+
capabilities: [],
|
|
50
|
+
allowedHosts: [],
|
|
51
|
+
storage: {},
|
|
52
|
+
routes: {
|
|
53
|
+
resolve: {
|
|
54
|
+
public: true,
|
|
55
|
+
methods: ['GET', 'HEAD'],
|
|
56
|
+
request: { body: 'none' },
|
|
57
|
+
cacheControl: 'public, max-age=3600',
|
|
58
|
+
handler: async (ctx) => {
|
|
59
|
+
const result = resolvePictureRouteQuery(ctx.input);
|
|
60
|
+
if (!result.ok) {
|
|
61
|
+
throw new PictureRouteError(result.error);
|
|
62
|
+
}
|
|
63
|
+
return result;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const defaultDescriptor: PluginDescriptor = Object.freeze(emdashPicture());
|
|
71
|
+
export default defaultDescriptor;
|
|
72
|
+
|
|
73
|
+
export * from './constants.js';
|
|
74
|
+
export * from './catalog.js';
|
|
75
|
+
export * from './resolve-media.js';
|
package/src/plugin.ts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { pluginRoute, type SandboxedPlugin } from 'emdash/plugin';
|
|
2
|
+
import { PictureRouteError, resolvePictureRouteQuery } from './resolve-media.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Sandboxed EmDash runtime entry. Compatible with both isolated sandbox
|
|
6
|
+
* execution (`emdash({ sandboxed: [...] })`) and in-process adaptation
|
|
7
|
+
* (`emdash({ plugins: [...] })`).
|
|
8
|
+
*/
|
|
9
|
+
const plugin: SandboxedPlugin = {
|
|
10
|
+
routes: {
|
|
11
|
+
resolve: pluginRoute({
|
|
12
|
+
public: true,
|
|
13
|
+
methods: ['GET', 'HEAD'],
|
|
14
|
+
cacheControl: 'public, max-age=3600',
|
|
15
|
+
request: { body: 'none' },
|
|
16
|
+
handler: async (routeCtx, ctx) => {
|
|
17
|
+
const result = resolvePictureRouteQuery(routeCtx.input);
|
|
18
|
+
if (!result.ok) {
|
|
19
|
+
ctx.log.warn('Invalid picture resolve request', { error: result.error });
|
|
20
|
+
throw new PictureRouteError(result.error);
|
|
21
|
+
}
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
})
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export default plugin;
|