2020-12-02 15:43:44 +00:00
|
|
|
const TurndownService = require('@joplin/turndown');
|
|
|
|
const turndownPluginGfm = require('@joplin/turndown-plugin-gfm').gfm;
|
2021-04-11 19:01:06 +02:00
|
|
|
import markdownUtils from './markdownUtils';
|
2018-05-16 14:16:14 +01:00
|
|
|
|
2021-04-11 19:01:06 +02:00
|
|
|
export interface ParseOptions {
|
|
|
|
anchorNames?: string[];
|
|
|
|
preserveImageTagsWithSize?: boolean;
|
|
|
|
baseUrl?: string;
|
2021-09-19 13:00:06 +01:00
|
|
|
disableEscapeContent?: boolean;
|
2021-04-11 19:01:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default class HtmlToMd {
|
|
|
|
|
|
|
|
public parse(html: string, options: ParseOptions = {}) {
|
2018-05-22 00:54:23 +01:00
|
|
|
const turndown = new TurndownService({
|
|
|
|
headingStyle: 'atx',
|
2020-05-21 09:14:33 +01:00
|
|
|
anchorNames: options.anchorNames ? options.anchorNames.map(n => n.trim().toLowerCase()) : [],
|
2019-06-22 18:57:41 +01:00
|
|
|
codeBlockStyle: 'fenced',
|
2020-03-09 23:24:57 +00:00
|
|
|
preserveImageTagsWithSize: !!options.preserveImageTagsWithSize,
|
2020-03-23 00:47:25 +00:00
|
|
|
bulletListMarker: '-',
|
|
|
|
emDelimiter: '*',
|
|
|
|
strongDelimiter: '**',
|
2020-12-02 15:43:44 +00:00
|
|
|
br: '',
|
2021-09-19 13:00:06 +01:00
|
|
|
disableEscapeContent: 'disableEscapeContent' in options ? options.disableEscapeContent : false,
|
2019-07-29 15:43:53 +02:00
|
|
|
});
|
|
|
|
turndown.use(turndownPluginGfm);
|
2018-05-20 10:19:59 +01:00
|
|
|
turndown.remove('script');
|
2018-05-24 13:32:43 +01:00
|
|
|
turndown.remove('style');
|
2019-07-29 15:43:53 +02:00
|
|
|
let md = turndown.turndown(html);
|
2018-05-23 12:14:38 +01:00
|
|
|
if (options.baseUrl) md = markdownUtils.prependBaseUrl(md, options.baseUrl);
|
|
|
|
return md;
|
2018-05-16 14:16:14 +01:00
|
|
|
}
|
|
|
|
|
2021-04-11 19:01:06 +02:00
|
|
|
}
|