Class MarkdownDocument

Builder for Markdown documents.

md for creating inline Markdown texts

Constructors

Methods

  • Add all blocks from another document.

    Useful for reusing code.

    Parameters

    Returns MarkdownDocument

    document extended by additional blocks

    const footerDoc = new MarkdownDocument().rule().paragraph('Some footer content.')
    const mainDoc = new MarkdownDocument()
    // ... add main blocks ...
    .$concat(footerDoc)
  • Add all blocks from multiple other documents.

    Useful for reusing code.

    Parameters

    Returns MarkdownDocument

    document extended by additional blocks

    const intro = new MarkdownDocument().heading(1, 'Main title').paragraph('Some text.')
    const chapter1 = new MarkdownDocument().heading(2, 'Chapter title').paragraph('Some text.')
    const chapter2 = new MarkdownDocument().heading(2, 'Chapter title').paragraph('Some text.')
    const conclusion = new MarkdownDocument().heading(2, 'Conclusion').paragraph('Some text.')

    new MarkdownDocument()
    .$concat(intro, chapter1, chapter2, conclusion)
  • Iteratively adds element to document using callback function.

    Convenient when multiple elements need be added dynamically per item in array.

    Type Parameters

    • T

      type of each array item

    Parameters

    Returns MarkdownDocument

    document with additional blocks from each array item

    new MarkdownDocument().$foreach(
    sections,
    (doc, section) => doc.heading(2, section.title).paragraph(section.content)
    )
  • Conditionally adds elements to document using callback function.

    Convenient when multiple elements are subject to same conditional logic.

    Parameters

    Returns MarkdownDocument

    document with or without additional blocks based on condition

    new MarkdownDocument()
    .$if(
    shouldIncludeFooter(),
    doc => doc.rule().paragraph('Some footer content.')
    )
    new MarkdownDocument()
    .$if(
    items.length > 0,
    doc => doc.paragraph(`${items.length} items found:`).list(items),
    doc => doc.paragraph('No items found.')
    )
  • Adds code block to document (unless empty), without any syntax highlighting.

    Parameters

    • text: Conditional<string>

      source as plain string (may be multiline)

    Returns MarkdownDocument

    document with additional code block

    new MarkdownDocument().code('npm install build-md')
    

    md.code

  • Adds code block to document (unless empty), with syntax highlighting.

    Parameters

    • lang: string

      programming language for syntax highlighting

    • text: Conditional<string>

      source as plain string (may be multiline)

    Returns MarkdownDocument

    document with additional code block

    new MarkdownDocument().code('ts', `function greet(name: string): void {
    console.log('Hello, ' + name + '!');
    }`)

    md.code

  • Adds expandable details element (unless empty) to document, without custom summary text.

    Not part of Markdown syntax, relies on support for HTML rendering.

    Parameters

    Returns MarkdownDocument

    document with additional details block

    new MarkdownDocument()
    .details('text hidden until expanded')
    .details(md`text with ${bold('inline')} and ${list(['block'])} elements.`)

    md.details

  • Adds expandable details element (unless empty) to document, with custom summary text.

    Not part of Markdown syntax, relies on support for HTML rendering.

    Parameters

    Returns MarkdownDocument

    document with additional details block

    new MarkdownDocument()
    .details('summary text always shown', 'text hidden until expanded')
    .details(
    md`summary text with ${italic('inline')} formatting`,
    md`text with ${bold('inline')} and ${list(['block'])} elements.`
    )

    md.details method

  • Adds unordered list to document (unless empty).

    Parameters

    • items: Conditional<BlockText[]>

      array of items, each of which may contain block or inline formatting

    Returns MarkdownDocument

    document with additional unordered list block

    new MarkdownDocument()
    .list(['first item', 'second item'])
    .list([
    md`first item with ${italic('text formatting')}`,
    md`second item with nested list:${md.list([
    "second item's first item",
    "second item's second item"
    ])}`
    ])

    md.list

  • Adds unordered list to document (unless empty).

    Parameters

    • kind: "unordered"

      type of list (may be ommitted since 'unordered' is the default)

    • items: Conditional<BlockText[]>

      array of items, each of which may contain block or inline formatting

    Returns MarkdownDocument

    document with additional unordered list block

    new MarkdownDocument()
    .list('unordered', ['first item', 'second item'])
    .list('unordered', [
    md`first item with ${italic('text formatting')}`,
    md`second item with nested list:${md.list('unordered', [
    "second item's first item",
    "second item's second item"
    ])}`
    ])

    md.list

  • Adds ordered list to document (unless empty).

    Parameters

    • kind: "ordered"

      type of list

    • items: Conditional<BlockText[]>

      array of items, each of which may contain block or inline formatting

    Returns MarkdownDocument

    document with additional ordered list block

    new MarkdownDocument()
    .list('ordered', ['first item', 'second item'])
    .list('ordered', [
    md`first item with ${italic('text formatting')}`,
    md`second item with nested list:${md.list('ordered', [
    "second item's first item",
    "second item's second item"
    ])}`
    ])

    md.list

  • Adds task list to document (unless empty). Also known as checklist or todo list.

    Part of extended Markdown syntax, not supported by all Markdown processors.

    Parameters

    • kind: "task"

      type of list

    • items: Conditional<[boolean, BlockText][]>

      array of items, each of which is a tuple of checked state and text (plain or with formatting)

    Returns MarkdownDocument

    document with additional task list block

    new MarkdownDocument().list('task', [
    [true, 'first task is done'],
    [false, 'second task is done']
    ])

    md.list

  • Adds table to document (unless no columns).

    Part of extended Markdown syntax, not supported by all Markdown processors.

    Parameters

    • columns: TableColumn[]

      table header - column heading texts with optional column alignments

    • rows: TableRow[]

      table body - rows with text content for column cells

    Returns MarkdownDocument

    document with additional table block

    new MarkdownDocument().table(['x', 'y'], [['0', '0'], ['5', '20']])
    
    new MarkdownDocument().table(
    [
    { heading: 'Error', alignment: 'left' },
    { heading: link('./environments.md', 'Environment'), alignment: 'center' },
    { heading: 'Occurrences', alignment: 'right' },
    ],
    [
    [
    code("TypeError: Cannot read properties of undefined (reading 'push')"),
    'production',
    '19',
    ],
    [
    code("TypeError: Cannot read properties of null (reading '0')"),
    'staging',
    '5',
    ],
    ]
    )

    md.table

  • Renders document to Markdown.

    Returns string

    Markdown string