From e852ca497301c7ecdcdb8c6f1a9272f7e499c011 Mon Sep 17 00:00:00 2001 From: Stuart Holland Date: Sun, 29 Sep 2024 21:49:06 +0100 Subject: [PATCH] Fixed link validity status of existing links not changing until next re-render for page creations, deletions, and moves. --- server/models/pages.js | 45 +++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/server/models/pages.js b/server/models/pages.js index bb5b658512..07f285e78e 100644 --- a/server/models/pages.js +++ b/server/models/pages.js @@ -849,23 +849,54 @@ module.exports = class Page extends Model { */ static async reconnectLinks (opts) { const pageHref = `/${opts.locale}/${opts.path}` + await this.reconnectLinksToSpecificHref({ + path: opts.path, + locale: opts.locale, + pageHref: pageHref, + sourceHref: typeof opts.sourcePath !== 'undefined' ? `/${opts.sourceLocale}/${opts.sourcePath}` : undefined, + mode: opts.mode + }) + + if (opts.locale === WIKI.config.lang.code) { + const pageHrefWithoutLangCode = `/${opts.path}` + await this.reconnectLinksToSpecificHref({ + path: opts.path, + locale: opts.locale, + pageHref: pageHrefWithoutLangCode, + sourceHref: typeof opts.sourcePath !== 'undefined' ? `/${opts.sourcePath}` : undefined, + mode: opts.mode + }) + } + } + + /** + * Reconnect links to new/move/deleted page + * + * @param {Object} opts - Page parameters + * @param {string} opts.path - Page Path + * @param {string} opts.locale - Page Locale Code + * @param {string} opts.pageHref - Page href (e.g. '/en/page', '/page') + * @param {string} [opts.sourceHref] - Previous Page Href (move only) + * @param {string} opts.mode - Page Update mode (create, move, delete) + * @returns {Promise} Promise with no value + */ + static async reconnectLinksToSpecificHref(opts) { let replaceArgs = { from: '', to: '' } switch (opts.mode) { case 'create': - replaceArgs.from = `` - replaceArgs.to = `` + replaceArgs.from = `` + replaceArgs.to = `` break case 'move': - const prevPageHref = `/${opts.sourceLocale}/${opts.sourcePath}` - replaceArgs.from = `` - replaceArgs.to = `` + replaceArgs.from = `` + replaceArgs.to = `` break case 'delete': - replaceArgs.from = `` - replaceArgs.to = `` + replaceArgs.from = `` + replaceArgs.to = `` break default: return false