Pasting Markdown as Formatted Text
Pasting Markdown as Formatted Text
I’ve written (in one blog or another) about my fondness for Markdown and Pandoc. Basically, this combination allows me to use plain text files, composed in whatever text editor I want (generally EMACS, but Notepad will do in a pinch), and create useful formatting.
It may not be as full-on as Microsoft Word, but for 80% of my formatting needs, it’s great. Since it ties to styles, I can change some of the parameters quickly (in Markdown, I can designate some text as “Heading 1,” and in Word, or using CSS for HTML, define what typface, size, color, etc. that will be).
From Markdown to Word
Pandoc is great at converting files, and, if my target is .docx
, that works. To convert to Microsoft Word, the command line would be:
pandoc input-file.md -f markdown -t docx -o output-file.docx
However, if I want to paste into an email message in Outlook, or even some browser applications, it’s a bit cumbersome. The workflow is convert the Markdown file to a Word document, open it, then copy-paste the formatted text (so *this*
becomes this). I’ve cut out the middleman in EMACS, by writing some code that will write the buffer contents (in Markdown) to a designated temporary file (which I can rename, or simply copy-paste):
(defvar tempword "~/tempword-markdown.docx"
"Location and name of a temporary file to dump conversion of markdown to word. Default is ~/tempword-markdown.docx")
(defun tempword-buffer (prefix)
"Runs the buffer through pandoc and puts the output in a standard, temporary file."
(interactive)
(setq dacommand (concat "pandoc -f markdown -t docx -o " tempword))
(shell-command-on-region (point-min) (point-max) dacommand))
Going Outside of Word
The new MacBook brought with it a new wrinkle. While Microsoft makes a version of Word for OS X, they have moved to a subscription model. For my personal use, I simply couldn’t justify paying $70 a year for it.
I’ll acknowldege this negates part of one of the reasons I prefer Macs (native versions of apps everyone uses). There are plenty of native versions of common apps, and still plenty I like. It’s also always an option–I can always buy it if my needs change.
Microsoft Office Live gives me some of the capabilities, which is what I was doing on Linux. It integrates with my tempword-buffer
function, proivded I put that in my OneDrive folder. However, as a web application, it is dependent upon internet access.
Apple also makes a suite that is Mac-specific, taking advantage of the TouchBar as well. The word processor is called Pages. Unfortunately, Pandoc does not support that as a format. It does read and write Word reasonably well. I figure my workflow, should I get into a situation where .docx
format is critical (like a resume), will be to export to Word, and touch it up in Microsoft Live.
But that did create an extra step. I can create a Word file from Markdown as above, but then it gets converted again to Pages (or I keep converting back and forth to Word). My copy-convert-paste system seemed more relevant.
The Ideal Solution
At the end of the day, all of these solutions were, at best, work-arounds. They work great if the end state was Microsoft Word, and were workable if my target could at least read a .docx
file. It couldn’t easly go anywhere else.
The ideal solution would be to copy the Markdown text, have it converted in the clipboard, then paste it as formatted (“rich”) text. Most applications I would need this for, be it Microsoft’s Office Suite, Apple’s, web apps, or other software, could take both text and rich format. If I needed to tweak it (such as change fonts), I could manage that through styles.
I looked, and could not find an easy solution in either Windows or Linux. But OS X comes with two very powerful command-line utilities, pbcopy and pbpaste. This allows for data in the clipboard to be routed and piped like any file. These could be used with pandoc, taking the Markdown, it into Rich Text Format (RTF). The output could be sent, with formatting, to the clipbaord. I just needed to put this command at a command prompt:
pandoc --standalone -f markdown -t rtf | pbcopy
A few tests, and it’s just what I wanted. EMACS could integrate this quite nicely:
(defun mdcopy ()
"Copies the buffer to the clipboard, converting from Markdown to Rich Text in the prodcess."
(interactive)
(shell-command-on-region (point-min) (point-max) "pandoc --standalone -f markdown -t rtf | pbcopy"))
This will take the whole buffer, copy it to the clipboard, and run the pandoc command. The output gots to the clipboard, and can be pasted into any application as rich text. A few more EMACS configuraion tweaks (and some BetterTouchTool adjustments), and I have a custom button in EMACS for it.
I see no reason a similar function couldn’t be created for any editor that allows for scripting and command line processes.
I like this combiantion, as it gives me the best of both worlds. Since I have a lot of custom functions and configurations in EMACS, as well as having built the muscle-memory of the keyboard combination, my workflow when generating text is consistent and easy. At the same time, I’m able to put the formating into docuents and mails that need it.
I’m going to look and see if there is a similar capability in Linux and Windows. To my knowledge, they don’t have perfect analogs to pbcopy and pbpaste (they don’t necessarily handle formatting the same way).