Added option to convert HTML to Markdown instead of plain, using Turndown #1604

This commit is contained in:
the-djmaze 2024-09-14 14:00:36 +02:00
parent d3b0d6ca57
commit 8fb7067531
45 changed files with 201 additions and 135 deletions

View file

@ -4,6 +4,9 @@ import { SettingsUserStore } from 'Stores/User/Settings';
const
tmpl = createElement('template'),
turndown = new TurndownService(),
htmlre = /[&<>"']/g,
httpre = /^(https?:)?\/\//i,
htmlmap = {
@ -604,6 +607,9 @@ export const
* @returns {string}
*/
htmlToPlain = html => {
if (SettingsUserStore.markdown()) {
return htmlToMarkdown(html);
}
const
hr = '⎯'.repeat(64),
forEach = (selector, fn) => tmpl.content.querySelectorAll(selector).forEach(fn),
@ -640,6 +646,8 @@ export const
.replace(/<t[dh](\s[\s\S]*?)?>/gi, '\t')
.replace(/<\/tr(\s[\s\S]*?)?>/gi, '\n');
forEach('style', node => node.remove());
// lines
forEach('hr', node => node.replaceWith(`\n\n${hr}\n\n`));
@ -700,6 +708,11 @@ export const
return (tmpl.content.textContent || '').trim();
},
htmlToMarkdown = html => {
tmpl.innerHTML = html;
return turndown.turndown(tmpl.content);
},
/**
* @param {string} plain
* @param {boolean} findEmailAndLinksInText = false
@ -771,5 +784,7 @@ export const
rl.Utils = {
htmlToPlain: htmlToPlain,
plainToHtml: plainToHtml
plainToHtml: plainToHtml,
htmlToMarkdown: htmlToMarkdown
// markdownToHtml: md => marked.parse(md)
};

View file

@ -45,6 +45,7 @@ export const
proxy = url =>
BASE + '?/ProxyExternal/'
// + btoa(JSON.stringify([token,url]).replace(/ /g, '%20')).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, ''),
+ btoa(url.replace(/ /g, '%20')).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, ''),
// + b64EncodeJSONSafe(url.replace(/ /g, '%20')),

View file

@ -62,7 +62,7 @@ export class UserSettingsGeneral extends AbstractViewSettings {
'viewHTML', 'viewImages', 'viewImagesWhitelist', 'removeColors', 'allowStyles', 'allowDraftAutosave',
'hideDeleted', 'listInlineAttachments', 'simpleAttachmentsList', 'collapseBlockquotes',
'useCheckboxesInList', 'listGrouped', 'replySameFolder', 'allowSpellcheck',
'messageReadAuto', 'showNextMessage', 'messageNewWindow'
'messageReadAuto', 'showNextMessage', 'messageNewWindow', 'markdown'
].forEach(name => this[name] = SettingsUserStore[name]);
this.allowLanguagesOnSettings = !!SettingsGet('allowLanguagesOnSettings');
@ -123,7 +123,7 @@ export class UserSettingsGeneral extends AbstractViewSettings {
'ViewHTML', 'ViewImages', 'ViewImagesWhitelist', 'RemoveColors', 'AllowStyles', 'AllowDraftAutosave',
'HideDeleted', 'ListInlineAttachments', 'simpleAttachmentsList', 'CollapseBlockquotes',
'UseCheckboxesInList', 'listGrouped', 'ReplySameFolder', 'allowSpellcheck',
'messageReadAuto', 'showNextMessage', 'messageNewWindow',
'messageReadAuto', 'showNextMessage', 'messageNewWindow', 'markdown',
'DesktopNotifications', 'SoundNotification']);
const fReloadLanguageHelper = (saveSettingsStep) => () => {

View file

@ -50,6 +50,7 @@ export const SettingsUserStore = new class {
layout: 1,
editorDefaultType: 'Html',
editorWysiwyg: 'Squire',
markdown: 0,
msgDefaultAction: 1
});
@ -113,7 +114,8 @@ export const SettingsUserStore = new class {
'requireTLS',
'pgpSign',
'pgpEncrypt',
'allowSpellcheck'
'allowSpellcheck',
'markdown'
/*
'MessagesPerPage',
'MessageReadDelay',