@humanspeak/svelte-markdown
Version:
Fast, customizable markdown renderer for Svelte with built-in caching, TypeScript support, and Svelte 5 runes
53 lines (52 loc) • 2.17 kB
TypeScript
import type { Component } from 'svelte';
/**
* Generic component type for filter utilities.
* Allows Component, undefined, or null values.
*/
type FilterComponent = Component<any, any, any> | undefined | null;
/**
* Creates a set of filter utility functions for renderer maps.
* This factory generates three functions: buildUnsupported, allowOnly, and excludeOnly.
*
* Used to eliminate code duplication between unsupportedRenderers.ts and unsupportedHtmlRenderers.ts.
*
* @template TKey - The string literal type for valid keys
* @template TResult - The result map type (e.g., Partial<Renderers> or HtmlRenderers)
*
* @param keys - Array of valid keys for this renderer type
* @param unsupportedComponent - The component to use for unsupported/disabled renderers
* @param defaultsMap - Map of keys to their default component implementations
*
* @returns Object containing buildUnsupported, allowOnly, and excludeOnly functions
*
* @example
* ```typescript
* import { createFilterUtilities } from './createFilterUtilities'
*
* type MyKey = 'foo' | 'bar' | 'baz'
* const keys: readonly MyKey[] = ['foo', 'bar', 'baz'] as const
* const UnsupportedComponent = () => null
* const defaults = { foo: FooComponent, bar: BarComponent, baz: BazComponent }
*
* const { buildUnsupported, allowOnly, excludeOnly } = createFilterUtilities<MyKey, Record<MyKey, Component>>(
* keys,
* UnsupportedComponent,
* defaults
* )
*
* // Block all renderers
* const allUnsupported = buildUnsupported()
*
* // Allow only 'foo' and 'bar', block 'baz'
* const allowList = allowOnly(['foo', 'bar'])
*
* // Block only 'baz', allow others with defaults
* const denyList = excludeOnly(['baz'])
* ```
*/
export declare const createFilterUtilities: <TKey extends string, TResult extends Record<string, FilterComponent>>(keys: readonly TKey[], unsupportedComponent: FilterComponent, defaultsMap: Record<TKey, FilterComponent>) => {
buildUnsupported: () => TResult;
allowOnly: (_allowed: Array<TKey | [TKey, FilterComponent]>) => TResult;
excludeOnly: (_excluded: TKey[], _overrides?: Array<[TKey, FilterComponent]>) => TResult;
};
export {};