From bae7b330f833f3d46fb594d10799437cbc2545a7 Mon Sep 17 00:00:00 2001 From: SimonGschnell Date: Wed, 15 Jan 2025 13:34:54 +0100 Subject: [PATCH] feature(LegacyLinkReplacementHelper.js): adds a helper file with multiple functions that replace relative or absolute legacy links with new links that use the new app_root and ci_router --- .../Cis/Cms/Content_types/General.js | 15 ++-- public/js/helpers/LegacyLinkReplaceHelper.js | 71 +++++++++++++++++++ 2 files changed, 78 insertions(+), 8 deletions(-) create mode 100644 public/js/helpers/LegacyLinkReplaceHelper.js diff --git a/public/js/components/Cis/Cms/Content_types/General.js b/public/js/components/Cis/Cms/Content_types/General.js index 8a2c3dbd1..692843e3e 100644 --- a/public/js/components/Cis/Cms/Content_types/General.js +++ b/public/js/components/Cis/Cms/Content_types/General.js @@ -1,4 +1,4 @@ - +import { replaceRelativeLegacyLink } from "../../../../helpers/LegacyLinkReplaceHelper.js" export default { name: "GeneralComponent", props:{ @@ -99,13 +99,12 @@ export default { FHC_JS_DATA_STORAGE_OBJECT.app_root ); }); - document.querySelectorAll("[href]").forEach((el) => { - let original_href = el.getAttribute("href"); - // replaces the old relative paths with the new app_root path - if (original_href.match(/^\.\.\/index\.ci\.php/)){ - el.href = original_href.replace(/^\.\.\/index\.ci\.php\//,FHC_JS_DATA_STORAGE_OBJECT.app_root); - } - }); + + document.querySelectorAll("[href]").forEach((element) => { + let orignal_href = element.getAttribute("href"); + let new_href = replaceRelativeLegacyLink(orignal_href); + element.href = new_href; + }); }, template: /*html*/ ` diff --git a/public/js/helpers/LegacyLinkReplaceHelper.js b/public/js/helpers/LegacyLinkReplaceHelper.js new file mode 100644 index 000000000..ef0341269 --- /dev/null +++ b/public/js/helpers/LegacyLinkReplaceHelper.js @@ -0,0 +1,71 @@ +// collection of relative and absolute regex to replace legacy links +// +const regexList = { + relative:[ + { + priority: 1, + regex: new RegExp(/^\.\.\/cms\/content\.php\?content_id=([0-9]+)/), + replacement: FHC_JS_DATA_STORAGE_OBJECT.app_root + FHC_JS_DATA_STORAGE_OBJECT.ci_router + '/CisVue/Cms/content', + }, + { + priority: 2, + regex: new RegExp(/^\.\.\/cms\/news\.php/), + replacement: FHC_JS_DATA_STORAGE_OBJECT.app_root + FHC_JS_DATA_STORAGE_OBJECT.ci_router + '/CisVue/Cms/news', + }, + { + priority: 3, + regex: new RegExp(/^\.\.\/index\.ci\.php\//), + replacement: FHC_JS_DATA_STORAGE_OBJECT.app_root + FHC_JS_DATA_STORAGE_OBJECT.ci_router, + }, + { + priority: 10, + regex: new RegExp("/^\.\.\//"), + replacement: FHC_JS_DATA_STORAGE_OBJECT.app_root, + }, + ], + absolute:[ + {} + ] +}; + +// sorts the relative regex array by priority ascending +const relative_regex = regexList.relative + .sort((a, b) => { + return a.priority - b.priority; + }) + .map(regex => { + return { + regex: regex.regex, + replacement: regex.replacement, + } + }); + +// sorts the absolute regex array by priority ascending +const absolute_regex = regexList.absolute + .sort((a, b) => { + return a.priority - b.priority; + }) + .map(regex => { + return { + regex: regex.regex, + replacement: regex.replacement, + } + }) + + +export function replaceRelativeLegacyLink(relativeLegacyLink){ + for (let {regex,replacement} of relative_regex){ + // if any of the regex matches the relativeLegacyLink, replace the matched part with the new app_root path + let match = relativeLegacyLink.match(regex); + if (match) { + let new_link = relativeLegacyLink.replace(regex, replacement); + for(let query_parameter of match.slice(1)){ + new_link = new_link.concat(`/${query_parameter}`) + } + return new_link; + } + } + // if none of the regex matched with the string return the original path + return relativeLegacyLink; +} +