2019-07-29 13:43:53 +00:00
|
|
|
const TurndownService = require('joplin-turndown');
|
2018-05-23 11:14:38 +00:00
|
|
|
const markdownUtils = require('lib/markdownUtils');
|
2018-05-16 13:16:14 +00:00
|
|
|
|
|
|
|
class HtmlToMd {
|
2018-05-23 11:14:38 +00:00
|
|
|
parse(html, options = {}) {
|
2019-07-29 13:43:53 +00:00
|
|
|
const turndownPluginGfm = require('joplin-turndown-plugin-gfm').gfm;
|
2018-05-21 23:54:23 +00:00
|
|
|
const turndown = new TurndownService({
|
|
|
|
headingStyle: 'atx',
|
2019-06-12 23:26:09 +00:00
|
|
|
anchorNames: options.anchorNames ? options.anchorNames.map(n => n.trim().toLowerCase()) : [],
|
2019-06-22 17:57:41 +00:00
|
|
|
codeBlockStyle: 'fenced',
|
2019-07-29 13:43:53 +00:00
|
|
|
});
|
|
|
|
turndown.use(turndownPluginGfm);
|
2018-05-20 09:19:59 +00:00
|
|
|
turndown.remove('script');
|
2018-05-24 12:32:43 +00:00
|
|
|
turndown.remove('style');
|
2019-07-29 13:43:53 +00:00
|
|
|
let md = turndown.turndown(html);
|
2018-05-23 11:14:38 +00:00
|
|
|
if (options.baseUrl) md = markdownUtils.prependBaseUrl(md, options.baseUrl);
|
|
|
|
return md;
|
2018-05-16 13:16:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-29 13:43:53 +00:00
|
|
|
module.exports = HtmlToMd;
|