styfle styfle - 1 year ago 174
Markdown Question

Use compact lists when converting from docx to markdown

I'm using pandoc on Windows to convert from a

file to a

The flags I'm using are the following:

pandoc --wrap none --to markdown_github --output "FMS.docx"

When I view the output markdown file, it has newlines separating each list item. The documentation defines this as a loose list such as the one below.

- one

- two

- three

I want to use a compact list for the output such as the one below.

- one
- two
- three

Is there a flag to make pandoc output a compact list?

If not, how can I use a filter to achieve the desired output?

Answer Source

There is no flag to achieve this, but there is a simple solution using pandoc's filter functionallity. Internally, list items are represented as a list of blocks; a list is compact if all block items only consist of Plain blocks. If all items consist of only a single paragraph, then it is sufficient to change the type of the item block from Para (for paragraph) to Plain.

The Haskell program below does just that. Safe it and use it as a filter: pandoc -t markdown --filter the-filter.hs your-document.docx

import Text.Pandoc.JSON

main = toJSONFilter compactifyList

compactifyList :: Block -> Block
compactifyList blk = case blk of
  (BulletList items)         -> BulletList $ map compactifyItem items
  (OrderedList attrbs items) -> OrderedList attrbs $ map compactifyItem items
  _                          -> blk

compactifyItem :: [Block] -> [Block]
compactifyItem [Para bs] = [Plain bs]
compactifyItem item      = item

The same would also be possible using a Python filter in case Haskell isn't an option. See pandoc's scripting page for details.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download