Class MarkdownWriter

All Implemented Interfaces:
Closeable, Flushable, Appendable, AutoCloseable

Low-level Markdown writer providing helper methods for producing Markdown output.

Extends SerializerWriter to provide Markdown-specific output helpers for headings, tables, lists, and inline formatting.

Notes:
  • This class is not thread safe and is typically discarded after one use.
  • Constructor Details

    • MarkdownWriter

      public MarkdownWriter(Writer out, boolean useWhitespace, int maxIndent, boolean trimStrings, UriResolver uriResolver)
      Constructor.
      Parameters:
      out - The underlying writer.
      useWhitespace - Whether to use whitespace formatting.
      maxIndent - The maximum indentation level.
      trimStrings - Whether to trim strings.
      uriResolver - The URI resolver for resolving relative URIs.
  • Method Details

    • heading

      public MarkdownWriter heading(int level, String text) throws IOException
      Writes a Markdown heading at the specified level.
      Parameters:
      level - The heading level (1-6).
      text - The heading text.
      Can be null (written as an empty string).
      Returns:
      This object.
      Throws:
      IOException - Thrown by underlying stream.
    • tableHeader

      public MarkdownWriter tableHeader(String... columns) throws IOException
      Writes a Markdown table header row.
      Parameters:
      columns - The column names.
      Individual column names can be null (written as an empty cell).
      Returns:
      This object.
      Throws:
      IOException - Thrown by underlying stream.
    • tableSeparator

      public MarkdownWriter tableSeparator(int numColumns) throws IOException
      Writes a Markdown table separator row.
      Parameters:
      numColumns - The number of columns.
      Returns:
      This object.
      Throws:
      IOException - Thrown by underlying stream.
    • tableRow

      public MarkdownWriter tableRow(String... values) throws IOException
      Writes a Markdown table data row.
      Parameters:
      values - The cell values.
      Individual values can be null (written as an empty cell).
      Returns:
      This object.
      Throws:
      IOException - Thrown by underlying stream.
    • bulletItem

      public MarkdownWriter bulletItem(int depth, String text) throws IOException
      Writes a Markdown bulleted list item.
      Parameters:
      depth - The indentation depth (0 = no indent).
      text - The item text.
      Can be null (written as an empty string).
      Returns:
      This object.
      Throws:
      IOException - Thrown by underlying stream.
    • horizontalRule

      Writes a Markdown horizontal rule.
      Returns:
      This object.
      Throws:
      IOException - Thrown by underlying stream.
    • blankLine

      Writes a blank line.
      Returns:
      This object.
      Throws:
      IOException - Thrown by underlying stream.
    • text

      public MarkdownWriter text(String text) throws IOException
      Writes plain text with Markdown escaping.
      Parameters:
      text - The text to write.
      Can be null (written as an empty string).
      Returns:
      This object.
      Throws:
      IOException - Thrown by underlying stream.
    • code

      public MarkdownWriter code(String text) throws IOException
      Writes text wrapped in backticks (inline code).
      Parameters:
      text - The text to render as code.
      Can be null (written as an empty string).
      Returns:
      This object.
      Throws:
      IOException - Thrown by underlying stream.
    • bold

      public MarkdownWriter bold(String text) throws IOException
      Writes bold text.
      Parameters:
      text - The text to render bold.
      Can be null (written as an empty string).
      Returns:
      This object.
      Throws:
      IOException - Thrown by underlying stream.
    • italic

      public MarkdownWriter italic(String text) throws IOException
      Writes italic text.
      Parameters:
      text - The text to render italic.
      Can be null (written as an empty string).
      Returns:
      This object.
      Throws:
      IOException - Thrown by underlying stream.
    • escapeCell

      public static String escapeCell(String text)
      Escapes a string for use inside a Markdown table cell.

      Escapes pipe characters and backslashes. Replaces newlines with <br>.

      Parameters:
      text - The raw text.
      Can be null (returns an empty string).
      Returns:
      The escaped text suitable for a table cell. Never null.
    • escapeText

      public static String escapeText(String text)
      Escapes a string for use in general Markdown text.

      Escapes backslashes and leading Markdown special characters.

      Parameters:
      text - The raw text.
      Can be null (returns an empty string).
      Returns:
      The escaped text. Never null.