@withstudiocms/internal_helpers
Version:
Internal helper utilities for StudioCMS
72 lines (71 loc) • 4.17 kB
TypeScript
import { z } from 'astro/zod';
export declare const HeadConfigSchema: () => z.ZodDefault<z.ZodArray<z.ZodObject<{
/** Name of the HTML tag to add to `<head>`, e.g. `'meta'`, `'link'`, or `'script'`. */
tag: z.ZodEnum<["title", "base", "link", "style", "meta", "script", "noscript", "template"]>;
/** Attributes to set on the tag, e.g. `{ rel: 'stylesheet', href: '/custom.css' }`. */
attrs: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodString, z.ZodBoolean, z.ZodUndefined]>>>;
/** Content to place inside the tag (optional). */
content: z.ZodDefault<z.ZodString>;
}, "strip", z.ZodTypeAny, {
tag: "title" | "base" | "link" | "style" | "meta" | "script" | "noscript" | "template";
attrs: Record<string, string | boolean | undefined>;
content: string;
}, {
tag: "title" | "base" | "link" | "style" | "meta" | "script" | "noscript" | "template";
attrs?: Record<string, string | boolean | undefined> | undefined;
content?: string | undefined;
}>, "many">>;
export type HeadUserConfig = z.input<ReturnType<typeof HeadConfigSchema>>;
export type HeadConfig = z.output<ReturnType<typeof HeadConfigSchema>>;
export declare const HeadSchema: z.ZodDefault<z.ZodArray<z.ZodObject<{
/** Name of the HTML tag to add to `<head>`, e.g. `'meta'`, `'link'`, or `'script'`. */
tag: z.ZodEnum<["title", "base", "link", "style", "meta", "script", "noscript", "template"]>;
/** Attributes to set on the tag, e.g. `{ rel: 'stylesheet', href: '/custom.css' }`. */
attrs: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodString, z.ZodBoolean, z.ZodUndefined]>>>;
/** Content to place inside the tag (optional). */
content: z.ZodDefault<z.ZodString>;
}, "strip", z.ZodTypeAny, {
tag: "title" | "base" | "link" | "style" | "meta" | "script" | "noscript" | "template";
attrs: Record<string, string | boolean | undefined>;
content: string;
}, {
tag: "title" | "base" | "link" | "style" | "meta" | "script" | "noscript" | "template";
attrs?: Record<string, string | boolean | undefined> | undefined;
content?: string | undefined;
}>, "many">>;
/** Create a fully parsed, merged, and sorted head entry array from multiple sources. */
export declare function createHead(defaults: HeadUserConfig, ...heads: HeadConfig[]): {
tag: "title" | "base" | "link" | "style" | "meta" | "script" | "noscript" | "template";
attrs: Record<string, string | boolean | undefined>;
content: string;
}[];
/**
* Test if a head config object contains a matching `<title>` or `<meta>` tag.
*
* For example, will return true if `head` already contains
* `<meta name="description" content="A">` and the passed `tag`
* is `<meta name="description" content="B">`. Tests against `name`,
* `property`, and `http-equiv` attributes for `<meta>` tags.
*/
export declare function hasTag(head: HeadConfig, entry: HeadConfig[number]): boolean;
/**
* Test if a head config object contains a tag of the same type
* as `entry` and a matching attribute for one of the passed `keys`.
*/
export declare function hasOneOf(head: HeadConfig, entry: HeadConfig[number], keys: string[]): boolean;
/** Find the first matching key–value pair in a head entry’s attributes. */
export declare function getAttr(keys: string[], entry: HeadConfig[number]): [key: string, value: string | boolean] | undefined;
/** Merge two heads, overwriting entries in the first head that exist in the second. */
export declare function mergeHead(oldHead: HeadConfig, newHead: HeadConfig): {
tag: "title" | "base" | "link" | "style" | "meta" | "script" | "noscript" | "template";
attrs: Record<string, string | boolean | undefined>;
content: string;
}[];
/** Sort head tags to place important tags first and relegate “SEO” meta tags. */
export declare function sortHead(head: HeadConfig): {
tag: "title" | "base" | "link" | "style" | "meta" | "script" | "noscript" | "template";
attrs: Record<string, string | boolean | undefined>;
content: string;
}[];
/** Get the relative importance of a specific head tag. */
export declare function getImportance(entry: HeadConfig[number]): 100 | 90 | 70 | 80 | 0;