--- name: comark description: 'Comark (Components in Markdown) parser: syntax, AST, Vue/React/Svelte/Angular renderers, plugins, and LLM streaming with auto-close.' --- # Comark - Skills Guide A high-performance markdown parser with Comark (Components in Markdown) support, built on markdown-it, offering both string-based and streaming APIs. ## Overview **Comark** extends standard markdown with a powerful component system while maintaining full compatibility with CommonMark and GitHub Flavored Markdown. It provides: - πŸš€ **High-performance parsing** with markdown-it engine - πŸ“¦ **Streaming support** with buffered and incremental modes - ⚑ **Real-time rendering** with auto-close for incomplete syntax - πŸ”§ **Comark component syntax** for custom components - 🎨 **Vue, React, Svelte & Angular renderers** with custom component mapping - πŸ“ **YAML frontmatter** support - πŸ“‘ **Automatic TOC generation** - 🎯 **Full TypeScript support** - 🌈 **Syntax highlighting** with Shiki integration ## Package Information - **Package Name:** `comark` - **Installation:** `npm install comark` or `pnpm add comark` - **Exports:** - Main parser: `comark` - Vue components: `@comark/vue` - React components: `@comark/react` - Svelte components: `@comark/svelte` - Angular components: `@comark/angular` - HTML rendering: `@comark/html` - ANSI terminal rendering: `@comark/ansi` - Nuxt module: `@comark/nuxt` ## Quick Start ### Basic Usage ```typescript import { parseMarkdown } from 'comark' const content = `--- title: Hello World --- # Hello World This is **markdown** with :icon component. ::alert{type="info"} Important message :: ` const result = await parseMarkdown(content) console.log(result.nodes) // Markdown AST console.log(result.frontmatter) // { title: 'Hello World' } console.log(result.meta) // Additional metadata ``` ### Vue Rendering ```vue ``` ### React Rendering ```tsx import { Markdown } from '@comark/react' export default function App() { return } ``` ### Svelte Rendering ```svelte ``` ### Angular Rendering ```typescript import { Component } from '@angular/core' import { Markdown } from '@comark/angular' @Component({ selector: 'app-root', standalone: true, imports: [Markdown], template: ``, }) export class AppComponent { content = `# Hello World` } ``` ## Documentation Sections This guide is organized into focused sections covering different aspects of the package: ### πŸ“ [1. Markdown Syntax](./references/markdown-syntax.md) Learn how to write Comark documents with complete syntax reference: - **Standard Markdown:** headings, text formatting, lists, links, images, blockquotes - **Frontmatter:** YAML metadata with special fields (title, depth, searchDepth) - **Comark Components:** block components (`::component`), inline components (`:component`), properties, slots, nesting - **Attributes:** custom attributes on native markdown elements using `{...}` syntax - **Code Blocks:** language specification, filename metadata, line highlighting, special characters - **Task Lists:** GFM-style checkboxes with `[x]` and `[ ]` syntax - **Tables:** GFM tables with alignment and inline markdown support **[β†’ Read Full Markdown Syntax Guide](./references/markdown-syntax.md)** --- ### πŸ”§ [2. Parsing & Document Model](./references/parsing-ast.md) Complete guide for parsing and working with `MarkdownDocument`: - **String Parsing:** `parseMarkdown()` function with options (autoUnwrap, autoClose) - **Async Parsing:** `parseMarkdown()` with Shiki syntax highlighting - **Document Structure:** serializable `MarkdownDocument` with compact array-based nodes - **Rendering Documents:** convert to HTML (`renderHtmlFromDocument` via `@comark/html`) or markdown (`renderMarkdown` via `comark/render`) - **Auto-close:** automatic closing of unclosed syntax - **Auto-unwrap:** remove unnecessary paragraph wrappers from container components **[β†’ Read Full Parsing & Document Model Guide](./references/parsing-ast.md)** --- ### βš›οΈ [3. Vue Rendering](./references/rendering-vue.md) Comprehensive guide for rendering in Vue applications: - **Basic Usage:** `Markdown` component setup - **Custom Components:** mapping custom Vue components to Comark elements - **Dynamic Loading:** `componentsManifest` for lazy-loaded components - **Slots Support:** named slots with `#slot-name` syntax - **Streaming Mode:** real-time rendering with reactive content - **Prose Components:** pre-built styled components for standard elements - **Error Handling:** built-in error capture for streaming scenarios - **Props Access:** accessing `__node` and parsed properties **[β†’ Read Full Vue Rendering Guide](./references/rendering-vue.md)** --- ### βš›οΈ [4. React Rendering](./references/rendering-react.md) Comprehensive guide for rendering in React applications: - **Basic Usage:** `Markdown` component setup - **Custom Components:** mapping custom React components to Comark elements - **Dynamic Loading:** `componentsManifest` for lazy-loaded components - **Props Conversion:** automatic HTML attribute conversion (`class` β†’ `className`, etc.) - **Streaming Mode:** real-time rendering with reactive content - **Prose Components:** pre-built styled components for standard elements - **Custom Props:** accessing parsed properties and `__node` - **CSS Class Name:** custom wrapper classes and Tailwind CSS integration **[β†’ Read Full React Rendering Guide](./references/rendering-react.md)** --- ### 🎑 [5. Svelte Rendering](./references/rendering-svelte.md) Comprehensive guide for rendering in Svelte 5 applications: - **Basic Usage:** `Markdown` component setup with `$state` - **Custom Components:** mapping custom Svelte components to Comark elements - **Dynamic Loading:** `componentsManifest` for lazy-loaded components - **Props Mapping:** attribute-to-prop conversion (close to HTML semantics) - **Streaming Mode:** real-time rendering with reactive `$state` - **Experimental Async:** `MarkdownAsync` with `` - **Prose Components:** `Prose` prefix for overriding native HTML elements **[β†’ Read Full Svelte Rendering Guide](./references/rendering-svelte.md)** --- ### πŸ…°οΈ [6. Angular Rendering](./references/rendering-angular.md) Comprehensive guide for rendering in Angular 17+ applications: - **Basic Usage:** `Markdown` standalone component setup - **Custom Components:** mapping Angular components to Comark elements - **Component Resolution:** `Prose{PascalTag}`, `PascalTag`, `tag` priority order - **Content Projection:** named slots via `` - **Streaming Mode:** real-time rendering with caret indicator - **Data Binding:** `:binding` resolution with ambient `data` input - **Pre-configured Components:** `defineMarkdownComponent` and `defineMarkdownDocumentComponent` - **Plugins:** Math (KaTeX), Mermaid, Binding with Angular component wrappers **[β†’ Read Full Angular Rendering Guide](./references/rendering-angular.md)** --- ### πŸ€– [7. Using with AI Agents](./AGENTS.md) Guide for integrating Comark in AI agent and LLM streaming workflows: - **Streaming from LLMs:** rendering incremental AI output in real time - **Auto-Close:** handling incomplete syntax from partial LLM tokens - **Caret Indicator:** showing a live cursor during generation - **Framework Examples:** Vue, React, Svelte, Angular streaming patterns - **ANSI for CLIs:** rendering AI output in terminal agents **[β†’ Read Full Agents Guide](./AGENTS.md)** --- ## Key Features Deep Dive ### Comark Component Syntax Comark extends markdown with custom components while preserving readability: ```markdown ::alert{type="warning" .important} This is a **warning** message with markdown support. :: Check out this :icon-star{.text-yellow} component. ::card #header ## Title #content Main content #footer Footer :: ``` ### Markdown Document Model Lightweight array-based structure for efficient processing: ```typescript interface MarkdownDocument { nodes: [ ["h1", { "id": "hello" }, "Hello"], ["p", {}, "Text with ", ["strong", {}, "bold"], " word"], ["alert", { "type": "info" }, "Message"] ], frontmatter: {}, meta: {} } ``` ## Common Use Cases ### 1. Static Site Generator ```typescript import { parseMarkdown } from 'comark' import { renderHtmlFromDocument } from '@comark/html' import shiki from '@comark/html/plugins/shiki' async function processMarkdownFile(filePath: string) { const content = await readFile(filePath, 'utf-8') const doc = await parseMarkdown(content, { plugins: [ shiki({ themes: { light: 'github-dark', dark: 'github-dark' }, }), ], }) return { html: await renderHtmlFromDocument(doc), frontmatter: doc.frontmatter, toc: doc.meta.toc } } ``` ### 2. Real-time Markdown Editor ```tsx import { useState } from 'react' import { Markdown } from '@comark/react' export default function Editor() { const [content, setContent] = useState('# Hello') return (