Index ¦ Archives ¦ RSS

Markdown import in Writer: the new template option

Estimated read time: 3 minutes

Writer recently got a new markdown import option to take styles from a template, leading to much prettier output when converting markdown to PDF, DOCX or ODT.

This work is primarily for Collabora Online, but the templating feature is available in desktop Writer as well.

Motivation

A previous post mentioned recent improvements to the markdown import/export in Writer.

But if you convert some markdown to e.g. PDF, all the headings just have the default look, wouldn't it be nice to take your organization template and add colors and other formatting there, automatically?

Also, wouldn't it be nice if you could paste as markdown in COOL or copy the current selection as markdown? Which would enable all sorts of interesting use-cases, like using an external API to turn the selection into a summary or translating it to a different language.

Results so far

Here is a sample input markdown:

# heading 1

body text

Here is how it looks like if you template it using the core.git sw/qa/filter/md/data/template.docx sample:

PDF result: templated

curl invocation for this:

curl -k -F "data=@/path/to/test.md" -F "template=@/path/to/template.docx" -F "format=pdf" -o out.pdf https://localhost:9980/cool/convert-to

Or example desktop command-line:

soffice --infilter='Markdown:{"TemplateURL":{"type":"string","value":"./template.ott"}}' test.md

While it would look like this by default:

PDF result: default

The other part is the PostMessage API of COOL, if you want to copy and paste as markdown. What's newly possible:

  • Copy the current selection: set MessageId to Action_Copy and the value to {"Mimetype": "text/markdown;charset=utf-8"}
  • Paste at the current cursor position: set MessageId to Action_Paste and the value to something like {"Mimetype": "text/markdown;charset=utf-8", "Data": "foo _bar_ baz"}

You can read more about the PostMessage API in the COOL SDK.

How is this implemented?

If you would like to know a bit more about how this works, continue reading... :-)

As usual, the high-level problem was addressed by a series of small changes. Core side:

Online side:

Want to start using this?

You can get a development edition of Collabora Online 25.04 and try it out yourself right now: try the development edition. Collabora intends to continue supporting and contributing to LibreOffice, the code is merged so we expect the core of this work will be available in TDF's next release too (26.2).

© Miklos Vajna. Built using Pelican. Theme by Giulio Fidente on github.