mirror of
https://github.com/zadam/trilium.git
synced 2025-11-18 03:00:41 +01:00
note content refactoring, WIP
This commit is contained in:
@@ -17,7 +17,7 @@ async function exportToOpml(branch, res) {
|
||||
const title = (branch.prefix ? (branch.prefix + ' - ') : '') + note.title;
|
||||
|
||||
const preparedTitle = prepareText(title);
|
||||
const preparedContent = prepareText(note.content);
|
||||
const preparedContent = prepareText(await note.getContent());
|
||||
|
||||
res.write(`<outline title="${preparedTitle}" text="${preparedContent}">\n`);
|
||||
|
||||
|
||||
@@ -18,30 +18,32 @@ async function exportSingleNote(branch, format, res) {
|
||||
|
||||
let payload, extension, mime;
|
||||
|
||||
const noteContent = await note.getNoteContent();
|
||||
|
||||
if (note.type === 'text') {
|
||||
if (format === 'html') {
|
||||
if (!note.content.toLowerCase().includes("<html")) {
|
||||
note.content = '<html><head><meta charset="utf-8"></head><body>' + note.content + '</body></html>';
|
||||
if (!noteContent.content.toLowerCase().includes("<html")) {
|
||||
noteContent.content = '<html><head><meta charset="utf-8"></head><body>' + noteContent.content + '</body></html>';
|
||||
}
|
||||
|
||||
payload = html.prettyPrint(note.content, {indent_size: 2});
|
||||
payload = html.prettyPrint(noteContent.content, {indent_size: 2});
|
||||
extension = 'html';
|
||||
mime = 'text/html';
|
||||
}
|
||||
else if (format === 'markdown') {
|
||||
const turndownService = new TurndownService();
|
||||
payload = turndownService.turndown(note.content);
|
||||
payload = turndownService.turndown(noteContent.content);
|
||||
extension = 'md';
|
||||
mime = 'text/markdown'
|
||||
}
|
||||
}
|
||||
else if (note.type === 'code') {
|
||||
payload = note.content;
|
||||
payload = noteContent.content;
|
||||
extension = mimeTypes.extension(note.mime) || 'code';
|
||||
mime = note.mime;
|
||||
}
|
||||
else if (note.type === 'relation-map' || note.type === 'search') {
|
||||
payload = note.content;
|
||||
payload = noteContent.content;
|
||||
extension = 'json';
|
||||
mime = 'application/json';
|
||||
}
|
||||
|
||||
@@ -123,7 +123,7 @@ async function exportToTar(branch, format, res) {
|
||||
const childBranches = await note.getChildBranches();
|
||||
|
||||
// if it's a leaf then we'll export it even if it's empty
|
||||
if (note.content.length > 0 || childBranches.length === 0) {
|
||||
if ((await note.getContent()).length > 0 || childBranches.length === 0) {
|
||||
meta.dataFileName = getDataFileName(note, baseFileName, existingFileNames);
|
||||
}
|
||||
|
||||
@@ -147,19 +147,21 @@ async function exportToTar(branch, format, res) {
|
||||
return meta;
|
||||
}
|
||||
|
||||
function prepareContent(note, format) {
|
||||
async function prepareContent(note, format) {
|
||||
const content = await note.getContent();
|
||||
|
||||
if (format === 'html') {
|
||||
if (!note.content.toLowerCase().includes("<html")) {
|
||||
note.content = '<html><head><meta charset="utf-8"></head><body>' + note.content + '</body></html>';
|
||||
if (!content.toLowerCase().includes("<html")) {
|
||||
note.content = '<html><head><meta charset="utf-8"></head><body>' + content + '</body></html>';
|
||||
}
|
||||
|
||||
return html.prettyPrint(note.content, {indent_size: 2});
|
||||
return html.prettyPrint(content, {indent_size: 2});
|
||||
}
|
||||
else if (format === 'markdown') {
|
||||
return turndownService.turndown(note.content);
|
||||
return turndownService.turndown(content);
|
||||
}
|
||||
else {
|
||||
return note.content;
|
||||
return content;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -179,7 +181,7 @@ async function exportToTar(branch, format, res) {
|
||||
notePaths[note.noteId] = path + (noteMeta.dataFileName || noteMeta.dirFileName);
|
||||
|
||||
if (noteMeta.dataFileName) {
|
||||
const content = prepareContent(note, noteMeta.format);
|
||||
const content = await prepareContent(note, noteMeta.format);
|
||||
|
||||
pack.entry({name: path + noteMeta.dataFileName, size: content.length}, content);
|
||||
}
|
||||
|
||||
@@ -218,6 +218,8 @@ async function importEnex(file, parentNote) {
|
||||
mime: 'text/html'
|
||||
})).note;
|
||||
|
||||
const noteContent = await noteEntity.getNoteContent();
|
||||
|
||||
for (const resource of resources) {
|
||||
const hash = utils.md5(resource.content);
|
||||
|
||||
@@ -238,8 +240,8 @@ async function importEnex(file, parentNote) {
|
||||
|
||||
const resourceLink = `<a href="#root/${resourceNote.noteId}">${utils.escapeHtml(resource.title)}</a>`;
|
||||
|
||||
noteEntity.content = noteEntity.content.replace(mediaRegex, resourceLink);
|
||||
}
|
||||
noteContent.content = noteContent.content.replace(mediaRegex, resourceLink);
|
||||
};
|
||||
|
||||
if (["image/jpeg", "image/png", "image/gif"].includes(resource.mime)) {
|
||||
try {
|
||||
@@ -249,12 +251,12 @@ async function importEnex(file, parentNote) {
|
||||
|
||||
const imageLink = `<img src="${url}">`;
|
||||
|
||||
noteEntity.content = noteEntity.content.replace(mediaRegex, imageLink);
|
||||
noteContent.content = noteContent.content.replace(mediaRegex, imageLink);
|
||||
|
||||
if (!note.content.includes(imageLink)) {
|
||||
if (!noteContent.content.includes(imageLink)) {
|
||||
// if there wasn't any match for the reference, we'll add the image anyway
|
||||
// otherwise image would be removed since no note would include it
|
||||
note.content += imageLink;
|
||||
noteContent.content += imageLink;
|
||||
}
|
||||
} catch (e) {
|
||||
log.error("error when saving image from ENEX file: " + e);
|
||||
@@ -267,7 +269,7 @@ async function importEnex(file, parentNote) {
|
||||
}
|
||||
|
||||
// save updated content with links to files/images
|
||||
await noteEntity.save();
|
||||
await noteContent.save();
|
||||
}
|
||||
|
||||
saxStream.on("closetag", async tag => {
|
||||
|
||||
@@ -245,8 +245,10 @@ async function importTar(fileBuffer, importRootNote) {
|
||||
let note = await repository.getNote(noteId);
|
||||
|
||||
if (note) {
|
||||
note.content = content;
|
||||
await note.save();
|
||||
const noteContent = await note.getNoteContent();
|
||||
|
||||
noteContent.content = content;
|
||||
await noteContent.save();
|
||||
}
|
||||
else {
|
||||
const noteTitle = getNoteTitle(filePath, noteMeta);
|
||||
|
||||
@@ -8,6 +8,7 @@ const eventService = require('./events');
|
||||
const repository = require('./repository');
|
||||
const cls = require('../services/cls');
|
||||
const Note = require('../entities/note');
|
||||
const NoteContent = require('../entities/note_content');
|
||||
const Link = require('../entities/link');
|
||||
const NoteRevision = require('../entities/note_revision');
|
||||
const Branch = require('../entities/branch');
|
||||
@@ -87,12 +88,16 @@ async function createNewNote(parentNoteId, noteData) {
|
||||
const note = await new Note({
|
||||
noteId: noteData.noteId, // optionally can force specific noteId
|
||||
title: noteData.title,
|
||||
content: noteData.content,
|
||||
isProtected: noteData.isProtected,
|
||||
type: noteData.type || 'text',
|
||||
mime: noteData.mime || 'text/html'
|
||||
}).save();
|
||||
|
||||
note.noteContent = await new NoteContent({
|
||||
noteId: note.noteId,
|
||||
content: noteData.content
|
||||
});
|
||||
|
||||
const branch = await new Branch({
|
||||
noteId: note.noteId,
|
||||
parentNoteId: parentNoteId,
|
||||
@@ -284,6 +289,12 @@ async function saveLinks(note, content) {
|
||||
}
|
||||
|
||||
async function saveNoteRevision(note) {
|
||||
// files and images are immutable, they can't be updated
|
||||
// but we don't even version titles which is probably not correct
|
||||
if (note.type !== 'file' || note.type !== 'image' || await note.hasLabel('disableVersioning')) {
|
||||
return;
|
||||
}
|
||||
|
||||
const now = new Date();
|
||||
const noteRevisionSnapshotTimeInterval = parseInt(await optionService.getOption('noteRevisionSnapshotTimeInterval'));
|
||||
|
||||
@@ -294,16 +305,12 @@ async function saveNoteRevision(note) {
|
||||
|
||||
const msSinceDateCreated = now.getTime() - dateUtils.parseDateTime(note.dateCreated).getTime();
|
||||
|
||||
if (note.type !== 'file'
|
||||
&& !await note.hasLabel('disableVersioning')
|
||||
&& !existingNoteRevisionId
|
||||
&& msSinceDateCreated >= noteRevisionSnapshotTimeInterval * 1000) {
|
||||
|
||||
if (!existingNoteRevisionId && msSinceDateCreated >= noteRevisionSnapshotTimeInterval * 1000) {
|
||||
await new NoteRevision({
|
||||
noteId: note.noteId,
|
||||
// title and text should be decrypted now
|
||||
title: note.title,
|
||||
content: note.content,
|
||||
content: note.noteContent.content,
|
||||
type: note.type,
|
||||
mime: note.mime,
|
||||
isProtected: false, // will be fixed in the protectNoteRevisions() call
|
||||
@@ -320,22 +327,23 @@ async function updateNote(noteId, noteUpdates) {
|
||||
throw new Error(`Note ${noteId} is not available for change!`);
|
||||
}
|
||||
|
||||
if (note.type === 'file' || note.type === 'image') {
|
||||
// files and images are immutable, they can't be updated
|
||||
noteUpdates.content = note.content;
|
||||
}
|
||||
|
||||
await saveNoteRevision(note);
|
||||
|
||||
const noteTitleChanged = note.title !== noteUpdates.title;
|
||||
|
||||
noteUpdates.content = await saveLinks(note, noteUpdates.content);
|
||||
noteUpdates.noteContent.content = await saveLinks(note, noteUpdates.noteContent.content);
|
||||
|
||||
note.title = noteUpdates.title;
|
||||
note.setContent(noteUpdates.content);
|
||||
note.isProtected = noteUpdates.isProtected;
|
||||
await note.save();
|
||||
|
||||
if (note.type !== 'file' && note.type !== 'image') {
|
||||
const noteContent = await note.getNoteContent();
|
||||
noteContent.content = noteUpdates.noteContent.content;
|
||||
noteContent.isProtected = noteUpdates.isProtected;
|
||||
await noteContent.save();
|
||||
}
|
||||
|
||||
if (noteTitleChanged) {
|
||||
await triggerNoteTitleChanged(note);
|
||||
}
|
||||
@@ -394,7 +402,7 @@ async function cleanupDeletedNotes() {
|
||||
// it's better to not use repository for this because it will complain about saving protected notes
|
||||
// out of protected session
|
||||
|
||||
await sql.execute("UPDATE notes SET content = NULL WHERE isDeleted = 1 AND content IS NOT NULL AND dateModified <= ?", [dateUtils.dateStr(cutoffDate)]);
|
||||
await sql.execute("UPDATE note_contents SET content = NULL WHERE content IS NOT NULL AND noteId IN (SELECT noteId FROM notes WHERE isDeleted = 1 AND notes.dateModified <= ?)", [dateUtils.dateStr(cutoffDate)]);
|
||||
|
||||
await sql.execute("UPDATE note_revisions SET content = NULL WHERE note_revisions.content IS NOT NULL AND noteId IN (SELECT noteId FROM notes WHERE isDeleted = 1 AND notes.dateModified <= ?)", [dateUtils.dateStr(cutoffDate)]);
|
||||
}
|
||||
|
||||
@@ -42,6 +42,14 @@ async function getNote(noteId) {
|
||||
return await getEntity("SELECT * FROM notes WHERE noteId = ?", [noteId]);
|
||||
}
|
||||
|
||||
/** @returns {Promise<Note|null>} */
|
||||
async function getNoteWithContent(noteId) {
|
||||
const note = await getEntity("SELECT * FROM notes WHERE noteId = ?", [noteId]);
|
||||
await note.getNoteContent();
|
||||
|
||||
return note;
|
||||
}
|
||||
|
||||
/** @returns {Promise<NoteContent|null>} */
|
||||
async function getNoteContent(noteContentId) {
|
||||
return await getEntity("SELECT * FROM note_contents WHERE noteContentId = ?", [noteContentId]);
|
||||
@@ -126,6 +134,7 @@ module.exports = {
|
||||
getEntities,
|
||||
getEntity,
|
||||
getNote,
|
||||
getNoteWithContent,
|
||||
getNoteContent,
|
||||
getBranch,
|
||||
getAttribute,
|
||||
|
||||
@@ -56,10 +56,10 @@ async function executeBundle(bundle, apiParams = {}) {
|
||||
*/
|
||||
async function executeScript(script, params, startNoteId, currentNoteId, originEntityName, originEntityId) {
|
||||
const startNote = await repository.getNote(startNoteId);
|
||||
const currentNote = await repository.getNote(currentNoteId);
|
||||
const currentNote = await repository.getNoteWithContent(currentNoteId);
|
||||
const originEntity = await repository.getEntityFromName(originEntityName, originEntityId);
|
||||
|
||||
currentNote.content = `return await (${script}\r\n)(${getParams(params)})`;
|
||||
currentNote.noteContent.content = `return await (${script}\r\n)(${getParams(params)})`;
|
||||
currentNote.type = 'code';
|
||||
currentNote.mime = 'application/javascript;env=backend';
|
||||
|
||||
@@ -158,7 +158,7 @@ apiContext.modules['${note.noteId}'] = {};
|
||||
${root ? 'return ' : ''}await ((async function(exports, module, require, api` + (modules.length > 0 ? ', ' : '') +
|
||||
modules.map(child => sanitizeVariableName(child.title)).join(', ') + `) {
|
||||
try {
|
||||
${note.content};
|
||||
${await note.getContent()};
|
||||
} catch (e) { throw new Error("Load of script note \\"${note.title}\\" (${note.noteId}) failed with: " + e.message); }
|
||||
if (!module.exports) module.exports = {};
|
||||
for (const exportKey in exports) module.exports[exportKey] = exports[exportKey];
|
||||
@@ -167,7 +167,7 @@ for (const exportKey in exports) module.exports[exportKey] = exports[exportKey];
|
||||
`;
|
||||
}
|
||||
else if (note.isHtml()) {
|
||||
bundle.html += note.content;
|
||||
bundle.html += await note.getContent();
|
||||
}
|
||||
|
||||
return bundle;
|
||||
|
||||
@@ -48,14 +48,16 @@ async function updateEntity(sync, entity, sourceId) {
|
||||
}
|
||||
}
|
||||
|
||||
function deserializeNoteContentBuffer(note) {
|
||||
if (note.content !== null && (note.type === 'file' || note.type === 'image')) {
|
||||
note.content = Buffer.from(note.content, 'base64');
|
||||
async function deserializeNoteContentBuffer(note) {
|
||||
const noteContent = await note.getNoteContent();
|
||||
|
||||
if (noteContent.content !== null && (note.type === 'file' || note.type === 'image')) {
|
||||
noteContent.content = Buffer.from(noteContent.content, 'base64');
|
||||
}
|
||||
}
|
||||
|
||||
async function updateNote(entity, sourceId) {
|
||||
deserializeNoteContentBuffer(entity);
|
||||
await deserializeNoteContentBuffer(entity);
|
||||
|
||||
const origNote = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [entity.noteId]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user