1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-18 09:35:20 +02:00
joplin/packages/lib/HtmlToMd.ts
Laurent Cozic 5a620ee26e Desktop: Fixes #4669: Copying code block from Rich Text editor results in two copies of the text
Also improved copying plain text from Rich Text editor - in that case the HTML is converted to Markdown
2021-04-11 19:01:06 +02:00

33 lines
951 B
TypeScript

const TurndownService = require('@joplin/turndown');
const turndownPluginGfm = require('@joplin/turndown-plugin-gfm').gfm;
import markdownUtils from './markdownUtils';
export interface ParseOptions {
anchorNames?: string[];
preserveImageTagsWithSize?: boolean;
baseUrl?: string;
}
export default class HtmlToMd {
public parse(html: string, options: ParseOptions = {}) {
const turndown = new TurndownService({
headingStyle: 'atx',
anchorNames: options.anchorNames ? options.anchorNames.map(n => n.trim().toLowerCase()) : [],
codeBlockStyle: 'fenced',
preserveImageTagsWithSize: !!options.preserveImageTagsWithSize,
bulletListMarker: '-',
emDelimiter: '*',
strongDelimiter: '**',
br: '',
});
turndown.use(turndownPluginGfm);
turndown.remove('script');
turndown.remove('style');
let md = turndown.turndown(html);
if (options.baseUrl) md = markdownUtils.prependBaseUrl(md, options.baseUrl);
return md;
}
}