1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-15 09:04:04 +02:00
joplin/packages/app-desktop/gui/NoteEditor/utils/index.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

32 lines
1.2 KiB
TypeScript

import { FormNote } from './types';
import HtmlToMd from '@joplin/lib/HtmlToMd';
import Note from '@joplin/lib/models/Note';
const { MarkupToHtml } = require('@joplin/renderer');
export async function htmlToMarkdown(markupLanguage: number, html: string, originalCss: string): Promise<string> {
let newBody = '';
if (markupLanguage === MarkupToHtml.MARKUP_LANGUAGE_MARKDOWN) {
const htmlToMd = new HtmlToMd();
newBody = htmlToMd.parse(html, { preserveImageTagsWithSize: true });
newBody = await Note.replaceResourceExternalToInternalLinks(newBody, { useAbsolutePaths: true });
} else {
newBody = await Note.replaceResourceExternalToInternalLinks(html, { useAbsolutePaths: true });
if (originalCss) newBody = `<style>${originalCss}</style>\n${newBody}`;
}
return newBody;
}
export async function formNoteToNote(formNote: FormNote): Promise<any> {
return {
id: formNote.id,
// Should also include parent_id so that the reducer can know in which folder the note should go when saving
// https://discourse.joplinapp.org/t/experimental-wysiwyg-editor-in-joplin/6915/57?u=laurent
parent_id: formNote.parent_id,
title: formNote.title,
body: formNote.body,
};
}