Dependency-free and lean DOM parser that outputs Markdown
You can use it on the server-side as well, thanks to jsdom. The client-side version leverages the browser DOM. Originally based on html-md.
You can get it on npm.
npm install domador --save
Or bower, too.
bower install domador --save
Converts DOM tree (or HTML string) input
into Markdown. domador
takes the following options.
Convert relative links into absolute ones automatically.
Links ([foo](/bar)
) and image sources (![foo](/bar)
) are inlined. By default, they are added as footnote references [foo][1]\n\n[1]: /bar
.
The western art of combat with rapiers or rapier-like swords. It can also be set to true
to use fences like instead of spaces when delimiting code blocks.
If fencing
is enabled, fencinglanguage
can be a function
that will run on every pre
element and returns the appropriate language in the fence.
If nothing is returned, a language won't be assigned to the fence. The example below returns fence languages according to a md-lang-{language}
class found on the pre
element.
function fencinglanguage (el) {
var match = el.className.match(/md-lang-((?:[^\s]|$)+)/);
if (match) {
return match.pop();
}
}
Allows you to take over the default transformation for any given DOM element. Ignore elements you don't want to override, and return Markdown for the ones you want to change. This method is executed on every single DOM element that's parsed by domador
. The example below converts links that start with @
into mentions like @bevacqua
instead of traditional Markdown links like [@bevacqua](/users/bevacqua)
. This is particularly useful to transform Markdown-generated HTML back into the original Markdown when your Markdown parser has special tokenizers or hooks.
domador(el, {
transform: function (el) {
if (el.tagName === 'A' && el.innerHTML[0] === '@') {
return el.innerHTML;
}
}
});
Read the unit tests for examples of expected output and their inputs. Run unit tests using the command below.
npm test
Don't expect this to work for arbitrary HTML, it is intended to restore HTML compiled from a Markdown source back into Markdown.
MIT