fix absolute image paths to relative paths as part of parsing links

This commit is contained in:
azivner 2018-11-19 15:00:49 +01:00
parent 77a89d85c8
commit f20ab45576

View File

@ -177,7 +177,7 @@ async function protectNoteRevisions(note) {
} }
function findImageLinks(content, foundLinks) { function findImageLinks(content, foundLinks) {
const re = /src="\/api\/images\/([a-zA-Z0-9]+)\//g; const re = /src="[^"]*\/api\/images\/([a-zA-Z0-9]+)\//g;
let match; let match;
while (match = re.exec(content)) { while (match = re.exec(content)) {
@ -186,11 +186,13 @@ function findImageLinks(content, foundLinks) {
targetNoteId: match[1] targetNoteId: match[1]
}); });
} }
return match;
// removing absolute references to server to keep it working between instances
return content.replace(/src="[^"]*\/api\/images\//g, 'src="/api/images/');
} }
function findHyperLinks(content, foundLinks) { function findHyperLinks(content, foundLinks) {
const re = /href="#root[a-zA-Z0-9\/]*\/([a-zA-Z0-9]+)\/?"/g; const re = /href="[^"]*#root[a-zA-Z0-9\/]*\/([a-zA-Z0-9]+)\/?"/g;
let match; let match;
while (match = re.exec(content)) { while (match = re.exec(content)) {
@ -200,7 +202,8 @@ function findHyperLinks(content, foundLinks) {
}); });
} }
return match; // removing absolute references to server to keep it working between instances
return content.replace(/href="[^"]*#root/g, 'href="#root');
} }
function findRelationMapLinks(content, foundLinks) { function findRelationMapLinks(content, foundLinks) {
@ -214,7 +217,7 @@ function findRelationMapLinks(content, foundLinks) {
} }
} }
async function saveLinks(note) { async function saveLinks(note, content) {
if (note.type !== 'text' && note.type !== 'relation-map') { if (note.type !== 'text' && note.type !== 'relation-map') {
return; return;
} }
@ -222,11 +225,11 @@ async function saveLinks(note) {
const foundLinks = []; const foundLinks = [];
if (note.type === 'text') { if (note.type === 'text') {
findImageLinks(note.content, foundLinks); content = findImageLinks(content, foundLinks);
findHyperLinks(note.content, foundLinks); content = findHyperLinks(content, foundLinks);
} }
else if (note.type === 'relation-map') { else if (note.type === 'relation-map') {
findRelationMapLinks(note.content, foundLinks); findRelationMapLinks(content, foundLinks);
} }
else { else {
throw new Error("Unrecognized type " + note.type); throw new Error("Unrecognized type " + note.type);
@ -262,6 +265,8 @@ async function saveLinks(note) {
unusedLink.isDeleted = true; unusedLink.isDeleted = true;
await unusedLink.save(); await unusedLink.save();
} }
return content;
} }
async function saveNoteRevision(note) { async function saveNoteRevision(note) {
@ -310,6 +315,8 @@ async function updateNote(noteId, noteUpdates) {
const noteTitleChanged = note.title !== noteUpdates.title; const noteTitleChanged = note.title !== noteUpdates.title;
noteUpdates.content = await saveLinks(note, noteUpdates.content);
note.title = noteUpdates.title; note.title = noteUpdates.title;
note.setContent(noteUpdates.content); note.setContent(noteUpdates.content);
note.isProtected = noteUpdates.isProtected; note.isProtected = noteUpdates.isProtected;
@ -319,8 +326,6 @@ async function updateNote(noteId, noteUpdates) {
await triggerNoteTitleChanged(note); await triggerNoteTitleChanged(note);
} }
await saveLinks(note);
await protectNoteRevisions(note); await protectNoteRevisions(note);
} }