From 6957e011439eb2d3cbf42bfb67ed81b07d4bcc2a Mon Sep 17 00:00:00 2001 From: TNXG Date: Sat, 28 Oct 2023 14:09:08 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20Optimize=20Footnote=20Displ?= =?UTF-8?q?ay?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/ui/markdown/Markdown.tsx | 12 +++-- .../ui/markdown/renderers/footnotes.tsx | 44 ++++++++++++++++++- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/src/components/ui/markdown/Markdown.tsx b/src/components/ui/markdown/Markdown.tsx index 1a161aa456..7b922399ae 100644 --- a/src/components/ui/markdown/Markdown.tsx +++ b/src/components/ui/markdown/Markdown.tsx @@ -32,7 +32,7 @@ import { MTableRow, } from './renderers' import { MDetails } from './renderers/collapse' -import { MFootNote } from './renderers/footnotes' +import { MFootNote, red_highlight } from './renderers/footnotes' import { MHeader } from './renderers/heading' import { MarkdownImage } from './renderers/image' import { MTag } from './renderers/tag' @@ -156,7 +156,6 @@ export const Markdown: FC = const thisUrl = new URL(footnote?.footnote?.replace(': ', '')) const isCurrentHost = thisUrl.hostname === window.location.hostname - if (!isCurrentHost && !isDev) { return undefined } @@ -170,18 +169,17 @@ export const Markdown: FC = return ( { e.preventDefault() - springScrollToElement( - document.getElementById(content)!, - + document.getElementById(`fn:${content}`)!, -window.innerHeight / 2, ) + red_highlight(`fn:${content}`) }} > - ^{content} + {`[^${content}]`} {linkCardId && } diff --git a/src/components/ui/markdown/renderers/footnotes.tsx b/src/components/ui/markdown/renderers/footnotes.tsx index bdade4c394..a43a5c3cab 100644 --- a/src/components/ui/markdown/renderers/footnotes.tsx +++ b/src/components/ui/markdown/renderers/footnotes.tsx @@ -1,13 +1,55 @@ import React from 'react' import type { FC, PropsWithChildren } from 'react' +import { springScrollToElement } from '~/lib/scroller' + import { Divider } from '../../divider' export const MFootNote: FC = (props) => { return (
- {props.children} + {React.Children.map(props.children, (child, index) => { + if (React.isValidElement(child)) { + return ( + + ) + } else { + return null // 或者其他处理方式 + } + })}
) } + +export function red_highlight(id: string) { + const fnRefElement = document.getElementById(id) + if (fnRefElement) { + fnRefElement.style.color = 'red' + setTimeout(() => { + fnRefElement.style.color = '' + }, 5000) + } else { + console.log(`Element with id fnref:${id} not found.`) + } +} \ No newline at end of file