How do I adjust non-markdown markup before viewing a document in Marked?

Viewed 12

I write and edit text for a content management system that uses non-standard markup for links, emphasis, etc. Can I bring Marked to transform these on the fly to proper markdown – without changing the original document?

1 Answers

Yes, Marked has a function called preprocessor. With it, you can use a script to modify the input before the preview gets rendered. This script can be Python, for example, or a shell script. For many, it might be easy to write their own script. Alternatively, you can use md-fixup, a markdown linting script by the same developer. It allows to define a collection of search-and-replace commands in the form of Regular Expressions.

The md-fixup GitHub Project Page explains how to install and set up a yaml file for replacements. Once this is done, proceed in Marked 2:

  • Open the settings dialog via Menu: Marked->Settings (Cmd+,)
  • Open the "advanced" tab
  • Switch to the "Preprocessor" tab
  • Check "Enable Custom Preprocessor"
  • In the "Path" field, enter the file path of the script. If you installed md-fixup via Homebrew, enter /opt/homebrew/bin/md-fix (Apple Silicon) or /usr/local/bin/md-fixup (Intel)
  • In the "Args:" field, enter --replacements-file , followed by the path to your yaml file with individual Regular Expression replacement patterns. One easy way to get a correct file path: Hold down Cmd & Opt while you click on your file, then choose "copy as file path". Then paste into the "Args:" field after --replacements-file .
  • Click "refresh" in the bottom right corner to see the effects of your changes in open Marked windows.

The md-fixup script primarily cleans up markdown nomenclature, so there might be some changes to the result. In my case, those were actually for the better. If you are experiencing problems, you might want to skip some rules, either by entries in the "Args:" field or in the config.yml. Consult the md-fixup Project page for an overview of all the options.