Automatic Text Replacement in Viewer Mode (Macros)

It would be nice to have a replacement engine that allows definition of replacement templates (in a file) that are used in viewer mode. So the file contents are left as they are but the visual appearance in viewer mode is changed. Ideally, this would work for live preview mode as well.

Example

This is probably best illustrated by an example.

A replacement template for abbreviations could look like

- file path : Abbreviations
  argument separator : " : "
  search term : $1
  replacement term : <abbr title="$2">$1<\abbr>

For a file Abbreviations with contents

abbr - Abbreviation

and a markdown file with contents

Some text abbr more text

the the viewer would actually render the file as if its contents were

Some text <abbr title="Abbreviation">abbr</abbr> more text

Motivation

Why do it like this and not use automatic replacement as provided e.g. by the inline scripts plugin? The benefits are

  • This approach doesn’t clutter source mode view.
  • No mental overhead. I don’t have to think that I want to insert an abbreviation. I can just happily type away.
  • No editing of past occurrences when I want to change some of the replacements.

Thoughts

It would be nice to be able to switch between regex and normal search terms (e.g. by switching from search term to search regex). I really like the idea of regex search & replace templates.
I’m not settled on the format of the replacement files, the syntax for variables and escaping but I think the argument separator field and $1 are a good starting point. Maybe tables with named elements are an option as well.
An alternative would be to let the templates be defined as arbitrary js functions that return a pair of strings for regex search/replace. one could then use a single file for all replacements that basically lists function calls. However, that would probably hamper accessibility quite a bit.
Maybe something along the lines of C style macro expansion would be a nice.

Beware: This is quite close to changing the syntax of markdown. A form of power that can be dangerous if not controlled.

Implementation Details & required Interfaces

Essentially, it would be nice to have a hook for pre-processing the MD source, while what we have right now is a hook for post-processing the generated HTML. Working on the DOM would also work for most cases but is overkill in most and harder to implement.

Quoting the discussion from the very much related topic Is there a way to run note text through a command line preprocess before displaying it in preview?:

Related Topics

This is basically a generalization of

And another topic arguing for a pre-processing hook