What are the options to parse Markdown document and process its elements to output an another Markdown document?
Let\'s say it
```
# unaffected #
``
Here is a solution with an external markdown parser, pandoc
. It allows for custom filters in haskell or python to modify the input (there also is a node.js port). Here is a python filter that increases every header one level. Let's save that as header_increase.py
.
from pandocfilters import toJSONFilter, Header
def header_increase(key, value, format, meta):
if key == 'Header' and value[0] < 7:
value[0] = value[0] + 1
return Header(value[0], value[1], value[2])
if __name__ == "__main__":
toJSONFilter(header_increase)
It will not affect the code block. However, it might transform setex-style headers for h1 and h2 elements (using ===
or ---
) into atx-style headers (using #
), and vice-versa.
To use the script, one could call pandoc from the command line:
pandoc input.md --filter header_increase.py -o output.md -t markdown
With node.js, you could use pdc to call pandoc.
var pdc = require('pdc');
pdc(input_md, 'markdown', 'markdown', [ '--filter', './header_increase.py' ], function(err, result) {
if (err)
throw err;
console.log(result);
});