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

This commit is contained in:
SimonGschnell
2025-01-15 13:34:54 +01:00
parent cf57fe5086
commit bae7b330f8
2 changed files with 78 additions and 8 deletions
@@ -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*/ `
<!-- div that contains the content -->
@@ -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;
}