convert H1 to H2 also during import

This commit is contained in:
zadam 2020-11-01 20:38:39 +01:00
parent 8e8148ce42
commit 9e97fdcc49
3 changed files with 18 additions and 4 deletions

View File

@ -5,8 +5,7 @@ const sanitizeHtml = require('sanitize-html');
function sanitize(dirtyHtml) { function sanitize(dirtyHtml) {
return sanitizeHtml(dirtyHtml, { return sanitizeHtml(dirtyHtml, {
allowedTags: [ allowedTags: [
// h1 is removed since that should be note's title 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'blockquote', 'p', 'a', 'ul', 'ol',
'h2', 'h3', 'h4', 'h5', 'h6', 'blockquote', 'p', 'a', 'ul', 'ol',
'li', 'b', 'i', 'strong', 'em', 'strike', 'abbr', 'code', 'hr', 'br', 'div', 'li', 'b', 'i', 'strong', 'em', 'strike', 'abbr', 'code', 'hr', 'br', 'div',
'table', 'thead', 'caption', 'tbody', 'tr', 'th', 'td', 'pre', 'section', 'img', 'table', 'thead', 'caption', 'tbody', 'tr', 'th', 'td', 'pre', 'section', 'img',
'figure', 'span', 'label', 'input' 'figure', 'span', 'label', 'input'

View File

@ -117,6 +117,8 @@ function convertTextToHtml(text) {
} }
function importMarkdown(taskContext, file, parentNote) { function importMarkdown(taskContext, file, parentNote) {
const title = utils.getNoteTitle(file.originalname, taskContext.data.replaceUnderscoresWithSpaces);
const markdownContent = file.buffer.toString("UTF-8"); const markdownContent = file.buffer.toString("UTF-8");
const reader = new commonmark.Parser(); const reader = new commonmark.Parser();
@ -127,7 +129,7 @@ function importMarkdown(taskContext, file, parentNote) {
htmlContent = htmlSanitizer.sanitize(htmlContent); htmlContent = htmlSanitizer.sanitize(htmlContent);
const title = utils.getNoteTitle(file.originalname, taskContext.data.replaceUnderscoresWithSpaces); htmlContent = handleH1(htmlContent, title);
const {note} = noteService.createNewNote({ const {note} = noteService.createNewNote({
parentNoteId: parentNote.noteId, parentNoteId: parentNote.noteId,
@ -143,12 +145,25 @@ function importMarkdown(taskContext, file, parentNote) {
return note; return note;
} }
function handleH1(content, title) {
content = content.replace(/<h1>([^<]*)<\/h1>/gi, (match, text) => {
if (title.trim() === text.trim()) {
return ""; // remove whole H1 tag
} else {
return `<h2>${text}</h2>`;
}
});
return content;
}
function importHtml(taskContext, file, parentNote) { function importHtml(taskContext, file, parentNote) {
const title = utils.getNoteTitle(file.originalname, taskContext.data.replaceUnderscoresWithSpaces); const title = utils.getNoteTitle(file.originalname, taskContext.data.replaceUnderscoresWithSpaces);
let content = file.buffer.toString("UTF-8"); let content = file.buffer.toString("UTF-8");
content = htmlSanitizer.sanitize(content); content = htmlSanitizer.sanitize(content);
content = handleH1(content, title);
const {note} = noteService.createNewNote({ const {note} = noteService.createNewNote({
parentNoteId: parentNote.noteId, parentNoteId: parentNote.noteId,
title, title,

View File

@ -275,7 +275,7 @@ async function importZip(taskContext, fileBuffer, importRootNote) {
return ""; // remove whole H1 tag return ""; // remove whole H1 tag
} }
else { else {
return match; return `<h2>${text}</h2>`;
} }
}); });