@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.
90 lines (89 loc) • 3.34 kB
TypeScript
import type { Token } from 'marked';
/**
* Analyzes a string to determine if it contains an HTML tag and its characteristics.
*
* @param {string} raw - Raw string potentially containing an HTML tag
* @returns {Object|null} Returns null if no tag found, otherwise returns:
* {
* tag: string - The name of the HTML tag
* isOpening: bool - True if opening tag, false if closing
* }
*
* @example
* isHtmlOpenTag('<div class="test">') // Returns { tag: 'div', isOpening: true }
* isHtmlOpenTag('</span>') // Returns { tag: 'span', isOpening: false }
* isHtmlOpenTag('plain text') // Returns null
*/
export declare const isHtmlOpenTag: (raw: string) => {
tag: string;
isOpening: boolean;
} | null;
/**
* Parses HTML attributes from a tag string into a structured object.
* Handles both single and double quoted attributes, plus bare boolean
* attributes. Quoted regions are stripped before the boolean pass so
* space-separated words inside a value (e.g. `bar` in `title="foo bar
* baz"`) aren't mistakenly harvested as boolean attributes (issue #297).
*
* @param raw - Raw HTML tag string containing attributes.
* @returns Map of attribute names to their values. Boolean attributes
* are represented as `''`.
*
* @example
* extractAttributes('<div class="foo" id="bar">')
* // → { class: 'foo', id: 'bar' }
*
* extractAttributes('<Tip title="foo bar baz">')
* // → { title: 'foo bar baz' } // not { title: …, bar: '' }
*
* extractAttributes('<input type="checkbox" checked disabled>')
* // → { type: 'checkbox', checked: '', disabled: '' }
*
* @internal
*/
export declare const extractAttributes: (raw: string) => Record<string, string>;
export declare const parseHtmlBlock: (html: string) => Token[];
export declare const containsMultipleTags: (html: string) => boolean;
/**
* Primary entry point for HTML token processing. Transforms flat token arrays
* into properly nested structures while preserving HTML semantics.
*
* Key features:
* - Breaks down complex HTML structures into atomic tokens
* - Formats self-closing tags with proper syntax (e.g., <br> -> <br/>)
* - Maintains attribute information
* - Preserves proper nesting relationships
* - Handles malformed HTML gracefully
*
* @param {Token[]} tokens - Array of tokens to process
* @returns {Token[]} Processed and properly nested token array
*
* @example
* const tokens = [
* { type: 'html', raw: '<div class="wrapper">' },
* { type: 'text', raw: 'content' },
* { type: 'html', raw: '</div>' }
* ];
* shrinkHtmlTokens(tokens);
* // Returns nested structure with proper token relationships
*
* @public
*/
export declare const shrinkHtmlTokens: (tokens: Token[]) => Token[];
/**
* Core token processing logic that handles the complexities of HTML nesting.
* Uses a stack-based approach to match opening and closing tags while
* maintaining proper hierarchical relationships.
*
* Implementation details:
* - Maintains a stack of opening tags
* - Processes nested tokens recursively
* - Preserves HTML attributes
* - Handles malformed HTML gracefully
*
* @param {Token[]} tokens - Tokens to be processed
* @returns {Token[]} Processed tokens with proper nesting structure
*
* @internal
*/
export declare const processHtmlTokens: (tokens: Token[]) => Token[];