Skip to content

nccgroup/kramdown-ts

Repository files navigation

kramdown-ts

A TypeScript port of kramdown, the fast, pure-Ruby Markdown-superset converter. This implementation is designed to maintain bug-for-bug compatibility with the original kramdown parser.

As a port, kramdown-ts was written referencing the original kramdown code, meaning effectively all of the abstractions available within kramdown are also available to kramdown-ts users.

Features

  • Bug-for-bug compatibility with kramdown
  • Multiple output formats: kramdown, Roundtrip, Remirror
  • Roundtrip converter: Outputs kramdown that will parse identically to the original input, preserving exact parsing behavior even when the upstream kramdown converter output has slightly different output
  • Comprehensive parser: Supports most kramdown syntax including tables, footnotes, math expressions
  • TypeScript native with full type definitions
  • Extensible architecture with modular parsers and converters

Usage

Basic Parsing

import { Document } from 'kramdown-ts'

const source = `
# Hello World

This is a **kramdown** document with:

- Lists
- Formatting
- And more!
`

const doc = new Document(source)
console.log(doc.root) // Element tree
console.log(doc.warnings) // Any parsing warnings

Converting to Different Formats

import { Document, Converter } from 'kramdown-ts'

const doc = new Document('# Hello World')

// Convert back to kramdown
const kramdownOutput = Converter.Kramdown.convert(doc.root)

// Convert using Roundtrip so the output can be re-parsed into the same tree
const roundtripOutput = Converter.Roundtrip.convert(doc.root)

// Convert to Remirror format (requires @remirror packages)
const remirrorOutput = Converter.Remirror.convert(schema, doc.root)

Parser Options

const doc = new Document(source, {
  auto_ids: true,
  footnote_nr: 1,
  parse_block_html: true,
  // ... other kramdown options
})

Extensibility

A "Limbdown" parser is included, with a small number of additional defined extensions, new element types, and overridden parsers. This serves as a demonstration of how to extend the existing kramdown class with your own modifications if so desired. (Tree shaking will remove the extra parser if you do not use it; it exists primarily for demonstration purposes.)

Remirror support

A Remirror "converter" (output) and "parser" (input) are provided. These have been built out to support our purposes: improvements are welcome, as are ways to make the kramdown-ts Remirror support more generic. For the most part, for the overlap of Kramdown and Remirror's core functionality, you should expect that the converter and parser will perform round-trip conversion correctly. Tables, however, are always converted to HTML tables (and some additional cleaning is performed): they will not remain as kramdown tables. Additionally, as with the rest of Remirror, arbitrary HTML is not supported, and attribute values are filtered.

Supported Features

This implementation supports the core kramdown syntax including:

  • Headers
  • Paragraphs and line breaks
  • Lists (ordered and unordered)
  • Tables with alignment
  • Code blocks and spans
  • Blockquotes
  • Links and images
  • Footnotes
  • Math expressions
  • HTML blocks
  • Typography (smart quotes, etc.)

Unsupported Features

  • YAML options parsing
  • Inline option overrides

These features are unlikely to be supported unless other users create PRs for them, as they are not necessary for the existing deployment of kramdown-ts.

Development

Building

yarn install
yarn run build

Testing

yarn test

Type Checking

yarn run typecheck

Compatibility

This version was last updated to match kramdown 2.4.0. The implementation strives for bug-for-bug compatibility, meaning it reproduces the same parsing behavior and edge cases as the original Ruby implementation.

There is one known difference, where kramdown occasionally parses classes that are added to elements multiple times, resulting in duplication of class names. To ensure that kramdown-ts can output kramdown that is parsed the same as the original kramdown, it deduplicates class names when parsing them. This should not produce differences in practice, as duplicated classes are typically ignored in CSS, but it can result in a minor parse difference.

License

As with the original kramdown, kramdown-ts is licensed under the MIT license. See the COPYING file for details.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published