import Setting from '@joplin/lib/models/Setting'; import { processPastedHtml } from './resourceHandling'; import markupLanguageUtils from '@joplin/lib/markupLanguageUtils'; import HtmlToMd from '@joplin/lib/HtmlToMd'; import { HtmlToMarkdownHandler, MarkupToHtmlHandler } from './types'; const createTestMarkupConverters = () => { const markupToHtml: MarkupToHtmlHandler = async (markupLanguage, markup, options) => { const conv = markupLanguageUtils.newMarkupToHtml({}, { resourceBaseUrl: `file://${Setting.value('resourceDir')}/`, customCss: '', }); return conv.render(markupLanguage, markup, {}, options); }; const htmlToMd: HtmlToMarkdownHandler = async (_markupLanguage, html, _originalCss) => { const conv = new HtmlToMd(); return conv.parse(html); }; return { markupToHtml, htmlToMd }; }; describe('resourceHandling', () => { it('should sanitize pasted HTML', async () => { Setting.setConstant('resourceDir', '/home/.config/joplin/resources'); const testCases = [ ['Test: ', 'Test: '], ['test', 'test'], ['test', 'test'], ['evil.pdf', 'evil.pdf'], ['evil()', ''], ['', ''], [ '', '', ], ]; for (const [html, expected] of testCases) { expect(await processPastedHtml(html, null, null)).toBe(expected); } }); it('should clean up pasted HTML', async () => { const { markupToHtml, htmlToMd } = createTestMarkupConverters(); const testCases = [ ['

Hello

World

', '

Hello

\n

World

\n'], ['', ''], ]; for (const [html, expected] of testCases) { expect(await processPastedHtml(html, htmlToMd, markupToHtml)).toBe(expected); } }); it('should preserve images pasted from the resource directory', async () => { const { markupToHtml, htmlToMd } = createTestMarkupConverters(); // All images in the resource directory should be preserved. const html = `test`; expect(await processPastedHtml(html, htmlToMd, markupToHtml)).toBe(html); }); });