diff --git a/server/services/backlinks.js b/server/services/backlinks.js index e6f4720f0..56547b9a3 100644 --- a/server/services/backlinks.js +++ b/server/services/backlinks.js @@ -44,6 +44,14 @@ export default class Backlinks { order: [["createdAt", "desc"]], limit: 2, }); + + // before parsing document text we must make sure it's been migrated to + // the latest version or the parser may fail on version differences + await currentRevision.migrateVersion(); + if (previousRevision) { + await previousRevision.migrateVersion(); + } + const previousLinkIds = previousRevision ? parseDocumentIds(previousRevision.text) : []; diff --git a/server/services/backlinks.test.js b/server/services/backlinks.test.js index b2c849da6..4335bb35e 100644 --- a/server/services/backlinks.test.js +++ b/server/services/backlinks.test.js @@ -32,6 +32,32 @@ describe("documents.update", () => { expect(backlinks.length).toBe(1); }); + test("should not fail when previous revision is different document version", async () => { + const otherDocument = await buildDocument(); + const document = await buildDocument({ + version: null, + text: `[ ] checklist item`, + }); + + document.text = `[this is a link](${otherDocument.url})`; + await document.save(); + + await Backlinks.on({ + name: "documents.update", + documentId: document.id, + collectionId: document.collectionId, + teamId: document.teamId, + actorId: document.createdById, + data: { autosave: false }, + }); + + const backlinks = await Backlink.findAll({ + where: { reverseDocumentId: document.id }, + }); + + expect(backlinks.length).toBe(1); + }); + test("should create new backlink records", async () => { const otherDocument = await buildDocument(); const document = await buildDocument();