@neabyte/chatmark
v0.1.3
Published
Expressive text formatting syntax built for everyday chat conversations
Maintainers
Readme
What is ChatMark?
ChatMark is a chat markup parser and HTML renderer. It parses paragraphs, blockquotes, fenced code blocks, and lists, with inline support for strong, emphasis, strikethrough, code, mentions, channels, filepaths, autolinks, and line breaks.
Installation
Deno:
deno add npm:@neabyte/chatmarknpm:
npm install @neabyte/chatmarkCDN (jsDelivr/esm.sh):
<script type="module">
import Chatmark from 'https://cdn.jsdelivr.net/npm/@neabyte/chatmark/dist/index.mjs'
</script>Or via esm.sh:
<script type="module">
import Chatmark from 'https://esm.sh/@neabyte/chatmark'
</script>Usage
HTML
import Chatmark from '@neabyte/chatmark'
// Render markup to an HTML string
const html = Chatmark.html('Hello *world* from @neabytelab!')
console.log(html)
// <p>Hello <strong>world</strong> from <span class="chatmark-mention" data-mention="neabytelab">@neabytelab</span>!</p>Chatmark.html() parses source, escapes HTML special characters (&, <, >, ") in text and attributes, and joins the rendered blocks with \n.
Parse
import Chatmark from '@neabyte/chatmark'
// Parse markup into a document tree
const doc = Chatmark.parse('> Quote\n\n- *bold* and _italic_\n- ~strike~ and `code`')
console.log(doc)
// {
// type: 'document',
// children: [
// {
// type: 'blockquote',
// children: [
// {
// type: 'paragraph',
// children: [{ type: 'text', value: 'Quote' }]
// }
// ]
// },
// {
// type: 'list',
// ordered: false,
// start: 1,
// items: [
// {
// children: [
// {
// type: 'paragraph',
// children: [
// { type: 'strong', children: [{ type: 'text', value: 'bold' }] },
// { type: 'text', value: ' and ' },
// { type: 'emphasis', children: [{ type: 'text', value: 'italic' }] }
// ]
// }
// ]
// },
// {
// children: [
// {
// type: 'paragraph',
// children: [
// { type: 'strike', children: [{ type: 'text', value: 'strike' }] },
// { type: 'text', value: ' and ' },
// { type: 'code', value: 'code' }
// ]
// }
// ]
// }
// ]
// }
// ]
// }| Element | Rule |
| ----------------- | ------------------------------------------------------------------------------------------------------------------------ |
| Document | Returns { type: 'document', children } containing the parsed blocks. |
| Blockquote | Nests up to a depth of 64. |
| Ordered list | Keeps start when not 1. |
| Fenced code block | Takes the first word after the fence as lang. |
| Mention | Matches @ followed by 3 to 35 characters of a-z, 0-9, ., or _ (at most one . and one _, no outer .). |
| Channel | Matches # followed by 1 to 80 characters of a-z, 0-9, ., _, or -. |
| Filepath | Matches /path/to/file or C:\path\to\file up to 4096 characters. |
| Autolink | Matches valid http:// and https:// URLs up to 2048 characters. |
API
Chatmark.html(source)
| Parameter | Type | Description |
| --------- | -------- | ---------------------- |
| source | string | Chat markup to render. |
Returns: string.
| Element | Syntax | Output |
| ----------------- | -------------------------- | ------------------------------------------------------------------------------------ |
| Paragraph | text | <p>text</p> |
| Blockquote | > quote | <blockquote>\n<p>quote</p>\n</blockquote> |
| Fenced code block | ```lang | <pre><code class="chatmark-codeblock-lang">...</code></pre> |
| Bullet list | - item | <ul>\n<li>item</li>\n</ul> |
| Ordered list | 1. item | <ol>\n<li>item</li>\n</ol> |
| Strong | *text* or **text** | <strong>text</strong> |
| Emphasis | _text_ or __text__ | <em>text</em> |
| Strikethrough | ~text~ or ~~text~~ | <del>text</del> |
| Inline code | `code` | <code>code</code> |
| Mention | @handle | <span class="chatmark-mention" data-mention="handle">@handle</span> |
| Channel | #channel | <span class="chatmark-channel" data-channel="channel">#channel</span> |
| Filepath | /path/to/file | <span class="chatmark-filepath" data-filepath="/path/to/file">/path/to/file</span> |
| Autolink | https://example.com | <a href="https://example.com">https://example.com</a> |
| Hard line break | Two trailing spaces or \ | <br />\n |
Chatmark.parse(source)
| Parameter | Type | Description |
| --------- | -------- | --------------------- |
| source | string | Chat markup to parse. |
Returns: ChatmarkDocument.
interface ChatmarkDocument {
type: 'document'
children: ChatmarkBlock[]
}
interface ChatmarkParagraph {
type: 'paragraph'
children: ChatmarkInline[]
}
interface ChatmarkItem {
children: Array<ChatmarkParagraph | ChatmarkListBlock>
}
interface ChatmarkListBlock {
type: 'list'
ordered: boolean
start: number
items: ChatmarkItem[]
}
type ChatmarkBlock =
| ChatmarkParagraph
| {
type: 'blockquote'
children: ChatmarkBlock[]
}
| {
type: 'codeblock'
lang: string
code: string
}
| ChatmarkListBlock
type ChatmarkInline =
| {
type: 'text'
value: string
}
| {
type: 'mention'
value: string
}
| {
type: 'channel'
value: string
}
| {
type: 'filepath'
value: string
}
| {
type: 'strong'
children: ChatmarkInline[]
}
| {
type: 'emphasis'
children: ChatmarkInline[]
}
| {
type: 'strike'
children: ChatmarkInline[]
}
| {
type: 'code'
value: string
}
| {
type: 'autolink'
href: string
text: string
}
| {
type: 'break'
}Build
npm run buildTesting
deno task checkdeno task testLicense
This project is licensed under the MIT license. See the LICENSE file for details.
