From c68823c4781a3b43dab333a9e6fea942a181ff14 Mon Sep 17 00:00:00 2001 From: Arik Chakma Date: Fri, 7 Jun 2024 00:01:05 +0600 Subject: [PATCH] feat: implement topic link's label (#5817) --- src/components/TopicDetail/TopicDetail.tsx | 40 +++++++++++++++---- .../frontend/content/100-internet/index.md | 14 +++---- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/src/components/TopicDetail/TopicDetail.tsx b/src/components/TopicDetail/TopicDetail.tsx index 267e24d72..e472d5911 100644 --- a/src/components/TopicDetail/TopicDetail.tsx +++ b/src/components/TopicDetail/TopicDetail.tsx @@ -42,10 +42,10 @@ type TopicDetailProps = { const linkTypes: Record = { article: 'bg-yellow-200', course: 'bg-green-200', - opensource: 'bg-blue-200', + opensource: 'bg-black-200 text-white', podcast: 'bg-purple-200', - video: 'bg-pink-200', - website: 'bg-red-200', + video: 'bg-pink-300', + website: 'bg-blue-300', }; export function TopicDetail(props: TopicDetailProps) { @@ -165,9 +165,8 @@ export function TopicDetail(props: TopicDetailProps) { } let topicHtml = ''; if (!isCustomResource) { - topicHtml = response as string; const topicDom = new DOMParser().parseFromString( - topicHtml, + response as string, 'text/html', ); @@ -180,6 +179,31 @@ export function TopicDetail(props: TopicDetailProps) { const otherElems = topicDom.querySelectorAll('body > *:not(h1, div)'); + const listLinks = Array.from( + topicDom.querySelectorAll('ul > li > a'), + ).map((link, counter) => { + const typePattern = /@([a-z]+)@/; + let linkText = link.textContent || ''; + const linkHref = link.getAttribute('href') || ''; + const linkType = linkText.match(typePattern)?.[1] || 'article'; + linkText = linkText.replace(typePattern, ''); + + return { + id: `link-${linkHref}-${counter}`, + title: linkText, + url: linkHref, + type: linkType as AllowedLinkTypes, + }; + }); + + const lastUl = topicDom.querySelector('ul:last-child'); + if (lastUl) { + lastUl.remove(); + } + + topicHtml = topicDom.body.innerHTML; + + setLinks(listLinks); setHasContent(otherElems.length > 0); setContributionUrl(contributionUrl); setHasEnoughLinks(links.length >= 3); @@ -322,7 +346,7 @@ export function TopicDetail(props: TopicDetailProps) {