Use Marked 2 preprocessor to rewrite images links in markdown document

When I started to write a blog with hexo, a static site generator who use markdown files as source, I encountered the problem that URLs of images in the document did not reflects the paths of the images on the system.
Opened with most of markdown software, the images are not displayed, the links in the document are not paths on the system. Initially, as I used absolutes links, I created a symlink of the folder on the root of my hard-drive, but this is not an ideal solution, and I couldn’t do that for every websites.

Marked 2 (marked2app.com) have a great functionality for this, you can use your own markdown preprocessor to parse the document before Marked generate and display the preview.

The simple idea here is to, in first place, see if the markdown file is located in a predefined directory (here it is the root of the blog), and if yes, replace every images’s links in the document with this root path to recreate a correct path to the image on the system.

Bellow a script example written in Ruby (marked-markdown-preprocessor.rb):

(Please note that this script may not work on the Mac App Store version of Marked 2 due to OS X restriction)

Place the script where you want, make it executable chmod +x marked-markdown-preprocessor.rb and add the path to this script in Marked preferences.

Marked 2 Preprocessor Preferences Panel

The first checkbox is also useful with Hexo, it strips from the preview, all header data of the document, this YAML data are used to define title, tags, categories of the page.