Skip to content

Commit 20b9fd2

Browse files
committed
fix: footernote anchor
Signed-off-by: Innei <[email protected]>
1 parent c50d283 commit 20b9fd2

File tree

6 files changed

+56
-52
lines changed

6 files changed

+56
-52
lines changed

src/components/ui/markdown/Markdown.tsx

+27-38
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import { MFootNote } from './renderers/footnotes'
3737
import { MHeader } from './renderers/heading'
3838
import { MarkdownImage } from './renderers/image'
3939
import { MTag } from './renderers/tag'
40+
import { getFootNoteDomId } from './utils/get-id'
4041
import { redHighlight } from './utils/redHighlight'
4142

4243
const CodeBlock = dynamic(() => import('~/components/widgets/shared/CodeBlock'))
@@ -167,46 +168,34 @@ export const Markdown: FC<MdProps & MarkdownToJSX.Options & PropsWithChildren> =
167168
return undefined
168169
}
169170
})()
170-
const footnotes = () => {
171-
return (
172-
<Fragment key={state?.key}>
173-
<a
174-
href={`#fn:${content}`}
175-
onClick={(e) => {
176-
e.preventDefault()
177-
springScrollToElement(
178-
document.getElementById(`fn:${content}`)!,
179-
-window.innerHeight / 2,
180-
)
181-
redHighlight(`fn:${content}`)
182-
}}
183-
>
184-
<sup id={`fnref:${content}`}>{`[^${content}]`}</sup>
185-
</a>
186-
{linkCardId && (
187-
<LinkCard id={linkCardId} source="mx-space" />
188-
)}
189-
</Fragment>
190-
)
191-
}
171+
192172
return (
193-
<FloatPopover
194-
as="span"
195-
TriggerComponent={footnotes}
196-
type="popover"
197-
wrapperClassName="footnotes_text"
198-
>
199-
<div className="space-y-2 leading-relaxed">
200-
<p className="flex items-center space-x-1 opacity-80">
201-
<span
202-
className="font-medium"
203-
dangerouslySetInnerHTML={{
204-
__html: footnote?.footnote?.substring(1),
173+
<Fragment key={state?.key}>
174+
<FloatPopover
175+
wrapperClassName="inline"
176+
as="span"
177+
TriggerComponent={() => (
178+
<a
179+
href={`#fn:${content}`}
180+
onClick={(e) => {
181+
e.preventDefault()
182+
const id = getFootNoteDomId(content)
183+
springScrollToElement(
184+
document.getElementById(id)!,
185+
-window.innerHeight / 2,
186+
)
187+
redHighlight(id)
205188
}}
206-
/>
207-
</p>
208-
</div>
209-
</FloatPopover>
189+
>
190+
<sup id={`fnref:${content}`}>{`[^${content}]`}</sup>
191+
</a>
192+
)}
193+
type="tooltip"
194+
>
195+
{footnote?.footnote?.substring(1)}
196+
</FloatPopover>
197+
{linkCardId && <LinkCard id={linkCardId} source="mx-space" />}
198+
</Fragment>
210199
)
211200
},
212201
},

src/components/ui/markdown/markdown.module.css

+2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@
8888

8989
input[type='checkbox'] {
9090
@apply checkbox checkbox-xs my-0 mr-2 align-text-bottom;
91+
92+
vertical-align: inherit;
9193
}
9294

9395
input[type='checkbox']:disabled,

src/components/ui/markdown/renderers/footnotes.tsx

+8-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { FC, PropsWithChildren } from 'react'
44
import { springScrollToElement } from '~/lib/scroller'
55

66
import { Divider } from '../../divider'
7+
import { getFootNoteDomId, getFootNoteRefDomId } from '../utils/get-id'
78
import { redHighlight } from '../utils/redHighlight'
89

910
export const MFootNote: FC<PropsWithChildren> = (props) => {
@@ -13,20 +14,22 @@ export const MFootNote: FC<PropsWithChildren> = (props) => {
1314
{React.Children.map(props.children, (child, index) => {
1415
if (React.isValidElement(child)) {
1516
return (
16-
<div id={`fn:${index + 1}`}>
17+
<div id={`${getFootNoteDomId(index + 1)}`}>
1718
<p className="inline">
1819
{React.cloneElement(child as React.ReactElement<any>, {
19-
style: { display: 'inline' }, // 设置 child 的 display 样式
20+
className: 'inline',
2021
})}
2122
<a
22-
href={`#fnref:${index + 1}`}
23+
href={`#${getFootNoteRefDomId(index + 1)}`}
2324
onClick={(e) => {
2425
e.preventDefault()
2526
springScrollToElement(
26-
document.getElementById(`fnref:${index + 1}`)!,
27+
document.getElementById(
28+
`${getFootNoteRefDomId(index + 1)}`,
29+
)!,
2730
-window.innerHeight / 2,
2831
)
29-
redHighlight(`fnref:${index + 1}`)
32+
redHighlight(`${getFootNoteRefDomId(index + 1)}`)
3033
}}
3134
className="inline"
3235
>

src/components/ui/markdown/renderers/paragraph.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const MParagraph: FC<
2727
)
2828
}
2929
}
30-
// console.log(children)
30+
3131
return (
3232
<p className={clsx('paragraph', className)} {...rest}>
3333
{children}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const getFootNoteRefDomId = (id: string | number) => `footnote-ref-${id}`
2+
3+
export const getFootNoteDomId = (id: string | number) => `footnote-${id}`

src/hooks/shared/use-mask-scrollarea.ts

+15-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { useEventCallback } from '../common/use-event-callback'
88
export const useMaskScrollArea = <T extends HTMLElement = HTMLElement>() => {
99
const containerRef = useRef<T>(null)
1010
const [isScrollToBottom, setIsScrollToBottom] = useState(false)
11-
const [isScrollToTop, setIsScrollToTop] = useState(true)
11+
const [isScrollToTop, setIsScrollToTop] = useState(false)
1212
const [canScroll, setCanScroll] = useState(false)
1313
const h = useViewport((v) => v.h)
1414

@@ -18,7 +18,12 @@ export const useMaskScrollArea = <T extends HTMLElement = HTMLElement>() => {
1818
if (!$) return
1919

2020
// if $ can not scroll, return null
21-
if ($.scrollHeight <= $.clientHeight + 2) return
21+
if ($.scrollHeight <= $.clientHeight + 2) {
22+
setCanScroll(false)
23+
setIsScrollToBottom(false)
24+
setIsScrollToTop(false)
25+
return
26+
}
2227

2328
setCanScroll(true)
2429

@@ -56,10 +61,12 @@ export const useMaskScrollArea = <T extends HTMLElement = HTMLElement>() => {
5661

5762
return [
5863
containerRef,
59-
clsx(
60-
isScrollToBottom && 'mask-t',
61-
isScrollToTop && 'mask-b',
62-
canScroll && !isScrollToBottom && !isScrollToTop && 'mask-both',
63-
),
64-
]
64+
canScroll
65+
? clsx(
66+
isScrollToBottom && 'mask-t',
67+
isScrollToTop && 'mask-b',
68+
!isScrollToBottom && !isScrollToTop && 'mask-both',
69+
)
70+
: '',
71+
] as const
6572
}

0 commit comments

Comments
 (0)