mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-09 08:45:55 +02:00
5a620ee26e
Also improved copying plain text from Rich Text editor - in that case the HTML is converted to Markdown
33 lines
951 B
TypeScript
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;
|
|
}
|
|
|
|
}
|