@hagicode/hagilight-core 0.14.1-dev.152.1.1b8dd12 → 0.15.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/og-image.d.ts +15 -0
- package/dist/og-image.js +26 -0
- package/dist/seo.d.ts +49 -0
- package/dist/seo.js +143 -0
- package/package.json +5 -1
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** Largest width, in pixels, of the Open Graph preview that a convertible image becomes. Never enlarged. */
|
|
2
|
+
export declare const OG_IMAGE_WIDTH = 1200;
|
|
3
|
+
/** Largest height, in pixels, of the Open Graph preview that a convertible image becomes. Never enlarged. */
|
|
4
|
+
export declare const OG_IMAGE_HEIGHT = 630;
|
|
5
|
+
/** Format of the Open Graph preview that a convertible image becomes. */
|
|
6
|
+
export declare const OG_IMAGE_FORMAT = "jpeg";
|
|
7
|
+
/** Encoder quality of the Open Graph preview that a convertible image becomes. */
|
|
8
|
+
export declare const OG_IMAGE_QUALITY = 80;
|
|
9
|
+
export type OgImageClassification = 'compatible' | 'convertible' | 'unrecognised';
|
|
10
|
+
/**
|
|
11
|
+
* Classify a declared preview image by the extension of its path. The query and fragment are
|
|
12
|
+
* ignored, the scheme and host of an absolute URL are ignored, and the extension is compared in
|
|
13
|
+
* lower case. The image bytes are never read, so the result does not depend on the site or host.
|
|
14
|
+
*/
|
|
15
|
+
export declare function classifyOgImage(reference: string): OgImageClassification;
|
package/dist/og-image.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/** Largest width, in pixels, of the Open Graph preview that a convertible image becomes. Never enlarged. */
|
|
2
|
+
export const OG_IMAGE_WIDTH = 1200;
|
|
3
|
+
/** Largest height, in pixels, of the Open Graph preview that a convertible image becomes. Never enlarged. */
|
|
4
|
+
export const OG_IMAGE_HEIGHT = 630;
|
|
5
|
+
/** Format of the Open Graph preview that a convertible image becomes. */
|
|
6
|
+
export const OG_IMAGE_FORMAT = 'jpeg';
|
|
7
|
+
/** Encoder quality of the Open Graph preview that a convertible image becomes. */
|
|
8
|
+
export const OG_IMAGE_QUALITY = 80;
|
|
9
|
+
const COMPATIBLE_EXTENSIONS = new Set(['jpg', 'jpeg', 'png', 'gif']);
|
|
10
|
+
const CONVERTIBLE_EXTENSIONS = new Set(['avif', 'webp']);
|
|
11
|
+
/**
|
|
12
|
+
* Classify a declared preview image by the extension of its path. The query and fragment are
|
|
13
|
+
* ignored, the scheme and host of an absolute URL are ignored, and the extension is compared in
|
|
14
|
+
* lower case. The image bytes are never read, so the result does not depend on the site or host.
|
|
15
|
+
*/
|
|
16
|
+
export function classifyOgImage(reference) {
|
|
17
|
+
const path = reference.replace(/[?#].*$/u, '').replace(/^[a-z][a-z\d+.-]*:\/\/[^/]*/iu, '');
|
|
18
|
+
const name = path.slice(path.lastIndexOf('/') + 1);
|
|
19
|
+
const dot = name.lastIndexOf('.');
|
|
20
|
+
const extension = dot < 0 ? '' : name.slice(dot + 1).toLowerCase();
|
|
21
|
+
if (COMPATIBLE_EXTENSIONS.has(extension))
|
|
22
|
+
return 'compatible';
|
|
23
|
+
if (CONVERTIBLE_EXTENSIONS.has(extension))
|
|
24
|
+
return 'convertible';
|
|
25
|
+
return 'unrecognised';
|
|
26
|
+
}
|
package/dist/seo.d.ts
CHANGED
|
@@ -53,6 +53,32 @@ export interface OrganizationStructuredDataInput {
|
|
|
53
53
|
logo?: string | URL;
|
|
54
54
|
alternateName?: string;
|
|
55
55
|
}
|
|
56
|
+
export interface WebSiteStructuredDataInput {
|
|
57
|
+
name: string;
|
|
58
|
+
url: string | URL;
|
|
59
|
+
basePath?: string;
|
|
60
|
+
}
|
|
61
|
+
export interface DefaultOrganizationOptions {
|
|
62
|
+
name?: string;
|
|
63
|
+
logo?: string | URL;
|
|
64
|
+
alternateName?: string;
|
|
65
|
+
}
|
|
66
|
+
export interface DefaultStructuredDataInput {
|
|
67
|
+
siteName?: string;
|
|
68
|
+
site?: string | URL;
|
|
69
|
+
basePath?: string;
|
|
70
|
+
enabled?: boolean;
|
|
71
|
+
organization?: DefaultOrganizationOptions;
|
|
72
|
+
structuredData?: StructuredData | readonly StructuredData[];
|
|
73
|
+
head?: readonly HeadEntry[];
|
|
74
|
+
}
|
|
75
|
+
export interface SocialCardOptions {
|
|
76
|
+
url?: string;
|
|
77
|
+
type?: string;
|
|
78
|
+
siteName?: string;
|
|
79
|
+
locale?: string;
|
|
80
|
+
image?: string;
|
|
81
|
+
}
|
|
56
82
|
export type StructuredData = Record<string, unknown>;
|
|
57
83
|
export declare function resolveAbsoluteHttpUrl(value: unknown, label?: string): URL;
|
|
58
84
|
export declare function resolveCanonicalUrl(pageUrl: string | URL, { site }?: {
|
|
@@ -71,8 +97,31 @@ export declare function composeSeoHead(head: readonly HeadEntry[], metadata: Seo
|
|
|
71
97
|
export declare function composeCanonicalSeoHead(head: readonly HeadEntry[], metadata: SeoMetadata & {
|
|
72
98
|
site?: string | URL;
|
|
73
99
|
}, { canonicalUrl, explicitHead }?: ComposeCanonicalSeoHeadOptions): HeadEntry[];
|
|
100
|
+
/**
|
|
101
|
+
* Convert a BCP 47 tag to the Open Graph `ll_CC` form. Only explicit mappings are used: a tag
|
|
102
|
+
* without an unambiguous region yields `undefined` instead of a guessed locale.
|
|
103
|
+
*/
|
|
104
|
+
export declare function toOpenGraphLocale(tag: string | undefined): string | undefined;
|
|
105
|
+
/**
|
|
106
|
+
* Append the Open Graph and Twitter card tags that `composeSeoHead` does not write. A key the
|
|
107
|
+
* consumer already supplies, under either attribute, is left to the consumer, and no entry the
|
|
108
|
+
* consumer passed is changed.
|
|
109
|
+
*/
|
|
110
|
+
export declare function composeSocialCardHead(head: readonly HeadEntry[], card: SocialCardOptions, explicitHead?: readonly (readonly HeadEntry[] | undefined)[]): HeadEntry[];
|
|
74
111
|
export declare function buildArticleStructuredData({ url, title, description, author, publishedDate, modifiedDate, }: ArticleStructuredDataInput): StructuredData;
|
|
75
112
|
export declare function buildBreadcrumbStructuredData(items?: readonly BreadcrumbItem[]): StructuredData;
|
|
76
113
|
export declare function buildOrganizationStructuredData({ name, url, logo, alternateName, }: OrganizationStructuredDataInput): StructuredData;
|
|
114
|
+
export declare function buildWebSiteStructuredData({ name, url, basePath }: WebSiteStructuredDataInput): StructuredData;
|
|
115
|
+
/**
|
|
116
|
+
* Collect the `@type` values a page already declares at the top level and inside `@graph`, from
|
|
117
|
+
* the consumer's structured data and from JSON-LD head scripts. An unparseable script declares
|
|
118
|
+
* nothing, so it can never suppress a default block.
|
|
119
|
+
*/
|
|
120
|
+
export declare function collectStructuredDataTypes(structuredData?: StructuredData | readonly StructuredData[], head?: readonly HeadEntry[]): Set<string>;
|
|
121
|
+
/**
|
|
122
|
+
* Build the `WebSite` and `Organization` blocks that the page does not already declare. Nothing is
|
|
123
|
+
* built when disabled, without an absolute site URL, or without a site title.
|
|
124
|
+
*/
|
|
125
|
+
export declare function buildDefaultStructuredData({ siteName, site, basePath, enabled, organization, structuredData, head, }?: DefaultStructuredDataInput): StructuredData[];
|
|
77
126
|
/** Serialize JSON-LD safely for inline `<script type="application/ld+json">`. */
|
|
78
127
|
export declare function serializeJsonLd(value: unknown): string;
|
package/dist/seo.js
CHANGED
|
@@ -194,6 +194,75 @@ export function composeCanonicalSeoHead(head, metadata, { canonicalUrl, explicit
|
|
|
194
194
|
output.splice(insertionIndex < 0 ? output.length : insertionIndex, 0, link);
|
|
195
195
|
return output;
|
|
196
196
|
}
|
|
197
|
+
const OPEN_GRAPH_REGION_BY_LANGUAGE = {
|
|
198
|
+
de: 'DE',
|
|
199
|
+
en: 'US',
|
|
200
|
+
es: 'ES',
|
|
201
|
+
fr: 'FR',
|
|
202
|
+
ja: 'JP',
|
|
203
|
+
ko: 'KR',
|
|
204
|
+
ru: 'RU',
|
|
205
|
+
zh: 'CN',
|
|
206
|
+
};
|
|
207
|
+
/**
|
|
208
|
+
* Convert a BCP 47 tag to the Open Graph `ll_CC` form. Only explicit mappings are used: a tag
|
|
209
|
+
* without an unambiguous region yields `undefined` instead of a guessed locale.
|
|
210
|
+
*/
|
|
211
|
+
export function toOpenGraphLocale(tag) {
|
|
212
|
+
if (typeof tag !== 'string')
|
|
213
|
+
return undefined;
|
|
214
|
+
const [language = '', second = '', third = ''] = tag.trim().split(/[-_]/u);
|
|
215
|
+
if (!/^[a-z]{2,3}$/iu.test(language))
|
|
216
|
+
return undefined;
|
|
217
|
+
const code = language.toLowerCase();
|
|
218
|
+
const script = second.length === 4 ? second.toLowerCase() : undefined;
|
|
219
|
+
const region = script === undefined ? second : third;
|
|
220
|
+
if (/^[a-z]{2}$/iu.test(region))
|
|
221
|
+
return `${code}_${region.toUpperCase()}`;
|
|
222
|
+
if (script !== undefined) {
|
|
223
|
+
if (code === 'zh' && script === 'hant')
|
|
224
|
+
return 'zh_TW';
|
|
225
|
+
if (code === 'zh' && script === 'hans')
|
|
226
|
+
return 'zh_CN';
|
|
227
|
+
return undefined;
|
|
228
|
+
}
|
|
229
|
+
if (second !== '')
|
|
230
|
+
return undefined;
|
|
231
|
+
const defaultRegion = OPEN_GRAPH_REGION_BY_LANGUAGE[code];
|
|
232
|
+
return defaultRegion === undefined ? undefined : `${code}_${defaultRegion}`;
|
|
233
|
+
}
|
|
234
|
+
function socialCardKey(entry) {
|
|
235
|
+
// Open Graph and Twitter names count under either attribute, so `name="og:type"` also wins.
|
|
236
|
+
return metaKey(entry)
|
|
237
|
+
?.replace(/^name:og:/u, 'property:og:')
|
|
238
|
+
.replace(/^property:twitter:/u, 'name:twitter:');
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Append the Open Graph and Twitter card tags that `composeSeoHead` does not write. A key the
|
|
242
|
+
* consumer already supplies, under either attribute, is left to the consumer, and no entry the
|
|
243
|
+
* consumer passed is changed.
|
|
244
|
+
*/
|
|
245
|
+
export function composeSocialCardHead(head, card, explicitHead = []) {
|
|
246
|
+
const present = new Set([...head, ...flattenHeads(explicitHead)]
|
|
247
|
+
.map(socialCardKey)
|
|
248
|
+
.filter((key) => Boolean(key)));
|
|
249
|
+
const siteName = card.siteName?.trim();
|
|
250
|
+
const values = [
|
|
251
|
+
['property:og:url', card.url || undefined],
|
|
252
|
+
['property:og:type', card.type?.trim() || 'website'],
|
|
253
|
+
['property:og:site_name', siteName || undefined],
|
|
254
|
+
['property:og:locale', toOpenGraphLocale(card.locale)],
|
|
255
|
+
['name:twitter:card', card.image ? 'summary_large_image' : 'summary'],
|
|
256
|
+
];
|
|
257
|
+
const result = [...head];
|
|
258
|
+
for (const [key, content] of values) {
|
|
259
|
+
if (content === undefined || present.has(key))
|
|
260
|
+
continue;
|
|
261
|
+
const separator = key.indexOf(':');
|
|
262
|
+
result.push({ tag: 'meta', attrs: { [key.slice(0, separator)]: key.slice(separator + 1), content } });
|
|
263
|
+
}
|
|
264
|
+
return result;
|
|
265
|
+
}
|
|
197
266
|
function dateValue(value, field) {
|
|
198
267
|
if (value === undefined || value === null)
|
|
199
268
|
return undefined;
|
|
@@ -264,6 +333,80 @@ export function buildOrganizationStructuredData({ name, url, logo, alternateName
|
|
|
264
333
|
}
|
|
265
334
|
return organization;
|
|
266
335
|
}
|
|
336
|
+
function siteRootHref(url, basePath = '/') {
|
|
337
|
+
return new URL(`${normalizeBasePath(basePath)}/`, resolveAbsoluteHttpUrl(url, 'site URL')).href;
|
|
338
|
+
}
|
|
339
|
+
export function buildWebSiteStructuredData({ name, url, basePath = '/' }) {
|
|
340
|
+
if (typeof name !== 'string' || !name.trim()) {
|
|
341
|
+
throw new TypeError('Hagilight SEO website name must be a non-empty string.');
|
|
342
|
+
}
|
|
343
|
+
const root = siteRootHref(url, basePath);
|
|
344
|
+
return {
|
|
345
|
+
'@context': 'https://schema.org',
|
|
346
|
+
'@type': 'WebSite',
|
|
347
|
+
'@id': `${root}#website`,
|
|
348
|
+
name: name.trim(),
|
|
349
|
+
url: root,
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
function addStructuredDataTypes(types, value) {
|
|
353
|
+
if (Array.isArray(value)) {
|
|
354
|
+
for (const item of value)
|
|
355
|
+
addStructuredDataTypes(types, item);
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
if (typeof value !== 'object' || value === null)
|
|
359
|
+
return;
|
|
360
|
+
const { '@type': type, '@graph': graph } = value;
|
|
361
|
+
for (const name of Array.isArray(type) ? type : [type]) {
|
|
362
|
+
if (typeof name === 'string')
|
|
363
|
+
types.add(name);
|
|
364
|
+
}
|
|
365
|
+
addStructuredDataTypes(types, graph);
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Collect the `@type` values a page already declares at the top level and inside `@graph`, from
|
|
369
|
+
* the consumer's structured data and from JSON-LD head scripts. An unparseable script declares
|
|
370
|
+
* nothing, so it can never suppress a default block.
|
|
371
|
+
*/
|
|
372
|
+
export function collectStructuredDataTypes(structuredData = [], head = []) {
|
|
373
|
+
const types = new Set();
|
|
374
|
+
addStructuredDataTypes(types, structuredData);
|
|
375
|
+
for (const entry of head) {
|
|
376
|
+
if (entry.tag !== 'script' || entry.attrs?.type !== 'application/ld+json' || typeof entry.content !== 'string') {
|
|
377
|
+
continue;
|
|
378
|
+
}
|
|
379
|
+
try {
|
|
380
|
+
addStructuredDataTypes(types, JSON.parse(entry.content));
|
|
381
|
+
}
|
|
382
|
+
catch {
|
|
383
|
+
// Unreadable JSON-LD is ignored; the default block for its types is still emitted.
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
return types;
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Build the `WebSite` and `Organization` blocks that the page does not already declare. Nothing is
|
|
390
|
+
* built when disabled, without an absolute site URL, or without a site title.
|
|
391
|
+
*/
|
|
392
|
+
export function buildDefaultStructuredData({ siteName, site, basePath, enabled = true, organization = {}, structuredData, head, } = {}) {
|
|
393
|
+
const name = siteName?.trim();
|
|
394
|
+
if (!enabled || !name || site === undefined)
|
|
395
|
+
return [];
|
|
396
|
+
const declared = collectStructuredDataTypes(structuredData, head);
|
|
397
|
+
const blocks = [];
|
|
398
|
+
if (!declared.has('WebSite'))
|
|
399
|
+
blocks.push(buildWebSiteStructuredData({ name, url: site, basePath }));
|
|
400
|
+
if (!declared.has('Organization')) {
|
|
401
|
+
blocks.push(buildOrganizationStructuredData({
|
|
402
|
+
name: organization.name?.trim() || name,
|
|
403
|
+
url: siteRootHref(site, basePath),
|
|
404
|
+
logo: organization.logo,
|
|
405
|
+
alternateName: organization.alternateName,
|
|
406
|
+
}));
|
|
407
|
+
}
|
|
408
|
+
return blocks;
|
|
409
|
+
}
|
|
267
410
|
/** Serialize JSON-LD safely for inline `<script type="application/ld+json">`. */
|
|
268
411
|
export function serializeJsonLd(value) {
|
|
269
412
|
return JSON.stringify(value).replace(/</gu, '\\u003c');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hagicode/hagilight-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.15.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Shared TypeScript utilities, components, and assets for HagiCode Astro and Starlight sites",
|
|
6
6
|
"repository": {
|
|
@@ -55,6 +55,10 @@
|
|
|
55
55
|
"types": "./dist/seo-schema.d.ts",
|
|
56
56
|
"default": "./dist/seo-schema.js"
|
|
57
57
|
},
|
|
58
|
+
"./og-image": {
|
|
59
|
+
"types": "./dist/og-image.d.ts",
|
|
60
|
+
"default": "./dist/og-image.js"
|
|
61
|
+
},
|
|
58
62
|
"./rss": {
|
|
59
63
|
"types": "./dist/rss.d.ts",
|
|
60
64
|
"default": "./dist/rss.js"
|