Skip to content

Commit e122241

Browse files
authored
feat: Optimize Footnote Display (#129)
1 parent 3c673a7 commit e122241

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

src/components/ui/markdown/Markdown.tsx

+5-7
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232
MTableRow,
3333
} from './renderers'
3434
import { MDetails } from './renderers/collapse'
35-
import { MFootNote } from './renderers/footnotes'
35+
import { MFootNote, red_highlight } from './renderers/footnotes'
3636
import { MHeader } from './renderers/heading'
3737
import { MarkdownImage } from './renderers/image'
3838
import { MTag } from './renderers/tag'
@@ -156,7 +156,6 @@ export const Markdown: FC<MdProps & MarkdownToJSX.Options & PropsWithChildren> =
156156
const thisUrl = new URL(footnote?.footnote?.replace(': ', ''))
157157
const isCurrentHost =
158158
thisUrl.hostname === window.location.hostname
159-
160159
if (!isCurrentHost && !isDev) {
161160
return undefined
162161
}
@@ -170,18 +169,17 @@ export const Markdown: FC<MdProps & MarkdownToJSX.Options & PropsWithChildren> =
170169
return (
171170
<Fragment key={state?.key}>
172171
<a
173-
href={sanitizeUrl(target)!}
172+
href={`#fn:${content}`}
174173
onClick={(e) => {
175174
e.preventDefault()
176-
177175
springScrollToElement(
178-
document.getElementById(content)!,
179-
176+
document.getElementById(`fn:${content}`)!,
180177
-window.innerHeight / 2,
181178
)
179+
red_highlight(`fn:${content}`)
182180
}}
183181
>
184-
<sup key={state?.key}>^{content}</sup>
182+
<sup id={`fnref:${content}`}>{`[^${content}]`}</sup>
185183
</a>
186184
{linkCardId && <LinkCard id={linkCardId} source="mx-space" />}
187185
</Fragment>
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,55 @@
11
import React from 'react'
22
import type { FC, PropsWithChildren } from 'react'
33

4+
import { springScrollToElement } from '~/lib/scroller'
5+
46
import { Divider } from '../../divider'
57

68
export const MFootNote: FC<PropsWithChildren> = (props) => {
79
return (
810
<div className="children:my-2 children:leading-6 children:text-base mt-4">
911
<Divider />
10-
{props.children}
12+
{React.Children.map(props.children, (child, index) => {
13+
if (React.isValidElement(child)) {
14+
return (
15+
<div id={`fn:${index + 1}`}>
16+
<p style={{ display: 'inline' }}>
17+
{React.cloneElement(child as React.ReactElement<any>, {
18+
style: { display: 'inline' }, // 设置child的display样式
19+
})}
20+
<a
21+
href={`#fnref:${index + 1}`}
22+
onClick={(e) => {
23+
e.preventDefault()
24+
springScrollToElement(
25+
document.getElementById(`fnref:${index + 1}`)!,
26+
-window.innerHeight / 2,
27+
)
28+
red_highlight(`fnref:${index + 1}`)
29+
}}
30+
style={{ display: 'inline' }}
31+
>
32+
33+
</a>
34+
</p>
35+
</div>
36+
)
37+
} else {
38+
return null // 或者其他处理方式
39+
}
40+
})}
1141
</div>
1242
)
1343
}
44+
45+
export function red_highlight(id: string) {
46+
const fnRefElement = document.getElementById(id)
47+
if (fnRefElement) {
48+
fnRefElement.style.color = 'red'
49+
setTimeout(() => {
50+
fnRefElement.style.color = ''
51+
}, 5000)
52+
} else {
53+
console.log(`Element with id fnref:${id} not found.`)
54+
}
55+
}

0 commit comments

Comments
 (0)