mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
export fixing links WIP
This commit is contained in:
parent
a81b00e130
commit
4be7ac7ae1
6
package-lock.json
generated
6
package-lock.json
generated
@ -3122,9 +3122,9 @@
|
||||
"integrity": "sha512-PcW2a0tyTuPHz3tWyYqtK6r1fZ3gp+3Sop8Ph+ZYN81Ob5rwmbHEzaqs10N3BEsaGTkh/ooniXK+WwszGlc2+Q=="
|
||||
},
|
||||
"electron": {
|
||||
"version": "6.0.6",
|
||||
"resolved": "https://registry.npmjs.org/electron/-/electron-6.0.6.tgz",
|
||||
"integrity": "sha512-sh5PfAHpKk+J/LpLK+Q2P2mCfTA7WDs0kImQ05uPuwmKjpCB3T8nMYCDuStS+opDoma19XPTh4049897GmPXmw==",
|
||||
"version": "6.0.7",
|
||||
"resolved": "https://registry.npmjs.org/electron/-/electron-6.0.7.tgz",
|
||||
"integrity": "sha512-W0TFnJrBdYBUhzRnEqZt/CfYFmG9RwSnhhXBbOumn/qLQYr9e7kXb6z4y0XQQLhXKkDhuXp+dNqfzhtId5ZiQw==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"@types/node": "^10.12.18",
|
||||
|
@ -77,7 +77,7 @@
|
||||
"xml2js": "0.4.19"
|
||||
},
|
||||
"devDependencies": {
|
||||
"electron": "6.0.6",
|
||||
"electron": "6.0.7",
|
||||
"electron-builder": "21.2.0",
|
||||
"electron-compile": "6.4.4",
|
||||
"electron-installer-debian": "2.0.0",
|
||||
|
@ -8,6 +8,7 @@ const mimeTypes = require('mime-types');
|
||||
const TurndownService = require('turndown');
|
||||
const packageInfo = require('../../../package.json');
|
||||
const utils = require('../utils');
|
||||
const log = require('../log');
|
||||
const sanitize = require("sanitize-filename");
|
||||
|
||||
/**
|
||||
@ -77,7 +78,7 @@ async function exportToTar(exportContext, branch, format, res) {
|
||||
return getUniqueFilename(existingFileNames, fileName);
|
||||
}
|
||||
|
||||
async function getNote(branch, existingFileNames) {
|
||||
async function getNoteMeta(branch, parentMeta, existingFileNames) {
|
||||
const note = await branch.getNote();
|
||||
|
||||
if (await note.hasLabel('excludeFromExport')) {
|
||||
@ -100,6 +101,7 @@ async function exportToTar(exportContext, branch, format, res) {
|
||||
const meta = {
|
||||
isClone: false,
|
||||
noteId: note.noteId,
|
||||
notePath: parentMeta.notePath.concat([note.noteId]),
|
||||
title: note.title,
|
||||
notePosition: branch.notePosition,
|
||||
prefix: branch.prefix,
|
||||
@ -141,7 +143,7 @@ async function exportToTar(exportContext, branch, format, res) {
|
||||
const childExistingNames = {};
|
||||
|
||||
for (const childBranch of childBranches) {
|
||||
const note = await getNote(childBranch, childExistingNames);
|
||||
const note = await getNoteMeta(childBranch, meta, childExistingNames);
|
||||
|
||||
// can be undefined if export is disabled for this note
|
||||
if (note) {
|
||||
@ -153,17 +155,70 @@ async function exportToTar(exportContext, branch, format, res) {
|
||||
return meta;
|
||||
}
|
||||
|
||||
async function prepareContent(note, format) {
|
||||
const content = await note.getContent();
|
||||
function findImageLinks(content, noteMeta) {
|
||||
try {
|
||||
return content.replace(/src="[^"]*api\/images\/([a-zA-Z0-9]+)\/[^"]*"/g, (_, targetNoteId) => {
|
||||
const targetNoteMeta = noteIdToMeta[targetNoteId];
|
||||
|
||||
if (format === 'html') {
|
||||
if (!targetNoteMeta) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const targetPath = targetNoteMeta.notePath.slice();
|
||||
const sourcePath = noteMeta.notePath.slice();
|
||||
|
||||
console.log("targetPath", targetPath);
|
||||
console.log("sourcePath", sourcePath);
|
||||
|
||||
// > 1 for edge case that targetPath and sourcePath are exact same (link to itself)
|
||||
while (targetPath.length > 1 && sourcePath.length > 1 && targetPath[0] === sourcePath[0]) {
|
||||
targetPath.shift();
|
||||
sourcePath.shift();
|
||||
}
|
||||
|
||||
console.log("targetPath", targetPath);
|
||||
console.log("sourcePath", sourcePath);
|
||||
|
||||
let url = "../".repeat(sourcePath.length - 1);
|
||||
|
||||
for (let i = 0; i < targetPath.length - 1; i++) {
|
||||
const meta = noteIdToMeta[targetPath[i]];
|
||||
|
||||
url += meta.dirFileName + '/';
|
||||
}
|
||||
|
||||
const meta = noteIdToMeta[targetPath[targetPath.length - 1]];
|
||||
|
||||
url += meta.dataFileName;
|
||||
|
||||
console.log("URL", url);
|
||||
|
||||
return url;
|
||||
});
|
||||
}
|
||||
catch (e) {
|
||||
log.error("Could not parse links from", content);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
async function prepareContent(note, noteMeta) {
|
||||
let content = await note.getContent();
|
||||
|
||||
if (['html', 'markdown'].includes(noteMeta.format)) {
|
||||
content = content.toString();
|
||||
|
||||
findImageLinks(content, noteMeta);
|
||||
}
|
||||
|
||||
if (noteMeta.format === 'html') {
|
||||
if (!content.toLowerCase().includes("<html")) {
|
||||
note.content = '<html><head><meta charset="utf-8"></head><body>' + content + '</body></html>';
|
||||
}
|
||||
|
||||
return html.prettyPrint(content, {indent_size: 2});
|
||||
}
|
||||
else if (format === 'markdown') {
|
||||
else if (noteMeta.format === 'markdown') {
|
||||
return turndownService.turndown(content);
|
||||
}
|
||||
else {
|
||||
@ -174,28 +229,28 @@ async function exportToTar(exportContext, branch, format, res) {
|
||||
// noteId => file path
|
||||
const notePaths = {};
|
||||
|
||||
async function saveNote(noteMeta, path) {
|
||||
async function saveNote(noteMeta, filePathPrefix) {
|
||||
if (noteMeta.isClone) {
|
||||
const content = "Note is present at " + notePaths[noteMeta.noteId];
|
||||
|
||||
pack.entry({name: path + noteMeta.dataFileName, size: content.length}, content);
|
||||
pack.entry({name: filePathPrefix + noteMeta.dataFileName, size: content.length}, content);
|
||||
return;
|
||||
}
|
||||
|
||||
const note = await repository.getNote(noteMeta.noteId);
|
||||
|
||||
notePaths[note.noteId] = path + (noteMeta.dataFileName || noteMeta.dirFileName);
|
||||
notePaths[note.noteId] = filePathPrefix + (noteMeta.dataFileName || noteMeta.dirFileName);
|
||||
|
||||
if (noteMeta.dataFileName) {
|
||||
const content = await prepareContent(note, noteMeta.format);
|
||||
const content = await prepareContent(note, noteMeta);
|
||||
|
||||
pack.entry({name: path + noteMeta.dataFileName, size: content.length}, content);
|
||||
pack.entry({name: filePathPrefix + noteMeta.dataFileName, size: content.length}, content);
|
||||
}
|
||||
|
||||
exportContext.increaseProgressCount();
|
||||
|
||||
if (noteMeta.children && noteMeta.children.length > 0) {
|
||||
const directoryPath = path + noteMeta.dirFileName;
|
||||
const directoryPath = filePathPrefix + noteMeta.dirFileName;
|
||||
|
||||
pack.entry({name: directoryPath, type: 'directory'});
|
||||
|
||||
@ -209,7 +264,7 @@ async function exportToTar(exportContext, branch, format, res) {
|
||||
formatVersion: 1,
|
||||
appVersion: packageInfo.version,
|
||||
files: [
|
||||
await getNote(branch, [])
|
||||
await getNoteMeta(branch, { notePath: [] }, [])
|
||||
]
|
||||
};
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user