Copy MarkDown HTML to Clipboard from the Terminal / Shell
I have a quite terminal based workflow. I needed a way to copy the HTML version of a MarkDown file, straight to my clipboard. I love writing in my favourite text editor and I know a lot of other people do. They don't want to bother with browser windows and their lack of key bindings and shortcuts. Projects like GHOST certainly make things easier for people like me, but I still have a majority of my blogs running WordPress.
Compile MarkDown to HTML and copy to Clipboard
To compile markdown to HTML I use the marked module on npm. Somewhere on StackOverflow I found the solution how to put an output of a process into the clipboard, xclip. The outcome is this little shell script:
#!/bin/bash
# requires xclip (apt)
# requires marked (npm)
# Usage: mdclip filename.md
marked $1 | xclip -selection c
This works on linux, but I'm sure there is a similar way for MacOS. To make this script globally available, you just have to add the path to your script folder in your .bashrc file: export PATH=~/scripts/:$PATH
.
This way I can keep my MarkDown files as sources, for the posts that I want to keep in MarkDown, not only inside the blogs database and as HTML.
The post I found on StackOverflow was basically just targeted at reading a file. Mixing in the marked module is really just replacing the cat with marked, since it follows the default I/O patterns of the shell.
I love the environment that enables me to do this stuff and through 29 characters allows me to do what I want to do at least once a day without doing annoying things like dragging my mouse up an automatically scrolling window or other crazy, not very user friendly stuff.