@humanspeak/svelte-markdown
Version:
Markdown and HTML renderer for Svelte 5 — built for rendering streaming AI agent output from Claude Code, ChatGPT, and agentic workflows. XSS-safe defaults, streaming-aware sanitization, token caching, TypeScript types, and Svelte 5 runes.
68 lines (67 loc) • 2.6 kB
TypeScript
/**
* Parse and Cache Utility
*
* Handles markdown parsing with intelligent caching.
* Separates parsing logic from component code for better testability.
*
* @module parse-and-cache
*/
import type { SvelteMarkdownOptions } from '../types.js';
import type { Token, TokensList } from './markdown-parser.js';
/**
* Lexes markdown source and cleans the resulting tokens. Shared by sync and async paths.
*
* @param source - Raw markdown string to lex
* @param options - Parser options forwarded to the Marked lexer
* @param isInline - When true, uses inline tokenization (no block elements)
* @returns Cleaned token array with HTML tokens properly nested
*
* @example
* ```typescript
* import { lexAndClean } from './parse-and-cache.js'
*
* const tokens = lexAndClean('# Hello **world**', { gfm: true }, false)
* ```
*
* @internal
*/
export declare const lexAndClean: (source: string, options: SvelteMarkdownOptions, isInline: boolean) => Token[];
/**
* Parses markdown source with caching (synchronous path).
* Checks cache first, parses on miss, stores result, and returns tokens.
*
* @param source - Raw markdown string to parse
* @param options - Svelte markdown parser options
* @param isInline - Whether to parse as inline markdown (no block elements)
* @returns Cleaned and cached token array
*
* @example
* ```typescript
* import { parseAndCacheTokens } from './parse-and-cache.js'
*
* // Parse markdown with caching
* const tokens = parseAndCacheTokens('# Hello World', { gfm: true }, false)
*
* // Second call with same input returns cached result (<1ms)
* const cachedTokens = parseAndCacheTokens('# Hello World', { gfm: true }, false)
* ```
*/
export declare const parseAndCacheTokens: (source: string, options: SvelteMarkdownOptions, isInline: boolean) => Token[] | TokensList;
/**
* Parses markdown source with caching (async path).
* Uses Marked's recursive walkTokens with Promise.all to properly
* handle async walkTokens callbacks (e.g. marked-code-format).
*
* @param source - Raw markdown string to parse
* @param options - Svelte markdown parser options
* @param isInline - Whether to parse as inline markdown (no block elements)
* @returns Promise resolving to cleaned and cached token array
*
* @example
* ```typescript
* import { parseAndCacheTokensAsync } from './parse-and-cache.js'
*
* const tokens = await parseAndCacheTokensAsync('# Hello', opts, false)
* ```
*/
export declare const parseAndCacheTokensAsync: (source: string, options: SvelteMarkdownOptions, isInline: boolean) => Promise<Token[] | TokensList>;