Skip to content

@ogygia/core · API

@ogygia/core/plugin/utils

Pure helpers plugins share, safe in any bundle. Safe HTML building and escaping, URL and srcset checks, hashing, a bounded cache, dedent and the Standard Schema types.

On this page

This page is generated from the package’s own .d.ts files on every build, so the signatures, descriptions and examples below always match the source. ogygia/plugin/utils re-exports the same module. Utils lists what each helper is for.

Exports attr · close_safe · content_css_key · content_css_key · decode_text · dedent · escape_amp_quot · escape_text · fnv1a32 · html · is_script_url · is_space · source_tag_of · srcset_candidates · trusted_html · Html · SizedLru · SrcsetCandidate · StandardIssue · StandardSchemaV1 · HtmlValue · StandardResult

import { … } from '@ogygia/core/plugin/utils';

attr

function

One attribute with its leading space, name="value": the value escaped (and checked, for a URL attribute). true places the bare name; null, undefined and false place nothing.

function attr(name: string, value: string | number | boolean | null | undefined): Html;

close_safe

function

Code that cannot leave the tag it lives in. Two HTML tokenizer traps, both in strings, regexes or comments of the code (where they are legal JS):

  • <\/script (any case) closes the tag → <\/script (the same text in a string, a template or a regex, with or without the u flag);
  • <!-- switches the tokenizer into the escaped state, where a later <script makes the REAL <\/script> no longer close the tag — the rest of the page becomes script text → \x3C!-- (the same text in a string, a template or a regex, u flag included). (a <!--b as an expression — a < !(--b) — would no longer parse. Nobody writes it.)
function close_safe(code: string): string;

content_css_key

function

The stable cross-build key for a markup module's own scoped CSS (a .svx body with a <style>): the path from the first /src/ segment, POSIX (/…/proj/src/content/docs/x.svx → content/docs/x.svx). The preprocessor that bakes the key into the module and the build that emits the CSS and writes the handoff compute it identically from the same absolute filename.

function content_css_key(abs: string): string;

content_css_key

function

The stable cross-build key for a markup module's own scoped CSS (a .svx body with a <style>): the path from the first /src/ segment, POSIX (/…/proj/src/content/docs/x.svx → content/docs/x.svx). The preprocessor that bakes the key into the module and the build that emits the CSS and writes the handoff compute it identically from the same absolute filename.

function content_css_key(abs: string): string;

decode_text

function

HTML text made plain again (text lifted out of markup, a <title>'s content): numeric character references, and the named ones a serializer writes (& < > " '  ), are decoded. Any other named reference stays as written. Plain text with no & comes back as itself.

function decode_text(s: string): string;

dedent

function

Strip the common indentation from a block (see the module's comment); dedent.inline joins it onto one line.

const dedent: {
  (text: string): string;
  (strings: TemplateStringsArray, ...values: unknown[]): string;
  inline(strings: TemplateStringsArray, ...values: unknown[]): string;
}

escape_amp_quot

function

& and " only (a value re-emitted into a double-quoted attribute exactly as it was, < kept).

function escape_amp_quot(s: string): string;

escape_text

function

Text content: &, <, > escaped. Text with none of them comes back as itself, no copy.

function escape_text(s: string): string;

fnv1a32

function

FNV-1a 32-bit over a string → uint32. The NUMERIC sibling for callers that bucket/mod rather than compare (experiment splits, build-cache keys) — no BigInt, no hex round-trip. This is THE one shared implementation; the driver/vite copies were folded into it.

function fnv1a32(s: string): number;

html

function

HTML from a template, each value escaped for where it sits. A plain string cannot say whether it is text or markup, so a value was escaped zero times in one builder and twice in another (#435, #436, #439). Here the type says it: an Html is markup that is safe to place as is, and every other value is text, escaped for its place in the template:

  • between tags: &, <, >;
  • inside a double-quoted attribute: &, ", <; single-quoted: &, ', <;
  • inside a URL attribute (href, src, action, …): the same, and a javascript:, vbscript: or data: URL becomes about:invalid#unsafe (its scheme read as a URL parser reads it: leading controls and spaces skipped, tab, LF and CR dropped anywhere);
  • anywhere else in a tag (an unquoted attribute, a tag name): refused, it throws.

An Html value (from html, attr or trusted_html) is placed as is between tags, and escaped like any text inside an attribute. It becomes its markup wherever a string is wanted (String(h), '' + h, a template literal).

html.svelte is for markup written into a Svelte template (a .svelte / .svx source): the same, and every placed value's { and } as { / }, so text an author wrote never becomes an expression at compile time. An Html value between tags is placed as is, braces included; one inside a tag (an attr(), or markup in a quoted attribute) has its braces escaped.

html<a href="${url}">${label}</a>; // url and label escaped for their places html<pre${attr('data-lang', lang)}>; // an optional attribute: nothing when null trusted_html(highlighted, "the highlighter's own markup");

const html: {
  (parts: TemplateStringsArray, ...values: HtmlValue[]): Html;
  svelte(parts: TemplateStringsArray, ...values: HtmlValue[]): Html;
}

is_script_url

function

Whether a browser would run value as script when it follows or loads it as a URL: its scheme, read as the WHATWG URL parser reads it, is javascript:, vbscript: or data:. The parser trims leading C0 controls and spaces, and drops ASCII tab, LF and CR anywhere; a scheme is an ASCII letter, then letters, digits, +, - or ., then :. A value with no scheme is a relative URL. value is the attribute's value as the DOM holds it (character references decoded).

Self-contained (no outer names): the inline restorer ships it as its own source.

function is_script_url(value: string): boolean;

is_space

function

The regex \s: ASCII whitespace and the Unicode spaces (NaN, past the end, is not).

function is_space(c: number): boolean;

source_tag_of

function

The receipt tag one (source, args) call files, or null when fn is not a declared source.

function source_tag_of(fn: SourceFn, args?: unknown[]): string | null;

srcset_candidates

function

The candidates of a srcset value, split the way the HTML spec splits one: a URL runs to whitespace (its trailing commas dropped), and its descriptor runs to the next comma. A comma inside a URL (an image CDN's options, a data: URL) is the URL's.

function srcset_candidates(value: string): SrcsetCandidate[];

trusted_html

function

Markup made elsewhere and vouched for (a highlighter's output, a peer's signed document), placed as is. why says what makes it safe, for whoever reads the call.

function trusted_html(markup: string, why: string): Html;

Html

class

Markup that is safe to place as is: made by html, attr or trusted_html only.

Html.constructor

(made by this module's functions; the token keeps everyone else out)

Html.toString

Html.valueOf

class Html {/*…*/}
constructor(token: typeof MADE_HERE, markup: string);
toString(): string;
valueOf(): string;

SizedLru

class

SizedLru.max_bytes

SizedLru.max_entries

SizedLru.constructor

SizedLru.get

The live value for key (an LRU touch), or null on a miss or past expires.

SizedLru.has

SizedLru.set

Store value (bytes big, live until expires), evicting the least recently used entries to make room. A value larger than the whole budget is not stored.

SizedLru.delete

SizedLru.keys

Every live key (insertion order — least recently used first).

SizedLru.sweep

Drop everything past expires (harness counts want live entries only).

SizedLru.clear

SizedLru.size

SizedLru.bytes

class SizedLru<V> {/*…*/}
readonly max_bytes: number;
readonly max_entries: number;
constructor(max_bytes: number, max_entries: number);
get(key: string, now: number): V | null;
has(key: string): boolean;
set(key: string, value: V, bytes: number, expires: number): void;
delete(key: string): boolean;
keys(): IterableIterator<string>;
sweep(now: number): void;
clear(): void;
get size(): number;
get bytes(): number;

SrcsetCandidate

interface

One candidate of a srcset / imagesrcset: its URL as written and where it sits in the value (start, end), and its descriptor (640w, 2x; '' for none).

SrcsetCandidate.url

SrcsetCandidate.start

SrcsetCandidate.end

SrcsetCandidate.descriptor

interface SrcsetCandidate {/*…*/}
url: string;
start: number;
end: number;
descriptor: string;

StandardIssue

interface

StandardIssue.message

StandardIssue.path

interface StandardIssue {/*…*/}
readonly message: string;
readonly path?: ReadonlyArray<PropertyKey | {
    readonly key: PropertyKey;
  }>;

StandardSchemaV1

interface

The Standard Schema interface (standardschema.dev): the ~standard spec Zod / Valibot / ArkType all implement, the very thing Kit's remote functions consume. Validate a value with any schema library. Types only.

StandardSchemaV1."~standard"

interface StandardSchemaV1<Output = unknown> {/*…*/}
readonly "~standard": {
    readonly version: 1;
    readonly vendor: string;
    readonly validate: (value: unknown) => StandardResult<Output> | Promise<StandardResult<Output>>;
    readonly types?: {
      readonly output: Output;
    };
  };

HtmlValue

type

What a hole of html takes: text (escaped), an Html (kept between tags), a number, or nothing (null, undefined and false place nothing).

type HtmlValue = string | number | Html | null | undefined | false;

StandardResult

type

type StandardResult<Output> = {
  readonly value: Output;
  readonly issues?: undefined;
} | {
  readonly issues: ReadonlyArray<StandardIssue>;
};