From e11f18c0340eeec79a3a861a57bd2684795c1322 Mon Sep 17 00:00:00 2001 From: turbolnk Date: Tue, 25 Feb 2025 19:31:32 +0800 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor:=20Reduce=20code?= =?UTF-8?q?=20complexity=20and=20improve=20readability?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/user/utils/tutorial.ts | 41 ++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/apps/user/utils/tutorial.ts b/apps/user/utils/tutorial.ts index 999c4f1..9f5b76b 100644 --- a/apps/user/utils/tutorial.ts +++ b/apps/user/utils/tutorial.ts @@ -50,28 +50,27 @@ type TutorialItem = { subItems?: TutorialItem[]; }; +const processIcon = (item: TutorialItem) => { + if ("icon" in item && typeof item.icon === 'string' && !item.icon.startsWith('http')) { + item.icon = `${BASE_URL}/${item.icon}`; + } +}; + export async function getTutorialList() { - return await getTutorial('SUMMARY.md').then(({ config, content }) => { - const navigation = config as Record | undefined; - let map = new Map(); - if (navigation) { - for (const value of Object.values(navigation)) { - for (const item of value) { - if (item.subItems) { - for (const subItem of item.subItems) { - if ("icon" in subItem && typeof subItem.icon === 'string' && !subItem.icon.startsWith('http')) { - subItem.icon = `${BASE_URL}/${subItem.icon}`; - } - } - } - } - } - map = new Map(Object.entries(navigation)); - } else { - map = parseTutorialToMap(content); - } - return map; - }); + const { config, content } = await getTutorial('SUMMARY.md'); + const navigation = config as Record | undefined; + + if (!navigation) { + return parseTutorialToMap(content); + } + + Object.values(navigation) + .flat() + .forEach(item => { + item.subItems?.forEach(processIcon); + }); + + return new Map(Object.entries(navigation)); } function parseTutorialToMap(markdown: string): Map {