--- title: Markdown-it description: Reuse existing markdown-it plugins in Comark or write custom parser syntax rules. links: - label: Plugin API icon: i-lucide-puzzle to: /plugins/custom/plugin-api color: neutral variant: soft - label: Parse API icon: i-lucide-file-code to: /reference/parse color: neutral variant: soft --- Comark's default parser uses [markdown-exit](https://github.com/serkodev/markdown-exit), a TypeScript rewrite of [markdown-it](https://github.com/markdown-it/markdown-it) that preserves its plugin API. Parser plugins written for markdown-it work in Comark when they add or transform tokens. ## Usage Wrap any markdown-it plugin with [`defineComarkPlugin`](/plugins/custom/plugin-api) and pass it in `markdownItPlugins`: ```typescript import { defineComarkPlugin } from 'comark' import markdownItSub from 'markdown-it-sub' import markdownItSup from 'markdown-it-sup' const subscript = defineComarkPlugin(() => ({ name: 'subscript', markdownItPlugins: [markdownItSub], })) const superscript = defineComarkPlugin(() => ({ name: 'superscript', markdownItPlugins: [markdownItSup], })) ``` ::code-group ```typescript [Parse API] import { parseMarkdown } from 'comark' const tree = await parseMarkdown('H~2~O is water, 2^10^ is 1024', { plugins: [subscript(), superscript()] }) ``` ```vue [Vue] ``` :: ## Custom syntax rules To add new syntax, write a markdown-it inline or block rule and include it in `markdownItPlugins`. Comark converts the resulting tokens into AST nodes. The following example adds `==highlighted text==` syntax: ```typescript import type { MarkdownItPlugin } from 'comark' import { defineComarkPlugin } from 'comark' const highlightRule = (state: any, silent: boolean) => { const start = state.pos const max = state.posMax if (start + 1 >= max) return false if (state.src.charCodeAt(start) !== 0x3D /* = */) return false if (state.src.charCodeAt(start + 1) !== 0x3D /* = */) return false let pos = start + 2 while (pos + 1 < max) { if (state.src.charCodeAt(pos) === 0x3D && state.src.charCodeAt(pos + 1) === 0x3D) { if (!silent) { const token = state.push('mark_open', 'mark', 1) token.markup = '==' state.pos = start + 2 state.posMax = pos state.md.inline.tokenize(state) state.push('mark_close', 'mark', -1) state.posMax = max } state.pos = pos + 2 return true } pos++ } return false } const markdownItHighlight: MarkdownItPlugin = (md) => { md.inline.ruler.before('emphasis', 'mark', highlightRule) } export default defineComarkPlugin(() => ({ name: 'highlight', markdownItPlugins: [markdownItHighlight], })) ``` `==hello==` becomes the following AST node: ```json ["mark", {}, "hello"] ``` ## Compatibility When you provide `markdownItPlugins`, they are registered directly on the underlying markdown-it instance with full access to its API: - **Inline rules**: [`md.inline.ruler`](https://markdown-it.github.io/markdown-it/#Ruler) - **Block rules**: [`md.block.ruler`](https://markdown-it.github.io/markdown-it/#Ruler) - **Core rules**: [`md.core.ruler`](https://markdown-it.github.io/markdown-it/#Ruler) - **Renderer rules**: [`md.renderer.rules`](https://markdown-it.github.io/markdown-it/#Renderer) (limited, see below) ### What works Plugins that add **parsing rules** work without changes: - Plugins that add new inline syntax (subscript, superscript, mark, insert) - Plugins that add new block syntax (containers, definition lists, footnotes) - Plugins that transform tokens via core rules (abbreviations, replacements) ### What doesn't work ::warning Plugins that rely on markdown-it's **renderer** don't affect Comark output. Comark renders from its own AST instead of calling `md.renderer`, so custom functions in `md.renderer.rules` are ignored. :: If a plugin only customizes rendering, handle the rendering side in Comark using custom components: ::code-group ```vue [Vue] ``` ```tsx [React] import { Markdown } from '@comark/react' const components = { mark: ({ children, ...props }) => {children} } {content} ``` :: ### markdown-exit vs markdown-it Comark uses [`markdown-exit`](https://github.com/serkodev/markdown-exit) as its base parser. This TypeScript rewrite preserves the markdown-it plugin API, so plugins written for markdown-it work without modification. The [`MarkdownItPlugin`](https://markdown-it.github.io/markdown-it/#MarkdownIt) type accepted by Comark is the standard plugin signature: ```typescript type MarkdownItPlugin = (md: MarkdownIt) => void ```