/** * HTML ↔ Markdown Conversion Utilities * * This module provides utilities for converting between HTML and Markdown formats. * It uses specialized libraries for optimal performance: * - Turndown for HTML → Markdown conversion * - Marked for Markdown → HTML conversion * * These utilities are particularly useful for Sunsama API task notes and comments * where content can be provided in either format and needs conversion to the other. */ /** * Yjs formatting attributes for rich text */ export interface YjsTextAttributes { bold?: true; italic?: true; link?: { href: string; }; underline?: true; strikethrough?: true; code?: true; } /** * Represents a segment of text with optional formatting attributes */ export interface FormattedSegment { /** The text content */ text: string; /** Formatting attributes (e.g., bold, italic, link) */ attributes?: YjsTextAttributes; } /** * Block types supported by Sunsama's editor */ export type BlockType = 'paragraph' | 'blockquote' | 'horizontalRule' | 'codeBlock' | 'bulletList' | 'orderedList'; /** * Represents a list item in a bullet or ordered list */ export interface ListItem { /** Content segments for the list item */ segments: FormattedSegment[]; } /** * Represents a block-level element in the document */ export interface DocumentBlock { /** The type of block */ type: BlockType; /** Content segments (for paragraph, codeBlock) */ segments?: FormattedSegment[]; /** Nested blocks (for blockquote) */ children?: DocumentBlock[]; /** List items (for bulletList, orderedList) */ items?: ListItem[]; /** Start number for ordered lists */ start?: number; } /** * Configuration options for HTML to Markdown conversion */ export interface HtmlToMarkdownOptions { /** Whether to preserve HTML tags that don't have Markdown equivalents */ preserveHtml?: boolean; /** Whether to use GitHub Flavored Markdown features */ gfm?: boolean; /** Custom rules for specific HTML elements */ customRules?: Record; /** Whether to keep links as-is or convert to reference style */ linkStyle?: 'inlined' | 'referenced'; /** Whether to preserve line breaks from HTML */ br?: string; } /** * Configuration options for Markdown to HTML conversion */ export interface MarkdownToHtmlOptions { /** Whether to sanitize HTML output for security */ sanitize?: boolean; /** Whether to enable GitHub Flavored Markdown features */ gfm?: boolean; /** Whether to break on single line breaks */ breaks?: boolean; /** Custom renderer options */ renderer?: unknown; } /** * Combined options for bidirectional conversion */ export interface ConversionOptions { htmlToMarkdown?: HtmlToMarkdownOptions; markdownToHtml?: MarkdownToHtmlOptions; } /** * Converts HTML content to Markdown format * * @param html - The HTML content to convert * @param options - Configuration options for conversion * @returns The converted Markdown content * @throws SunsamaAuthError if input validation fails * * @example * ```typescript * const html = '

Hello World

This is bold text.

'; * const markdown = htmlToMarkdown(html); * console.log(markdown); // "# Hello World\n\nThis is **bold** text." * ``` */ export declare function htmlToMarkdown(html: string, options?: HtmlToMarkdownOptions): string; /** * Converts Markdown content to HTML format * * @param markdown - The Markdown content to convert * @param options - Configuration options for conversion * @returns The converted HTML content * @throws SunsamaAuthError if input validation fails * * @example * ```typescript * const markdown = '# Hello World\n\nThis is **bold** text.'; * const html = markdownToHtml(markdown); * console.log(html); // "

Hello World

\n

This is bold text.

" * ``` */ export declare function markdownToHtml(markdown: string, options?: MarkdownToHtmlOptions): string; /** * Sanitizes HTML content to prevent XSS attacks * This is a basic implementation - consider using a dedicated library like DOMPurify for production * * @param html - The HTML content to sanitize * @returns Sanitized HTML content */ export declare function sanitizeHtml(html: string): string; /** * Utility function to safely convert between HTML and Markdown with validation * * @param content - The content to convert * @param fromFormat - Source format ('html' or 'markdown') * @param toFormat - Target format ('html' or 'markdown') * @param options - Conversion options * @returns Converted content * @throws SunsamaAuthError if conversion fails or formats are invalid * * @example * ```typescript * const html = '

Hello world

'; * const markdown = convertContent(html, 'html', 'markdown'); * console.log(markdown); // "Hello **world**" * * const convertedBack = convertContent(markdown, 'markdown', 'html'); * console.log(convertedBack); // "

Hello world

" * ``` */ export declare function convertContent(content: string, fromFormat: 'html' | 'markdown', toFormat: 'html' | 'markdown', options?: ConversionOptions): string; /** * Parses markdown content into formatted segments suitable for Yjs XmlText insertion. * * This function converts markdown text into an array of segments, where each segment * contains the text content and optional formatting attributes (bold, italic, link, etc.). * The segments can be used to insert rich text into a Yjs document with proper formatting. * * @param markdown - The markdown content to parse * @returns Array of formatted segments with text and Yjs-compatible attributes * * @example * ```typescript * const segments = parseMarkdownToSegments('This is **bold** and *italic* text'); * // Returns: * // [ * // { text: 'This is ' }, * // { text: 'bold', attributes: { bold: true } }, * // { text: ' and ' }, * // { text: 'italic', attributes: { italic: true } }, * // { text: ' text' } * // ] * * const linkSegments = parseMarkdownToSegments('Visit [Google](https://google.com)'); * // Returns: * // [ * // { text: 'Visit ' }, * // { text: 'Google', attributes: { link: 'https://google.com' } } * // ] * ``` */ export declare function parseMarkdownToSegments(markdown: string): FormattedSegment[]; /** * Parses markdown content into a document structure with block-level elements. * * This function converts markdown text into an array of document blocks, where each block * represents a block-level element (paragraph, blockquote, horizontal rule, etc.). * This structure is suitable for creating proper Yjs XmlElement hierarchies that match * Sunsama's rich text editor format. * * @param markdown - The markdown content to parse * @returns Array of document blocks representing the document structure * * @example * ```typescript * const blocks = parseMarkdownToBlocks('Hello **world**\n\n> A quote\n\n---'); * // Returns: * // [ * // { type: 'paragraph', segments: [{ text: 'Hello ' }, { text: 'world', attributes: { bold: true } }] }, * // { type: 'blockquote', children: [{ type: 'paragraph', segments: [{ text: 'A quote' }] }] }, * // { type: 'horizontalRule' } * // ] * ``` */ export declare function parseMarkdownToBlocks(markdown: string): DocumentBlock[]; //# sourceMappingURL=conversion.d.ts.map