New extension: Markdown to RTF

Markdown to RTF

Convert Markdown source to formatted text, and place it on the clipboard.

Description

It interprets the plain text of the selection as Markdown, and converts it to Rich Text Format (RTF) and copies it to the clipboard.

By contrast with the Markdown extension, which produces HTML source code as plain text, this extension produces RTF content whicosh is labelled as RTF on the clipboard. This means it is recognised by any Mac app which supports pasting RTF content.

A note on formatting

As part of the conversion process, decision had to baked in regarding font styles, indentation, spacing etc. The conversion tries to use use simple, sane choices for formatting.

Please send feedback to Nick if you have comments about the output formatting choices or notice any glitches.

Limitations

Not all markdown features are currently supported:

  • ---- to produce a horizontal separator will not work

  • HTML tags mixed into the Markdown will not be processed

To-do, notes

  • Add a sub-action to set the output font by selecting some text in that font?

  • Icon is pretty poor. I’m open to better ideas!

About

This is an extension for PopClip.

Author

Nick Moore

Requirements

  • Requires PopClip 2022.12.

  • Requires macOS 12.0.

Changelog

2023-01-20

  • Proper release no that 2022.12 is available.

29 Nov 2022

  • Simplify and streamline the extension; change minimum requirement to next beta.

17 Sep 2022

  • Change default behaviour to Copy.

13 Apr 2022

  • Initial test release.
4 Likes

I wanted that! Too bad, though, that it requires macOS 12.
What’s the reason for requiring 12? Are you using APIs that were only added then?

2 Likes

Yes, it uses a new NSAttributedString Markdown initializer that was added in 12.0.

1 Like

Ah, nice. Do you’re basically creating an attributed string from markdown and then export it as RTF. Neat.

That also explains why yours can’t handle html tags - because Apple’s code doesn’t, either.

I need md-to-html conversion myself in one of my major. I use a public markdown renderer that generates html, which I then, using the old WebKit engine, export as PDF. Since that API has been deprecated, I’m seeking a replacement. Will this give a try, with exporting the attributed string to html (or webarchive, which can be parsed as a plist then).

Ah, good luck. One thing to note, even after parsing with the NSAttributedString initializer, the string still does not have any formatting. Instead, it’s marked up with “presentation intent” tags saying what each part is (this part is bold, this part is an unordered list element, etc.). You have to loop through manually and assign concrete styling to the string. You also have to insert line breaks yourself. It’s not difficult but is a bit tedious to code out. I’ll drop my code in a gist. It’s not pretty (especially the part that deals with bulleted/numbered lists, which is gnarly), but works.

1 Like