ferst-core 0.14.0 → 0.16.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/README.md +13 -4
- package/bin/ferst-media.mjs +96 -0
- package/cms/config.template.yml +11 -0
- package/components/Card.astro +53 -51
- package/components/PostCard.astro +138 -136
- package/components/SiteHeader.astro +1356 -1273
- package/components/blocks/Grid.astro +49 -48
- package/components/blocks/Heading.astro +4 -1
- package/components/recipes/Banner.astro +146 -146
- package/components/recipes/Book.astro +39 -4
- package/components/recipes/FeatureCards.astro +112 -118
- package/components/recipes/Gallery.astro +57 -59
- package/components/recipes/Hero.astro +638 -533
- package/components/recipes/LatestPosts.astro +295 -292
- package/components/recipes/Locations.astro +146 -143
- package/components/recipes/Marquee.astro +75 -75
- package/components/recipes/MediaCards.astro +213 -219
- package/components/recipes/Pricing.astro +149 -150
- package/components/recipes/Stats.astro +150 -157
- package/components/recipes/Tiles.astro +342 -296
- package/content/blocks.ts +1072 -1024
- package/content/imageSlots.mjs +542 -0
- package/content/schemas.ts +35 -2
- package/examples/recipes.ts +4 -0
- package/fonts/OFL.txt +1332 -287
- package/fonts/anton-latin-400-normal.woff2 +0 -0
- package/fonts/bangers-latin-400-normal.woff2 +0 -0
- package/fonts/bebas-neue-latin-400-normal.woff2 +0 -0
- package/fonts/bowlby-one-latin-400-normal.woff2 +0 -0
- package/fonts/fredoka-latin-400-normal.woff2 +0 -0
- package/fonts/fredoka-latin-600-normal.woff2 +0 -0
- package/fonts/fredoka-latin-700-normal.woff2 +0 -0
- package/fonts/knewave-latin-400-normal.woff2 +0 -0
- package/fonts/lora-latin-400-normal.woff2 +0 -0
- package/fonts/lora-latin-600-normal.woff2 +0 -0
- package/fonts/lora-latin-700-normal.woff2 +0 -0
- package/fonts/merriweather-latin-400-normal.woff2 +0 -0
- package/fonts/merriweather-latin-600-normal.woff2 +0 -0
- package/fonts/merriweather-latin-700-normal.woff2 +0 -0
- package/fonts/nunito-latin-400-normal.woff2 +0 -0
- package/fonts/nunito-latin-600-normal.woff2 +0 -0
- package/fonts/nunito-latin-700-normal.woff2 +0 -0
- package/fonts/oswald-latin-400-normal.woff2 +0 -0
- package/fonts/oswald-latin-600-normal.woff2 +0 -0
- package/fonts/oswald-latin-700-normal.woff2 +0 -0
- package/fonts/rubik-dirt-latin-400-normal.woff2 +0 -0
- package/integration.mjs +253 -249
- package/layouts/Base.astro +13 -3
- package/lib/book.ts +94 -1
- package/lib/fonts.ts +130 -0
- package/lib/gridRows.ts +197 -0
- package/lib/image-fields.ts +140 -0
- package/lib/image-size.mjs +108 -0
- package/lib/image-sizes.ts +16 -0
- package/lib/logo-header.ts +46 -0
- package/lib/media-check.mjs +524 -0
- package/lib/theme-preview.ts +203 -140
- package/lib/themeTokens.ts +235 -220
- package/package.json +3 -2
- package/styles/fonts.css +239 -76
- package/styles/theme.css +228 -160
- package/lib/gridColumns.ts +0 -22
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An image's own width and height, read from the first bytes of the file: PNG, JPEG, GIF,
|
|
3
|
+
* WebP and SVG. The ferst() integration reads every image in a site's `public/` at build
|
|
4
|
+
* time (publicImageSizes), so components reserve each picture's real shape before it
|
|
5
|
+
* arrives (lib/image-sizes.ts). Pure and synchronous, with no image library.
|
|
6
|
+
*/
|
|
7
|
+
import { closeSync, openSync, readdirSync, readFileSync, readSync } from 'node:fs';
|
|
8
|
+
|
|
9
|
+
/** @param {Uint8Array} bytes @returns {{ width: number, height: number } | null} */
|
|
10
|
+
export function imageSize(bytes) {
|
|
11
|
+
const b = Buffer.from(bytes);
|
|
12
|
+
if (b.length < 10) return null;
|
|
13
|
+
const ok = (width, height) => (width > 0 && height > 0 ? { width, height } : null);
|
|
14
|
+
|
|
15
|
+
// PNG: the IHDR chunk follows the 8-byte signature.
|
|
16
|
+
if (b.readUInt32BE(0) === 0x89504e47 && b.toString('ascii', 12, 16) === 'IHDR') {
|
|
17
|
+
return ok(b.readUInt32BE(16), b.readUInt32BE(20));
|
|
18
|
+
}
|
|
19
|
+
// GIF: logical screen size.
|
|
20
|
+
if (b.toString('ascii', 0, 4) === 'GIF8') return ok(b.readUInt16LE(6), b.readUInt16LE(8));
|
|
21
|
+
// WebP: lossy (VP8 ), lossless (VP8L) or extended (VP8X).
|
|
22
|
+
if (b.toString('ascii', 0, 4) === 'RIFF' && b.toString('ascii', 8, 12) === 'WEBP' && b.length >= 30) {
|
|
23
|
+
const kind = b.toString('ascii', 12, 16);
|
|
24
|
+
if (kind === 'VP8 ') return ok(b.readUInt16LE(26) & 0x3fff, b.readUInt16LE(28) & 0x3fff);
|
|
25
|
+
if (kind === 'VP8L') {
|
|
26
|
+
const bits = b.readUInt32LE(21);
|
|
27
|
+
return ok((bits & 0x3fff) + 1, ((bits >> 14) & 0x3fff) + 1);
|
|
28
|
+
}
|
|
29
|
+
if (kind === 'VP8X') return ok(b.readUIntLE(24, 3) + 1, b.readUIntLE(27, 3) + 1);
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
// JPEG: walk the markers to the first start-of-frame.
|
|
33
|
+
if (b[0] === 0xff && b[1] === 0xd8) {
|
|
34
|
+
let i = 2;
|
|
35
|
+
while (i + 9 < b.length) {
|
|
36
|
+
if (b[i] !== 0xff) return null;
|
|
37
|
+
const marker = b[i + 1];
|
|
38
|
+
if (marker === 0xff) { i += 1; continue; }
|
|
39
|
+
const length = b.readUInt16BE(i + 2);
|
|
40
|
+
const isFrame = marker >= 0xc0 && marker <= 0xcf && ![0xc4, 0xc8, 0xcc].includes(marker);
|
|
41
|
+
if (isFrame) return ok(b.readUInt16BE(i + 7), b.readUInt16BE(i + 5));
|
|
42
|
+
i += 2 + length;
|
|
43
|
+
}
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
// SVG: width and height in pixels on the root element, else its viewBox.
|
|
47
|
+
const text = b.toString('utf8', 0, Math.min(b.length, 4096));
|
|
48
|
+
const root = /<svg\b[^>]*>/i.exec(text)?.[0];
|
|
49
|
+
if (root) {
|
|
50
|
+
const attr = (name) => new RegExp(`\\s${name}\\s*=\\s*["']\\s*([\\d.]+)\\s*(px)?\\s*["']`, 'i').exec(root)?.[1];
|
|
51
|
+
const w = Number(attr('width'));
|
|
52
|
+
const h = Number(attr('height'));
|
|
53
|
+
if (w > 0 && h > 0) return { width: w, height: h };
|
|
54
|
+
const box = /\sviewBox\s*=\s*["']\s*[-\d.]+[\s,]+[-\d.]+[\s,]+([\d.]+)[\s,]+([\d.]+)\s*["']/i.exec(root);
|
|
55
|
+
if (box) return ok(Number(box[1]), Number(box[2]));
|
|
56
|
+
}
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
const IMAGE = /\.(png|jpe?g|gif|webp|svg)$/i;
|
|
62
|
+
/** Enough of a file to find its size in (a JPEG's frame can sit behind a large EXIF block). */
|
|
63
|
+
const HEAD_BYTES = 256 * 1024;
|
|
64
|
+
|
|
65
|
+
function head(file) {
|
|
66
|
+
const fd = openSync(file, 'r');
|
|
67
|
+
try {
|
|
68
|
+
const buf = Buffer.alloc(HEAD_BYTES);
|
|
69
|
+
return buf.subarray(0, readSync(fd, buf, 0, HEAD_BYTES, 0));
|
|
70
|
+
} finally {
|
|
71
|
+
closeSync(fd);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Every image in the site's `public/` folder, by its address, with its width and height:
|
|
77
|
+
* `{ "/uploads/badge.png": [256, 256] }`. Images it can't read are left out. Never throws.
|
|
78
|
+
* @param {URL | undefined} root the site's folder
|
|
79
|
+
* @returns {Record<string, [number, number]>}
|
|
80
|
+
*/
|
|
81
|
+
export function publicImageSizes(root) {
|
|
82
|
+
const sizes = {};
|
|
83
|
+
if (!root) return sizes;
|
|
84
|
+
const walk = (dir, prefix) => {
|
|
85
|
+
let entries;
|
|
86
|
+
try {
|
|
87
|
+
entries = readdirSync(dir, { withFileTypes: true });
|
|
88
|
+
} catch {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
for (const entry of entries) {
|
|
92
|
+
if (entry.name.startsWith('.')) continue;
|
|
93
|
+
if (entry.isDirectory()) {
|
|
94
|
+
walk(new URL(`${encodeURIComponent(entry.name)}/`, dir), `${prefix}${entry.name}/`);
|
|
95
|
+
} else if (IMAGE.test(entry.name)) {
|
|
96
|
+
try {
|
|
97
|
+
const file = new URL(encodeURIComponent(entry.name), dir);
|
|
98
|
+
const size = imageSize(head(file)) ?? imageSize(readFileSync(file));
|
|
99
|
+
if (size) sizes[`${prefix}${entry.name}`] = [size.width, size.height];
|
|
100
|
+
} catch {
|
|
101
|
+
/* unreadable: no reserved size for this one */
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
walk(new URL('public/', root), '/');
|
|
107
|
+
return sizes;
|
|
108
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The site's own images and their width and height, read at build time by the ferst()
|
|
3
|
+
* integration (lib/image-size.mjs) from everything in `public/`. A component uses it to
|
|
4
|
+
* give an `<img>` its real width and height, so the page keeps the picture's shape before
|
|
5
|
+
* it arrives: no jump, no wrong-shaped box. Empty outside a ferst() build (the showcase,
|
|
6
|
+
* the preview renderer), where an image simply has no reserved size.
|
|
7
|
+
*/
|
|
8
|
+
// Written exactly as `import.meta.env.FERST_IMAGE_SIZES`: the build replaces that text.
|
|
9
|
+
const SIZES = (import.meta.env.FERST_IMAGE_SIZES ?? {}) as Record<string, [number, number]>;
|
|
10
|
+
|
|
11
|
+
/** A site image's own size by its address (`/uploads/badge.png`), if the build read it. */
|
|
12
|
+
export function imageSizeOf(src: string | undefined): { width: number; height: number } | undefined {
|
|
13
|
+
if (!src) return undefined;
|
|
14
|
+
const found = SIZES[src.split(/[?#]/)[0]];
|
|
15
|
+
return found ? { width: found[0], height: found[1] } : undefined;
|
|
16
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The header logo's size and placement (logoSettings.header) as attributes on the site
|
|
3
|
+
* header. One function for both places that draw a header: SiteHeader at build time, and
|
|
4
|
+
* the live preview in the browser (lib/theme-preview.ts), which applies an editor's draft
|
|
5
|
+
* logo settings to a loaded page. The CSS that reads these attributes is SiteHeader's.
|
|
6
|
+
*
|
|
7
|
+
* Only a setting that differs from the default produces an attribute, so a site that sets
|
|
8
|
+
* nothing gets exactly the header it always had.
|
|
9
|
+
*/
|
|
10
|
+
import type { LogoHeader } from '../content/schemas';
|
|
11
|
+
|
|
12
|
+
/** Every attribute this can set, so a preview can clear the previous draft's. */
|
|
13
|
+
export const LOGO_HEADER_ATTRIBUTES = ['data-logo-size', 'data-logo-placement', 'data-logo-hang', 'data-logo-shadow'] as const;
|
|
14
|
+
|
|
15
|
+
export interface LogoHeaderLayout {
|
|
16
|
+
attributes: Partial<Record<(typeof LOGO_HEADER_ATTRIBUTES)[number], string>>;
|
|
17
|
+
/** A hanging logo's width over its height, when its size is known: the bar keeps that much room. */
|
|
18
|
+
ratio?: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function logoHeaderLayout({
|
|
22
|
+
header = {},
|
|
23
|
+
hasLogo,
|
|
24
|
+
size,
|
|
25
|
+
}: {
|
|
26
|
+
header?: Partial<LogoHeader>;
|
|
27
|
+
/** False when the site shows its name as text: the settings apply to an image only. */
|
|
28
|
+
hasLogo: boolean;
|
|
29
|
+
/** The image's own width and height, when known. */
|
|
30
|
+
size?: { width: number; height: number };
|
|
31
|
+
}): LogoHeaderLayout {
|
|
32
|
+
const attributes: LogoHeaderLayout['attributes'] = {};
|
|
33
|
+
if (!hasLogo) return { attributes };
|
|
34
|
+
if (header.size && header.size !== 'standard') attributes['data-logo-size'] = header.size;
|
|
35
|
+
if (header.placement === 'overhang') {
|
|
36
|
+
attributes['data-logo-placement'] = 'overhang';
|
|
37
|
+
if (header.shrinkOnScroll === false) attributes['data-logo-hang'] = '';
|
|
38
|
+
}
|
|
39
|
+
if (header.shadow) attributes['data-logo-shadow'] = '';
|
|
40
|
+
// Only a hanging logo needs its shape up front: the bar keeps that much room beside it.
|
|
41
|
+
const ratio =
|
|
42
|
+
attributes['data-logo-placement'] && size && size.width > 0 && size.height > 0
|
|
43
|
+
? +(size.width / size.height).toFixed(4)
|
|
44
|
+
: undefined;
|
|
45
|
+
return ratio ? { attributes, ratio } : { attributes };
|
|
46
|
+
}
|