diff --git a/src/components/ui/markdown/Markdown.tsx b/src/components/ui/markdown/Markdown.tsx index 0b0876acf3..af8a1b882a 100644 --- a/src/components/ui/markdown/Markdown.tsx +++ b/src/components/ui/markdown/Markdown.tsx @@ -26,6 +26,7 @@ import { SpoilderRule } from './parsers/spoiler' import { MParagraph, MTableBody, MTableHead, MTableRow } from './renderers' import { MDetails } from './renderers/collapse' import { MFootNote } from './renderers/footnotes' +import { MHeader } from './renderers/heading' import { MLink } from './renderers/link' const CodeBlock = dynamic(() => import('~/components/widgets/shared/CodeBlock')) @@ -90,6 +91,15 @@ export const Markdown: FC = }, extendsRules: { + heading: { + react(node, output, state) { + return ( + + {output(node.content, state!)} + + ) + }, + }, gfmTask: { react(node, _, state) { return ( diff --git a/src/components/ui/markdown/renderers/heading.tsx b/src/components/ui/markdown/renderers/heading.tsx new file mode 100644 index 0000000000..e2247a5a8b --- /dev/null +++ b/src/components/ui/markdown/renderers/heading.tsx @@ -0,0 +1,43 @@ +import { createElement } from 'react' +import type { DOMAttributes } from 'react' + +import { springScrollToElement } from '~/lib/scroller' + +interface HeadingProps { + id: string + className?: string + children: React.ReactNode + level: number +} + +export const MHeader = (props: HeadingProps) => { + const { children, id, level } = props + + return createElement, HTMLHeadingElement>( + `h${level}`, + { + id, + className: 'group flex items-center', + } as any, + null, + <> + {children} + { + const state = history.state + history.replaceState(state, '', `#${id}`) + springScrollToElement( + document.getElementById(id)!, + -window.innerHeight / 2, + ) + }} + > + + + , + ) +} diff --git a/src/components/widgets/toc/TocAside.tsx b/src/components/widgets/toc/TocAside.tsx index ef5d2e5d6a..658af2a765 100644 --- a/src/components/widgets/toc/TocAside.tsx +++ b/src/components/widgets/toc/TocAside.tsx @@ -26,7 +26,6 @@ export const TocAside: Component = ({ }) => { const containerRef = useRef(null) const $article = useWrappedElement() - // const { h } = useWrappedElementSize() if (typeof $article === 'undefined') { throw new Error(' must be used in ') diff --git a/src/components/widgets/toc/TocAutoScroll.tsx b/src/components/widgets/toc/TocAutoScroll.tsx index 9f84ec59fc..28e2ba299a 100644 --- a/src/components/widgets/toc/TocAutoScroll.tsx +++ b/src/components/widgets/toc/TocAutoScroll.tsx @@ -2,8 +2,6 @@ import { useEffect } from 'react' -import { usePageScrollLocationSelector } from '~/providers/root/page-scroll-info-provider' - import { escapeSelector } from './escapeSelector' export const TocAutoScroll: Component = () => { @@ -20,13 +18,13 @@ export const TocAutoScroll: Component = () => { } }, []) - const isTop = usePageScrollLocationSelector((y) => y < 10) + // const isTop = usePageScrollLocationSelector((y) => y < 10) - useEffect(() => { - if (isTop) { - history.replaceState(history.state, '', `#`) - } - }, [isTop]) + // useEffect(() => { + // if (isTop) { + // history.replaceState(history.state, '', `#`) + // } + // }, [isTop]) return null } diff --git a/src/components/widgets/toc/TocItem.tsx b/src/components/widgets/toc/TocItem.tsx index b719009712..59922e2417 100644 --- a/src/components/widgets/toc/TocItem.tsx +++ b/src/components/widgets/toc/TocItem.tsx @@ -4,7 +4,6 @@ import { memo, useCallback, useEffect, useMemo, useRef } from 'react' import { tv } from 'tailwind-variants' import type { FC, MouseEvent } from 'react' -import { getIsInteractive } from '~/atoms/is-interactive' import { clsxm } from '~/lib/helper' const styles = tv({ @@ -39,14 +38,14 @@ export const TocItem: FC<{ const $ref = useRef(null) - useEffect(() => { - if (!active) { - return - } - if (!getIsInteractive()) return - const state = history.state - history.replaceState(state, '', `#${anchorId}`) - }, [active, anchorId]) + // useEffect(() => { + // if (!active) { + // return + // } + // if (!getIsInteractive()) return + // const state = history.state + // history.replaceState(state, '', `#${anchorId}`) + // }, [active, anchorId]) useEffect(() => { if (active) { @@ -92,3 +91,5 @@ export const TocItem: FC<{ ) }) + +TocItem.displayName = 'TocItem'