3.2.86 • Published 3 days ago

@thi.ng/hiccup-markdown v3.2.86

Weekly downloads
202
License
Apache-2.0
Repository
github
Last release
3 days ago

hiccup-markdown

npm version npm downloads Twitter Follow

This project is part of the @thi.ng/umbrella monorepo.

About

Markdown parser & serializer from/to Hiccup format. This is a support package for @thi.ng/hiccup.

This package provides both a customizable Markdown-to-Hiccup parser and an extensible Hiccup-to-Markdown converter.

Status

ALPHA - bleeding edge / work-in-progress

Search or submit any issues for this package

Installation

yarn add @thi.ng/hiccup-markdown

ES module import:

<script type="module" src="https://cdn.skypack.dev/@thi.ng/hiccup-markdown"></script>

Skypack documentation

For Node.js REPL:

# with flag only for < v16
node --experimental-repl-await

> const hiccupMarkdown = await import("@thi.ng/hiccup-markdown");

Package sizes (gzipped, pre-treeshake): ESM: 2.79 KB

Dependencies

Usage examples

Several demos in this repo's /examples directory are using this package.

A selection:

ScreenshotDescriptionLive demoSource
Minimal Markdown to Hiccup to HTML parser / transformerDemoSource

API

Generated API docs

Parser

Features

The parser itself is not aimed at supporting all of Markdown's quirky syntax features, but restricts itself to a sane subset of features:

FeatureComments
HeadingATX only (# line prefix), levels 1-6, then downgrade to paragraph
Paragraphno support for \ line breaks
BlockquoteRespects newlines
Formatbold, emphasis, code, strikethrough in paragraphs, headings, lists, blockquotes, tables
Linkno support for inline formats in label
Imageno image links
Listonly unordered (- line prefix), no nesting, supports line breaks
Tableno support for column alignment
Code blockGFM only (triple backtick prefix), w/ optional language hint
Horiz. Ruleonly dash supported (e.g. ---), min 3 chars required

Note: Because of MD's line break handling and the fact the parser only consumes single characters from an iterable without knowledge of further values, the last heading, paragraph, blockquote, list or table requires an additional newline.

Current issues & limitations

Paragraphs, headings and blockquotes ending with a character involved w/ inline formatting (e.g. !, ~, *, _) either require an additional space or 2 empty lines (instead of just one) between the next paragraph. See #156 for details.

Also, these MD features (and probably many more) are currently not supported:

  • inline HTML
  • nested inline formats (e.g. bold inside italic)
  • inline formats within link labels
  • image links
  • footnotes
  • link references
  • nested / ordered / numbered / todo lists

Some of these are considered, though currently not high priority... Pull requests are welcome, though!

Other parser features

  • Functional: parser entirely built using transducers (specifically those defined in @thi.ng/fsm) & function composition. Use the parser in a transducer pipeline to easily apply post-processing of the emitted results
  • Declarative: parsing rules defined declaratively with only minimal state/context handling needed
  • No regex: consumes input character-wise and produces an iterator of hiccup-style tree nodes, ready to be used with @thi.ng/hdom, @thi.ng/hiccup or the serializer of this package for back conversion to MD
  • Customizable: supports custom tag factory functions to override default behavior / representation of each parsed result element
  • Fast (enough): parses this markdown file (5.9KB) in ~5ms on MBP2016 / Chrome 71
  • Small: minified + gzipped ~2.5KB (parser sub-module incl. deps)

Serializing to HTML

import { iterator } from "@thi.ng/transducers";
import { serialize } from "@thi.ng/hiccup";

import { parse } from "@thi.ng/hiccup-markdown";

const src = `
# Hello world

[This](http://example.com) is a _test_.

`;

// convert to hiccup tree
[...iterator(parse(), src)]
// [ [ 'h1', ' Hello world ' ],
//   [ 'p',
//     [ 'a', { href: 'http://example.com' }, 'This' ],
//     ' is a ',
//     [ 'em', 'test' ],
//     '. ' ] ]

// or serialize to HTML
serialize(iterator(parse(), src));

// <h1>Hello world</h1><p>
// <a href="http://example.com">This</a> is a <em>test</em>. </p>

Customizing tags

The following interface defines factory functions for all supported elements. User implementations / overrides can be given to the parse() transducer to customize output.

interface TagFactories {
    blockquote(children: any[]): any[];
    code(body: string): any[];
    codeblock(lang: string, body: string): any[];
    em(body: string): any[];
    heading(level, children: any[]): any[];
    hr(): any[];
    img(src: string, alt: string): any[];
    li(children: any[]): any[];
    link(href: string, body: string): any[];
    list(type: string, items: any[]): any[];
    paragraph(children: any[]): any[];
    strike(body: string): any[];
    strong(body: string): any[];
    table(rows: any[]): any[];
    td(i: number, children: any[]): any[];
    tr(i: number, cells: any[]): any[];
}

Example with custom link elements:

const tags = {
    link: (href, body) => ["a.link.blue", { href }, body]
};

serialize(iterator(parse(tags), src));

// <h1>Hello world</h1>
// <p><a href="http://example.com" class="link blue">This</a> is a <em>test</em>. </p>

Serializer (Hiccup to Markdown)

For the reverse operation, the serialize() function can be used to convert an hiccup component tree into Markdown. Currently supports most standard (applicable) Markdown features:

Features

  • ATX-style headings (level 1-6)
  • Paragraphs
  • Forced line breaks
  • Inline styles: strong, italic, code
  • Images (w/ optional alt attrib)
  • Links, image links
  • Code blocks w/ language hint (GFM output)
  • Tables
  • Blockquotes
  • Nested lists (ordered & unordered)
  • Horizontal rule / separator
  • Inline HTML

Not (yet) supported:

  • Nested blockquotes
  • Link refs
  • Wordwrapped output

Behaviors

  • Unless needed for serialization, all other hiccup element attribs are ignored
  • Code blocks are always output in GFM flavor w/ optional language hint (via lang attrib)
  • Images use the optional alt attrib as label
  • Forced line breaks are realized via ["br"] elements in the hiccup tree
  • Headings, blockquotes, list items and link labels can contain inline formatting

Also, other element types can be supported by adding a new tag specific implementation to the exported serializeElement multi-method. See source code for reference.

Usage examples

import { serialize } from "@thi.ng/hiccup-markdown";

// list component
// the 1st arg is the optional user context object
// passed to `serialize()` (ignored here)
// the 2nd arg is the list tag (ul/ol)
// rest args are converted to list items
const list = (_, type, ...xs) =>
    [type, ...xs.map((x) => Array.isArray(x) ? x : ["li", x])];

// code block component w/ lang hint
const codeblock = (_, lang, body) =>
    ["pre", { lang }, ["code", body]];

// link component for thi.ng URLs
const thingLink = (_, id, label) =>
    ["a", { href: `http://thi.ng/${id}` }, label];

// Note: the same hiccup tree can be serialized to HTML via @thi.ng/hiccup or
// used interactively in the browser w/ @thi.ng/hdom
serialize(
    ["div",
        ["h1", "Hello Markdown"],
        ["p",
            "This is a test: ",
            ["strong", "I am strong and ", ["em", "italic"]],
            "..."],
        // anon component fn to demo context lookup
        [(ctx) => ["p", `My magic number is: ${ctx.magic}`]],
        // codeblock w/ language hint
        [codeblock, "ts",
            `import { serialize } from "@thi.ng/hiccup-markdown";`],
        // nested lists
        [list, "ul",
            "foo",
            "bar",
            [list, "ol", "b1", "b2", "b3"],
            "baz"],
        ["blockquote",
            "So long and thanks for all the fish."],
        ["table",
            ["caption", ["em", "Table #1"]],
            ["thead",
                ["tr", ["th", "ID"], ["th", "Name"]]],
            ["tbody",
                ["tr", ["td", 1], ["td", "Alice B. Charles"]],
                ["tr", ["td", 2], ["td", "Bart Simpson"]]]],
        ["p",
            "More info ",
            [thingLink, "hiccup-markdown", "here"], "."]],
    // optional context object passed to all component functions
    { magic: 42 }
)

Resulting Markdown:

(Note: the GFM codeblock fences are only shown escaped here to avoid GH layout breakage)

# Hello Markdown

This is a test: **I am strong and _italic_**...

My magic number is: 42

\`\`\`ts
import { serialize } from "@thi.ng/hiccup-markdown";
\`\`\`

- foo
- bar
    1. b1
    2. b2
    3. b3
- baz

> So long and thanks for all the fish.

| **ID**  | **Name**         |
| ------- | ---------------- |
| 1       | Alice B. Charles |
| 2       | Bart Simpson     |

_Table #1_

More info [here](http://thi.ng/hiccup-markdown).

Realized result:


Hello Markdown

This is a test: I am strong and italic...

My magic number is: 42

import { serialize } from "@thi.ng/hiccup-markdown";
  • foo
  • bar
    1. b1
    2. b2
    3. b3
  • baz

So long and thanks for all the fish.

IDName
1Alice B. Charles
2Bart Simpson

Table #1

More info here.


Authors

Karsten Schmidt

If this project contributes to an academic publication, please cite it as:

@misc{thing-hiccup-markdown,
  title = "@thi.ng/hiccup-markdown",
  author = "Karsten Schmidt",
  note = "https://thi.ng/hiccup-markdown",
  year = 2018
}

License

© 2018 - 2021 Karsten Schmidt // Apache Software License 2.0

3.2.86

3 days ago

3.2.85

5 days ago

3.2.84

8 days ago

3.2.83

17 days ago

3.2.82

20 days ago

3.2.81

1 month ago

3.2.80

1 month ago

3.2.79

1 month ago

3.2.78

1 month ago

3.2.77

1 month ago

3.2.76

1 month ago

3.2.75

1 month ago

3.2.74

2 months ago

3.2.73

2 months ago

3.2.72

2 months ago

3.2.71

2 months ago

3.2.70

2 months ago

3.2.69

2 months ago

3.2.68

2 months ago

3.2.67

2 months ago

3.2.66

2 months ago

3.2.64

2 months ago

3.2.65

2 months ago

3.2.63

2 months ago

3.2.62

3 months ago

3.2.61

3 months ago

3.2.60

3 months ago

3.2.59

3 months ago

3.2.58

3 months ago

3.2.56

3 months ago

3.2.55

3 months ago

3.2.53

4 months ago

3.2.54

4 months ago

3.2.51

4 months ago

3.2.50

4 months ago

3.2.52

4 months ago

3.2.48

5 months ago

3.2.49

5 months ago

3.2.47

5 months ago

3.2.24

8 months ago

3.2.23

8 months ago

3.2.26

8 months ago

3.2.25

8 months ago

3.2.28

8 months ago

3.2.27

8 months ago

3.2.29

7 months ago

3.2.20

9 months ago

3.2.22

8 months ago

3.2.21

9 months ago

3.2.35

6 months ago

3.2.34

7 months ago

3.2.37

6 months ago

3.2.36

6 months ago

3.2.39

6 months ago

3.2.38

6 months ago

3.2.31

7 months ago

3.2.30

7 months ago

3.2.33

7 months ago

3.2.32

7 months ago

3.2.15

10 months ago

3.2.16

9 months ago

3.2.19

9 months ago

3.2.18

9 months ago

3.2.46

5 months ago

3.2.45

5 months ago

3.2.40

6 months ago

3.2.41

6 months ago

3.2.44

6 months ago

3.2.43

6 months ago

3.2.13

11 months ago

3.2.14

11 months ago

3.2.12

12 months ago

3.2.11

1 year ago

3.2.10

1 year ago

3.2.2

1 year ago

3.2.1

1 year ago

3.2.0

1 year ago

3.2.6

1 year ago

3.2.5

1 year ago

3.2.4

1 year ago

3.2.3

1 year ago

3.2.9

1 year ago

3.2.8

1 year ago

3.2.7

1 year ago

3.1.0

1 year ago

3.0.1

1 year ago

3.0.0

1 year ago

2.1.49

1 year ago

2.1.48

1 year ago

2.1.46

1 year ago

2.1.45

1 year ago

2.1.43

1 year ago

2.1.44

1 year ago

2.1.41

1 year ago

2.1.42

1 year ago

2.1.40

1 year ago

2.1.29

2 years ago

2.1.38

1 year ago

2.1.39

1 year ago

2.1.36

1 year ago

2.1.37

1 year ago

2.1.34

2 years ago

2.1.35

1 year ago

2.1.32

2 years ago

2.1.33

2 years ago

2.1.30

2 years ago

2.1.31

2 years ago

2.1.27

2 years ago

2.1.28

2 years ago

2.1.26

2 years ago

2.1.16

2 years ago

2.1.17

2 years ago

2.1.15

2 years ago

2.1.18

2 years ago

2.1.19

2 years ago

2.1.25

2 years ago

2.1.23

2 years ago

2.1.24

2 years ago

2.1.21

2 years ago

2.1.22

2 years ago

2.1.20

2 years ago

2.1.14

2 years ago

2.1.12

2 years ago

2.1.13

2 years ago

2.1.10

2 years ago

2.1.11

2 years ago

2.1.9

2 years ago

2.1.8

2 years ago

2.0.9

2 years ago

2.1.2

2 years ago

2.1.1

2 years ago

2.1.4

2 years ago

2.1.3

2 years ago

2.1.6

2 years ago

2.1.5

2 years ago

2.1.7

2 years ago

2.1.0

2 years ago

2.0.7

2 years ago

2.0.8

2 years ago

2.0.4

3 years ago

2.0.6

3 years ago

2.0.3

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.3.33

3 years ago

1.3.31

3 years ago

1.3.32

3 years ago

1.3.30

3 years ago

1.3.29

3 years ago

1.3.28

3 years ago

1.3.26

3 years ago

1.3.27

3 years ago

1.3.24

3 years ago

1.3.25

3 years ago

1.3.23

3 years ago

1.3.22

3 years ago

1.3.21

3 years ago

1.3.20

3 years ago

1.3.19

3 years ago

1.3.18

3 years ago

1.3.17

3 years ago

1.3.16

3 years ago

1.3.15

3 years ago

1.3.14

3 years ago

1.3.13

3 years ago

1.3.12

3 years ago

1.3.10

3 years ago

1.3.11

3 years ago

1.3.9

3 years ago

1.3.8

3 years ago

1.3.7

3 years ago

1.3.6

3 years ago

1.3.2

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.44

3 years ago

1.2.43

3 years ago

1.2.42

3 years ago

1.2.41

3 years ago

1.2.40

3 years ago

1.2.39

3 years ago

1.2.38

3 years ago

1.2.37

3 years ago

1.2.36

3 years ago

1.2.35

4 years ago

1.2.34

4 years ago

1.2.33

4 years ago

1.2.32

4 years ago

1.2.31

4 years ago

1.2.30

4 years ago

1.2.29

4 years ago

1.2.28

4 years ago

1.2.27

4 years ago

1.2.25

4 years ago

1.2.26

4 years ago

1.2.23

4 years ago

1.2.24

4 years ago

1.2.22

4 years ago

1.2.20

4 years ago

1.2.21

4 years ago

1.2.19

4 years ago

1.2.18

4 years ago

1.2.17

4 years ago

1.2.16

4 years ago

1.2.15

4 years ago

1.2.14

4 years ago

1.2.13

4 years ago

1.2.12

4 years ago

1.2.11

4 years ago

1.2.10

4 years ago

1.2.9

4 years ago

1.2.8

4 years ago

1.2.7

4 years ago

1.2.6

4 years ago

1.2.5

4 years ago

1.2.4

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.14

4 years ago

1.1.13

4 years ago

1.1.12

4 years ago

1.1.9

4 years ago

1.1.8

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.22

5 years ago

1.0.21

5 years ago

1.0.20

5 years ago

1.0.19

5 years ago

1.0.18

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.2.0

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago