1
0
mirror of synced 2026-05-22 14:23:18 +00:00

FIX: Ensure that custom header links migration do not fail validation (#55)

This commit is a follow up to 73747938bd
where the migration will fail because the objects created by the
migration will end up failing to save because the objects will fail the
validation given the new schema. This commit updates the migration to
ensure that we do not end up with invalid objects.
This commit is contained in:
Alan Guo Xiang Tan
2024-04-26 12:57:46 +08:00
committed by GitHub
parent 73747938bd
commit 167bc8cee9
4 changed files with 228 additions and 37 deletions
@@ -1,32 +1,55 @@
export default function migrate(settings) {
const oldSetting = settings.get("custom_header_links");
// Do nothing if setting is already an array which means user has saved the setting in the new format
// This is required because there was a bug in core where this migration would fail but the theme was still updated
// allowing the users to save the setting in the new format.
if (Array.isArray(oldSetting)) {
return settings;
}
if (oldSetting) {
const newSetting = oldSetting.split("|").map((link) => {
const [text, title, url, view, target, hide_on_scroll, locale] = link
const newLinks = [];
oldSetting.split("|").forEach((link) => {
const [text, title, url, view, target, hideOnScroll, locale] = link
.split(",")
.map((s) => s.trim());
const newLink = {
text,
title,
url,
view,
target,
hide_on_scroll,
locale,
};
if (text && title && url) {
const newLink = {
text,
title,
url,
};
Object.keys(newLink).forEach((key) => {
if (newLink[key] === undefined) {
delete newLink[key];
if (["vdm", "vdo", "vmo"].includes(view)) {
newLink.view = view;
} else {
newLink.view = "vdm";
}
});
return newLink;
if (["blank", "self"].includes(target)) {
newLink.target = target;
} else {
newLink.target = "blank";
}
if (["remove", "keep"].includes(hideOnScroll)) {
newLink.hide_on_scroll = hideOnScroll;
} else {
newLink.hide_on_scroll = "keep";
}
if (locale) {
newLink.locale = locale;
}
newLinks.push(newLink);
}
});
settings.set("custom_header_links", newSetting);
settings.set("custom_header_links", newLinks);
}
return settings;