matheus23 / elm-markdown-transforms / Markdown.PrettyTables

Pretty-printing Markdown files with Tables

(Tables defined by the github flavoured markdown (GFM) spec).

Elm-markdown supports GFM tables. The function reducePretty in the Markdown.Scaffolded module doesn't support pretty printing these.

This module includes the function reducePrettyTable, which supports pretty-printing markdown tables.

Due to this being slightly complicated, this functionality was moved into this module together with its helpers.

reducePrettyTable : TableStyle -> Markdown.Scaffolded.Block (Basics.Int -> TableInfo String) -> Basics.Int -> TableInfo String

Convert a block of markdown back to markdown text.

See the 'Formatting Markdown Tables' test in the example/ folder:

  1. It will show you how pretty-printed tables will look like
  2. It shows you how to transform the result of this function to a string (See finishReduction).

finishReduction : List (Basics.Int -> TableInfo String) -> String

Transform the result of reducePrettyTable after being put through Markdown.render to a string.

Use it like this:

markdownBlocks
    |> Markdown.render
        (Scaffolded.toRenderer
            { renderHtml = Markdown.Html.oneOf []
            , renderMarkdown = Tables.reducePrettyTable style
            }
        )
    |> Result.map Tables.finishReduction

Rendering styles


type alias TableStyle =
{ renderCell : String -> ColumnInfo -> String
, renderDelimiter : List ColumnInfo -> String
, renderRow : List String -> String 
}

The style to render tables to. There are two values for this provided for your convenience (defaultStyle, compactStyle), so check those out before creating a value of your own.

In case you still want to, you need to provide:

  1. renderCell: A function that renders a cell content and applies appropriate padding using the given ColumnInfo.
  2. renderDelimiter: A function that renders the horizontal line between the table header and table body. Remember that to write valid markdown, you need to include a pipe (|) for each delimiter between columns.
  3. renderRow: A function for combining multiple cells in a row. Remember that to write valid markdown, you'll need to include a pipe (|) between each cell.

defaultStyle : TableStyle

A sensible default for pretty-printing tables.

It will:

  1. Make all cells in a column have uniform width
  2. Left/Center/Right-align cell contents (left by default)
  3. Add a border (|) to the left and right side of your table

compactStyle : TableStyle

This style will try to produce valid markdown tables without padding or additional borders (unlike defaultStyle).

However, it's not the minimal amount of characters needed to get a parsing table.

Internal datatypes


type alias TableInfo view =
{ render : Dict Basics.Int ColumnInfo -> view
, info : Dict Basics.Int ColumnInfo 
}

The main datatype that drives pretty-printing.

This allows the pretty-printing to work in a two-step process:

  1. Collect all the pretty-printed cells and their information ColumnInfo
  2. Use the maximum cell sizes as column sizes to add padding around cells


type alias ColumnInfo =
{ size : Basics.Int
, alignment : Maybe Markdown.Block.Alignment 
}

All additional column information needed to support pretty-printing.

resolve : TableInfo view -> view

Resolve the two-step process that a TableInfo encodes by running the steps.

pure : view -> TableInfo view

Make anything a TableInfo without adding column information.

map : (a -> b) -> TableInfo a -> TableInfo b

Transform the value that would be generated in the first step of the TableInfo process by applying a function.

map2 : (a -> b -> c) -> TableInfo a -> TableInfo b -> TableInfo c

Combine two TableInfo values using given function. This does two things:

  1. It combines what the TableInfos render to in their first step using the function
  2. It merges the collected column information from both

fold : List (TableInfo a) -> TableInfo (List a)

Combine all column information inside a TableInfo from a whole list.

combineColumnInfo : ColumnInfo -> ColumnInfo -> ColumnInfo

A helper function for combining two column infos form two cells of the same column.