You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-11-26 22:41:17 +02:00
Co-authored-by: Laurent Cozic <laurent22@users.noreply.github.com>
This commit is contained in:
30
packages/lib/commands/convertHtmlToMarkdown.test.ts
Normal file
30
packages/lib/commands/convertHtmlToMarkdown.test.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { runtime } from './convertHtmlToMarkdown';
|
||||
import { CommandContext } from '../services/CommandService';
|
||||
import { defaultState } from '../reducer';
|
||||
|
||||
const command = runtime();
|
||||
|
||||
const makeContext = (): CommandContext => {
|
||||
return {
|
||||
state: defaultState,
|
||||
dispatch: ()=>{},
|
||||
};
|
||||
};
|
||||
|
||||
describe('convertHtmlToMarkdown', () => {
|
||||
|
||||
it.each([
|
||||
['<b>test</b>', '**test**'],
|
||||
['<a href="https://joplin.org">Joplin</a>', '[Joplin](https://joplin.org)'],
|
||||
['<h1>Title</h1>\n<h2>Subtitle</h2>', '# Title\n\n## Subtitle'],
|
||||
['<ul><li>One</li><li>Two</li></ul>', '- One\n- Two'],
|
||||
['<p>First paragraph</p><p>This is the second paragraph</p>', 'First paragraph\n\nThis is the second paragraph'],
|
||||
['<p>A paragraph with <strong>bold</strong> and <em>italic</em></p>', 'A paragraph with **bold** and *italic*'],
|
||||
])('should turn HTML into Markdown', async (html, markdown) => {
|
||||
const context = makeContext();
|
||||
const result: string = await command.execute(context, html);
|
||||
|
||||
expect(result).toBe(markdown);
|
||||
});
|
||||
|
||||
});
|
||||
22
packages/lib/commands/convertHtmlToMarkdown.ts
Normal file
22
packages/lib/commands/convertHtmlToMarkdown.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import HtmlToMd from '../HtmlToMd';
|
||||
import { CommandContext, CommandRuntime, CommandDeclaration } from '../services/CommandService';
|
||||
|
||||
export const declaration: CommandDeclaration = {
|
||||
name: 'convertHtmlToMarkdown',
|
||||
};
|
||||
|
||||
export const runtime = (): CommandRuntime => {
|
||||
return {
|
||||
execute: async (_context: CommandContext, html: string) => {
|
||||
const htmlToMdParser = new HtmlToMd();
|
||||
|
||||
const markdown = await htmlToMdParser.parse(`<div>${html}</div>`, {
|
||||
baseUrl: '',
|
||||
anchorNames: [],
|
||||
convertEmbeddedPdfsToLinks: true,
|
||||
});
|
||||
|
||||
return markdown;
|
||||
},
|
||||
};
|
||||
};
|
||||
@@ -1,4 +1,5 @@
|
||||
// AUTO-GENERATED using `gulp buildScriptIndexes`
|
||||
import * as convertHtmlToMarkdown from './convertHtmlToMarkdown';
|
||||
import * as deleteNote from './deleteNote';
|
||||
import * as historyBackward from './historyBackward';
|
||||
import * as historyForward from './historyForward';
|
||||
@@ -12,6 +13,7 @@ import * as toggleAllFolders from './toggleAllFolders';
|
||||
import * as toggleEditorPlugin from './toggleEditorPlugin';
|
||||
|
||||
const index: any[] = [
|
||||
convertHtmlToMarkdown,
|
||||
deleteNote,
|
||||
historyBackward,
|
||||
historyForward,
|
||||
|
||||
Reference in New Issue
Block a user