@atmo-dev/kit 0.22.0 → 0.23.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/dist/articles/ArticleView.svelte +21 -7
- package/dist/articles/ArticleView.svelte.d.ts +8 -4
- package/dist/articles/styles.js +3 -2
- package/dist/articles-data.d.ts +1 -0
- package/dist/components/RichText.svelte +20 -11
- package/dist/components/RichText.svelte.d.ts +4 -1
- package/dist/index.d.ts +1 -1
- package/dist/media.d.ts +33 -0
- package/dist/media.js +74 -1
- package/package.json +1 -1
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
import ArticleCodeBlock from './ArticleCodeBlock.svelte';
|
|
10
10
|
import { formatArticleDate } from './dates.js';
|
|
11
11
|
import { articleCodeKey, articleMathKey } from './markdown.js';
|
|
12
|
+
import { sizedImage, type SizedImage } from '../media.js';
|
|
12
13
|
import type { ArticleSummary } from './records.js';
|
|
13
14
|
import { articleClass } from './styles.js';
|
|
14
15
|
|
|
@@ -35,11 +36,14 @@
|
|
|
35
36
|
content?: RichTextDocument;
|
|
36
37
|
/** Shown under the title: a description the author wrote. */
|
|
37
38
|
subtitle?: string;
|
|
38
|
-
/**
|
|
39
|
-
|
|
39
|
+
/**
|
|
40
|
+
* The cover image: its address, or a `SizedImage` with its size (space is kept while it
|
|
41
|
+
* loads), a `srcset` and its priority.
|
|
42
|
+
*/
|
|
43
|
+
cover?: string | SizedImage;
|
|
40
44
|
author?: { name: string; avatar?: string; href?: string };
|
|
41
|
-
/** The address to load an uploaded image (`cid:` destination) from
|
|
42
|
-
imageSrc?: (url: string) => string | undefined;
|
|
45
|
+
/** The address to load an uploaded image (`cid:` destination) from, or a `SizedImage`. */
|
|
46
|
+
imageSrc?: (url: string) => string | SizedImage | undefined;
|
|
43
47
|
/**
|
|
44
48
|
* Highlighted markup of code blocks by `articleCodeKey`, inserted as is: only pass a
|
|
45
49
|
* highlighter's output. Blocks without an entry are shown plain.
|
|
@@ -66,13 +70,23 @@
|
|
|
66
70
|
} = $props();
|
|
67
71
|
|
|
68
72
|
let authorHref = $derived(safeLinkHref(author?.href));
|
|
73
|
+
// A plain address is shown as before; a `SizedImage` is checked like the text's images.
|
|
74
|
+
let coverImage = $derived<SizedImage | undefined>(
|
|
75
|
+
typeof cover === 'string' ? (cover ? { src: cover } : undefined) : sizedImage(cover)
|
|
76
|
+
);
|
|
69
77
|
</script>
|
|
70
78
|
|
|
71
79
|
<article class={cn('w-full', className)}>
|
|
72
|
-
{#if
|
|
73
|
-
src={
|
|
80
|
+
{#if coverImage}<img
|
|
81
|
+
src={coverImage.src}
|
|
82
|
+
srcset={coverImage.srcset}
|
|
83
|
+
sizes={coverImage.sizes}
|
|
84
|
+
width={coverImage.width}
|
|
85
|
+
height={coverImage.height}
|
|
74
86
|
alt=""
|
|
75
|
-
|
|
87
|
+
loading={coverImage.loading}
|
|
88
|
+
fetchpriority={coverImage.fetchpriority}
|
|
89
|
+
class="mb-8 h-auto max-h-[90dvh] w-full rounded-xl object-cover"
|
|
76
90
|
/>{/if}
|
|
77
91
|
{#if header}{@render header()}{/if}
|
|
78
92
|
<h1
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Snippet } from 'svelte';
|
|
2
2
|
import type { LinkEmbed } from '../embeds/types.js';
|
|
3
3
|
import type { RichTextDocument } from '../richtext/document.js';
|
|
4
|
+
import { type SizedImage } from '../media.js';
|
|
4
5
|
import type { ArticleSummary } from './records.js';
|
|
5
6
|
type $$ComponentProps = {
|
|
6
7
|
article: Pick<ArticleSummary, 'title' | 'publishedAt'>;
|
|
@@ -8,15 +9,18 @@ type $$ComponentProps = {
|
|
|
8
9
|
content?: RichTextDocument;
|
|
9
10
|
/** Shown under the title: a description the author wrote. */
|
|
10
11
|
subtitle?: string;
|
|
11
|
-
/**
|
|
12
|
-
|
|
12
|
+
/**
|
|
13
|
+
* The cover image: its address, or a `SizedImage` with its size (space is kept while it
|
|
14
|
+
* loads), a `srcset` and its priority.
|
|
15
|
+
*/
|
|
16
|
+
cover?: string | SizedImage;
|
|
13
17
|
author?: {
|
|
14
18
|
name: string;
|
|
15
19
|
avatar?: string;
|
|
16
20
|
href?: string;
|
|
17
21
|
};
|
|
18
|
-
/** The address to load an uploaded image (`cid:` destination) from
|
|
19
|
-
imageSrc?: (url: string) => string | undefined;
|
|
22
|
+
/** The address to load an uploaded image (`cid:` destination) from, or a `SizedImage`. */
|
|
23
|
+
imageSrc?: (url: string) => string | SizedImage | undefined;
|
|
20
24
|
/**
|
|
21
25
|
* Highlighted markup of code blocks by `articleCodeKey`, inserted as is: only pass a
|
|
22
26
|
* highlighter's output. Blocks without an entry are shown plain.
|
package/dist/articles/styles.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Long-form typography, shared by the editor and the published article so that what is
|
|
3
3
|
* written is what will be shown. `!` because the components bring their own compact sizes.
|
|
4
|
-
* An image is never taller than the window, so that it can be seen whole.
|
|
4
|
+
* An image is never taller than the window, so that it can be seen whole. One with its size
|
|
5
|
+
* (`--atmo-image-ratio`) is narrowed to match, since a set width would otherwise stretch it.
|
|
5
6
|
* Nothing here reaches into an embed (`[data-link-embed]`), which brings its own headings
|
|
6
7
|
* and images. The classes are written out in full so that Tailwind finds them.
|
|
7
8
|
*/
|
|
8
|
-
const base = 'text-[1.0625rem] leading-[1.75]! space-y-5! [&_.ProseMirror]:space-y-5! [&_.ProseMirror]:leading-[1.75]! [&_:is(h1,h2,h3,h4,h5,h6):not([data-link-embed]_*)]:tracking-tight [&_img:not([data-link-embed]_*)]:mx-auto [&_img:not([data-link-embed]_*)]:max-h-[90dvh] [&_pre:not([data-link-embed]_*)]:text-sm! [&_pre:not([data-link-embed]_*)]:leading-relaxed [&_table:not([data-link-embed]_*)]:text-[0.9375rem]';
|
|
9
|
+
const base = 'text-[1.0625rem] leading-[1.75]! space-y-5! [&_.ProseMirror]:space-y-5! [&_.ProseMirror]:leading-[1.75]! [&_:is(h1,h2,h3,h4,h5,h6):not([data-link-embed]_*)]:tracking-tight [&_img:not([data-link-embed]_*)]:mx-auto [&_img:not([data-link-embed]_*)]:max-h-[90dvh] [&_img[style*=--atmo-image-ratio]:not([data-link-embed]_*)]:max-w-[min(100%,calc(90dvh*var(--atmo-image-ratio)))] [&_pre:not([data-link-embed]_*)]:text-sm! [&_pre:not([data-link-embed]_*)]:leading-relaxed [&_table:not([data-link-embed]_*)]:text-[0.9375rem]';
|
|
9
10
|
/** For `RichTextEditor markdown="article"`, where `#` is an h1. */
|
|
10
11
|
export const articleEditorClass = base +
|
|
11
12
|
' [&_h1:not([data-link-embed]_*)]:mt-10 [&_h1:not([data-link-embed]_*)]:text-3xl! [&_h2:not([data-link-embed]_*)]:mt-10 [&_h2:not([data-link-embed]_*)]:text-2xl! [&_h3:not([data-link-embed]_*)]:mt-8 [&_h3:not([data-link-embed]_*)]:text-xl! [&_h4:not([data-link-embed]_*)]:mt-6 [&_h4:not([data-link-embed]_*)]:text-lg!';
|
package/dist/articles-data.d.ts
CHANGED
|
@@ -5,4 +5,5 @@ export { readPastedArticle, type PastedArticle } from './articles/paste.js';
|
|
|
5
5
|
export { prepareArticleImage } from './articles/images.js';
|
|
6
6
|
export { formatArticleDate } from './articles/dates.js';
|
|
7
7
|
export { articleClass, articleEditorClass } from './articles/styles.js';
|
|
8
|
+
export type { SizedImage } from './media.js';
|
|
8
9
|
export { accentColors, baseColors, isAccentColor, isBaseColor, type AccentColor, type BaseColor, type Palette } from './palettes.js';
|
|
@@ -29,6 +29,7 @@
|
|
|
29
29
|
import LinkEmbed from './LinkEmbed.svelte';
|
|
30
30
|
import { cn } from '../utils.js';
|
|
31
31
|
import { safeHttpUrl } from '../urls.js';
|
|
32
|
+
import { sizedImage, type SizedImage } from '../media.js';
|
|
32
33
|
|
|
33
34
|
export type RichTextProps = RichTextLinkOptions & {
|
|
34
35
|
/** From `parseRichText`, usually on the server. Takes precedence over `text`. */
|
|
@@ -58,8 +59,10 @@
|
|
|
58
59
|
/**
|
|
59
60
|
* The address to load an article's image from, for destinations that are not HTTP(S),
|
|
60
61
|
* e.g. an app's own scheme for uploaded files. Other images are shown as their description.
|
|
62
|
+
* Return a `SizedImage` to give the image its size (no layout shift when it loads), a
|
|
63
|
+
* `srcset` and how it loads.
|
|
61
64
|
*/
|
|
62
|
-
imageSrc?: (url: string) => string | undefined;
|
|
65
|
+
imageSrc?: (url: string) => string | SizedImage | undefined;
|
|
63
66
|
/** Renders a code block in place of the plain `<pre>`, e.g. with highlighting. */
|
|
64
67
|
codeBlock?: Snippet<[value: string, lang: string | undefined]>;
|
|
65
68
|
/**
|
|
@@ -133,14 +136,12 @@
|
|
|
133
136
|
? `text-align: ${value}`
|
|
134
137
|
: undefined;
|
|
135
138
|
}
|
|
136
|
-
function
|
|
139
|
+
function image(node: Node): SizedImage | undefined {
|
|
137
140
|
const url = string(node.url);
|
|
138
|
-
const resolved = safeHttpUrl(url) ?? (url ? imageSrc?.(url) : undefined);
|
|
139
141
|
// An app's own address is a path on its origin or HTTP(S), never a script.
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
: safeHttpUrl(resolved);
|
|
142
|
+
return sizedImage(safeHttpUrl(url) ?? (url ? imageSrc?.(url) : undefined), {
|
|
143
|
+
loading: 'lazy'
|
|
144
|
+
});
|
|
144
145
|
}
|
|
145
146
|
|
|
146
147
|
function href(node: Node): string | undefined {
|
|
@@ -293,17 +294,25 @@
|
|
|
293
294
|
class="border-base-300 dark:border-[color-mix(in_srgb,var(--base-800)_80%,var(--base-950))]"
|
|
294
295
|
/>
|
|
295
296
|
{:else if node.type === 'image'}
|
|
296
|
-
{@const source =
|
|
297
|
+
{@const source = image(node)}
|
|
297
298
|
{@const caption = string(node.title).trim()}
|
|
298
299
|
{#if source}
|
|
299
300
|
<figure>
|
|
300
301
|
<img
|
|
301
|
-
src={source}
|
|
302
|
+
src={source.src}
|
|
303
|
+
srcset={source.srcset}
|
|
304
|
+
sizes={source.sizes}
|
|
305
|
+
width={source.width}
|
|
306
|
+
height={source.height}
|
|
307
|
+
style:--atmo-image-ratio={source.width && source.height
|
|
308
|
+
? source.width / source.height
|
|
309
|
+
: undefined}
|
|
302
310
|
alt={string(node.alt)}
|
|
303
|
-
loading=
|
|
311
|
+
loading={source.loading}
|
|
312
|
+
fetchpriority={source.fetchpriority}
|
|
304
313
|
decoding="async"
|
|
305
314
|
referrerpolicy="no-referrer"
|
|
306
|
-
class="mx-auto max-w-full rounded-md"
|
|
315
|
+
class="mx-auto h-auto max-w-full rounded-md"
|
|
307
316
|
/>
|
|
308
317
|
{#if caption}
|
|
309
318
|
<figcaption class="mt-2 text-center text-sm text-base-600 dark:text-base-400">
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Snippet } from 'svelte';
|
|
2
2
|
import { type RichTextDocument, type RichTextLinkOptions } from '../richtext/document.js';
|
|
3
3
|
import type { LinkEmbed as LinkEmbedDefinition } from '../embeds/types.js';
|
|
4
|
+
import { type SizedImage } from '../media.js';
|
|
4
5
|
export type RichTextProps = RichTextLinkOptions & {
|
|
5
6
|
/** From `parseRichText`, usually on the server. Takes precedence over `text`. */
|
|
6
7
|
content?: RichTextDocument | null;
|
|
@@ -29,8 +30,10 @@ export type RichTextProps = RichTextLinkOptions & {
|
|
|
29
30
|
/**
|
|
30
31
|
* The address to load an article's image from, for destinations that are not HTTP(S),
|
|
31
32
|
* e.g. an app's own scheme for uploaded files. Other images are shown as their description.
|
|
33
|
+
* Return a `SizedImage` to give the image its size (no layout shift when it loads), a
|
|
34
|
+
* `srcset` and how it loads.
|
|
32
35
|
*/
|
|
33
|
-
imageSrc?: (url: string) => string | undefined;
|
|
36
|
+
imageSrc?: (url: string) => string | SizedImage | undefined;
|
|
34
37
|
/** Renders a code block in place of the plain `<pre>`, e.g. with highlighting. */
|
|
35
38
|
codeBlock?: Snippet<[value: string, lang: string | undefined]>;
|
|
36
39
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -31,7 +31,7 @@ export { default as PhotoLightbox } from './components/PhotoLightbox.svelte';
|
|
|
31
31
|
export type { PhotoLightboxProps } from './components/PhotoLightbox.svelte';
|
|
32
32
|
export { default as PhotoGrid } from './components/PhotoGrid.svelte';
|
|
33
33
|
export type { PhotoGridProps } from './components/PhotoGrid.svelte';
|
|
34
|
-
export type { Photo } from './media.js';
|
|
34
|
+
export type { Photo, SizedImage } from './media.js';
|
|
35
35
|
export { default as GrainAlbumPicker } from './components/GrainAlbumPicker.svelte';
|
|
36
36
|
export type { GrainAlbumPickerProps } from './components/GrainAlbumPicker.svelte';
|
|
37
37
|
export { default as GrainAlbumView } from './components/GrainAlbumView.svelte';
|
package/dist/media.d.ts
CHANGED
|
@@ -8,3 +8,36 @@ export type Photo = {
|
|
|
8
8
|
height?: number;
|
|
9
9
|
contentWarning?: boolean;
|
|
10
10
|
};
|
|
11
|
+
/**
|
|
12
|
+
* An image with what the browser needs before it loads: its size, so that its space is kept
|
|
13
|
+
* (no layout shift), and smaller versions to choose from. Only `src` is required; each other
|
|
14
|
+
* field is left off the `<img>` when absent or invalid.
|
|
15
|
+
*/
|
|
16
|
+
export type SizedImage = {
|
|
17
|
+
src: string;
|
|
18
|
+
/** Candidates with width descriptors, e.g. `a.webp 1000w, b.webp 2000w`. */
|
|
19
|
+
srcset?: string;
|
|
20
|
+
/** Used with `srcset`; `100vw` when there is none. */
|
|
21
|
+
sizes?: string;
|
|
22
|
+
/** Intrinsic size in pixels; both or neither. */
|
|
23
|
+
width?: number;
|
|
24
|
+
height?: number;
|
|
25
|
+
/** Without it, an image loads lazily unless its `fetchpriority` is `high`. */
|
|
26
|
+
loading?: 'lazy' | 'eager';
|
|
27
|
+
fetchpriority?: 'high' | 'low' | 'auto';
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* An image address that is safe to load: HTTP(S), a path on the app's own origin, or a
|
|
31
|
+
* `blob:` address. Never a script, and never a path a browser would read as another host.
|
|
32
|
+
*/
|
|
33
|
+
export declare function safeImageSrc(value: unknown): string | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* The safe candidates of a `srcset`, each an address and a width descriptor (`1000w`). An
|
|
36
|
+
* address may itself contain commas, as image-resizing URLs often do.
|
|
37
|
+
*/
|
|
38
|
+
export declare function safeSrcset(value: unknown): string | undefined;
|
|
39
|
+
/**
|
|
40
|
+
* An image as an `<img>` can take it: a safe address, valid candidates, both dimensions or
|
|
41
|
+
* neither, and how it loads. A plain address is an image without the rest.
|
|
42
|
+
*/
|
|
43
|
+
export declare function sizedImage(value: string | SizedImage | null | undefined, defaults?: Pick<SizedImage, 'loading'>): SizedImage | undefined;
|
package/dist/media.js
CHANGED
|
@@ -1 +1,74 @@
|
|
|
1
|
-
|
|
1
|
+
import { safeHttpUrl } from './urls.js';
|
|
2
|
+
/**
|
|
3
|
+
* An image address that is safe to load: HTTP(S), a path on the app's own origin, or a
|
|
4
|
+
* `blob:` address. Never a script, and never a path a browser would read as another host.
|
|
5
|
+
*/
|
|
6
|
+
export function safeImageSrc(value) {
|
|
7
|
+
if (typeof value !== 'string' || !value)
|
|
8
|
+
return undefined;
|
|
9
|
+
const local = (value.startsWith('/') && !value.startsWith('//')) || value.startsWith('blob:');
|
|
10
|
+
if (!local)
|
|
11
|
+
return safeHttpUrl(value);
|
|
12
|
+
// `/\host` is `//host` to a browser.
|
|
13
|
+
for (const char of value) {
|
|
14
|
+
const code = char.charCodeAt(0);
|
|
15
|
+
if (char === '\\' || code <= 0x20 || code === 0x7f)
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
return value;
|
|
19
|
+
}
|
|
20
|
+
/** A positive whole number of pixels, rounded. */
|
|
21
|
+
const pixels = (value) => typeof value === 'number' && Number.isFinite(value) && Math.round(value) > 0
|
|
22
|
+
? Math.round(value)
|
|
23
|
+
: undefined;
|
|
24
|
+
/**
|
|
25
|
+
* The safe candidates of a `srcset`, each an address and a width descriptor (`1000w`). An
|
|
26
|
+
* address may itself contain commas, as image-resizing URLs often do.
|
|
27
|
+
*/
|
|
28
|
+
export function safeSrcset(value) {
|
|
29
|
+
if (typeof value !== 'string')
|
|
30
|
+
return undefined;
|
|
31
|
+
const candidates = [];
|
|
32
|
+
// A candidate is an address, whitespace, then descriptors up to a comma.
|
|
33
|
+
for (const match of value.matchAll(/\s*([^\s]+)((?:\s+[^\s,]+)*)\s*(?:,|$)/g)) {
|
|
34
|
+
if (!match[0].trim())
|
|
35
|
+
continue;
|
|
36
|
+
const address = safeImageSrc(match[1].replace(/,+$/, ''));
|
|
37
|
+
const descriptors = match[2].trim().split(/\s+/).filter(Boolean);
|
|
38
|
+
if (address && descriptors.length === 1 && /^[1-9]\d*w$/.test(descriptors[0]))
|
|
39
|
+
candidates.push(`${address} ${descriptors[0]}`);
|
|
40
|
+
}
|
|
41
|
+
return candidates.length ? candidates.join(', ') : undefined;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* An image as an `<img>` can take it: a safe address, valid candidates, both dimensions or
|
|
45
|
+
* neither, and how it loads. A plain address is an image without the rest.
|
|
46
|
+
*/
|
|
47
|
+
export function sizedImage(value, defaults = {}) {
|
|
48
|
+
const input = typeof value === 'string' ? { src: value } : value;
|
|
49
|
+
const src = safeImageSrc(input?.src);
|
|
50
|
+
if (!input || !src)
|
|
51
|
+
return undefined;
|
|
52
|
+
const width = pixels(input.width);
|
|
53
|
+
const height = pixels(input.height);
|
|
54
|
+
const srcset = safeSrcset(input.srcset);
|
|
55
|
+
const fetchpriority = input.fetchpriority === 'high' ||
|
|
56
|
+
input.fetchpriority === 'low' ||
|
|
57
|
+
input.fetchpriority === 'auto'
|
|
58
|
+
? input.fetchpriority
|
|
59
|
+
: undefined;
|
|
60
|
+
const loading = input.loading === 'lazy' || input.loading === 'eager'
|
|
61
|
+
? input.loading
|
|
62
|
+
: fetchpriority === 'high'
|
|
63
|
+
? undefined
|
|
64
|
+
: defaults.loading;
|
|
65
|
+
return {
|
|
66
|
+
src,
|
|
67
|
+
...(srcset
|
|
68
|
+
? { srcset, sizes: (typeof input.sizes === 'string' && input.sizes.trim()) || '100vw' }
|
|
69
|
+
: {}),
|
|
70
|
+
...(width && height ? { width, height } : {}),
|
|
71
|
+
...(loading ? { loading } : {}),
|
|
72
|
+
...(fetchpriority ? { fetchpriority } : {})
|
|
73
|
+
};
|
|
74
|
+
}
|