From 46e4ffc9d8f3ebb11e1fb2efb1e35e5cce0970f5 Mon Sep 17 00:00:00 2001 From: nyne Date: Thu, 29 May 2025 18:44:13 +0800 Subject: [PATCH] Improve article rendering --- frontend/src/markdown.css | 5 +- frontend/src/pages/resource_details_page.tsx | 108 +++++++++++-------- 2 files changed, 66 insertions(+), 47 deletions(-) diff --git a/frontend/src/markdown.css b/frontend/src/markdown.css index 2effb05..2b67a9e 100644 --- a/frontend/src/markdown.css +++ b/frontend/src/markdown.css @@ -1,6 +1,6 @@ article { & { - color: var(--color-base-content) + color: var(--color-base-content); } h1 { @@ -92,9 +92,6 @@ article { border-radius: 4px; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, system-ui; } - .line-break { - white-space: pre-wrap; - } iframe{ width: 100%; height: 100%; diff --git a/frontend/src/pages/resource_details_page.tsx b/frontend/src/pages/resource_details_page.tsx index 0c93ec4..c9e4805 100644 --- a/frontend/src/pages/resource_details_page.tsx +++ b/frontend/src/pages/resource_details_page.tsx @@ -238,7 +238,8 @@ function Article({resource}: { resource: ResourceDetails }) { return
{ + p: ({node, ...props}) => { + console.log(props.children) if (typeof props.children === "object" && (props.children as ReactElement).type === "strong") { // @ts-ignore const child = (props.children as ReactElement).props.children.toString() as string @@ -250,46 +251,17 @@ function Article({resource}: { resource: ResourceDetails }) { return !(s.startsWith("width") || s.startsWith("height") || s.startsWith("class") || s.startsWith("style")); }) html = splits.join(" ") - return
+ return
} - } else if (typeof props.children === "object" && (props.children as ReactElement).type === "a") { + // @ts-ignore + } else if (typeof props.children === "object" && props.children.props && props.children.props.href) { const a = props.children as ReactElement const childProps = a.props as any const href = childProps.href as string - if (href.startsWith(window.location.origin) || href.startsWith("/")) { - let path = href - if (path.startsWith(window.location.origin)) { - path = path.substring(window.location.origin.length) - } - const content = childProps.children?.toString() - if (path.startsWith("/resources/")) { - const id = path.substring("/resources/".length) - for (const r of resource.related) { - if (r.id.toString() === id) { - return
{ - navigate(`/resources/${r.id}`) - }}> - {r.image ?
- Cover -
: null} -
-

{r.title}

-

{content}

-
-
- } - } - } - } // @ts-ignore if (childProps.children?.length === 2) { // @ts-ignore @@ -304,12 +276,12 @@ function Article({resource}: { resource: ResourceDetails }) { return -
- {img} -
+
+ {img} +
- {second} -
+ {second} +
} } @@ -328,6 +300,55 @@ function Article({resource}: { resource: ResourceDetails }) { } return

{props.children}

}, + a: ({node, ...props}) => { + const href = props.href as string + + if (href.startsWith(window.location.origin) || href.startsWith("/")) { + let path = href + if (path.startsWith(window.location.origin)) { + path = path.substring(window.location.origin.length) + } + const content = props.children?.toString() + if (path.startsWith("/resources/")) { + const id = path.substring("/resources/".length) + for (const r of resource.related ?? []) { + if (r.id.toString() === id) { + return + { + navigate(`/resources/${r.id}`, {replace: true}) + }}> + {r.image && {"cover"}} + + {r.title} + + {content} + + + + } + } + } + } + + return + }, }}>{resource.article}
} @@ -399,7 +420,8 @@ function CloudflarePopup({file}: { file: RFile }) { window.open(link, "_blank"); }}> -

{t("Please check your network if the verification takes too long or the captcha does not appear.")}

+

{t("Please check your network if the verification takes too long or the captcha does not appear.")}

}