From fcf4f580d3c100519146003e73997948eb31f154 Mon Sep 17 00:00:00 2001 From: Innei Date: Mon, 8 May 2023 18:46:52 +0800 Subject: [PATCH] fix: md anchor (#508) --- package.json | 2 -- pnpm-lock.yaml | 28 ------------------ src/components/common/PageContent.tsx | 19 ++++++++---- src/components/site/PostLink.tsx | 40 +++++++++++++------------ src/components/site/PostToc.tsx | 42 +++++++++++++++------------ src/components/site/SiteHeader.tsx | 1 + src/lib/utils.ts | 9 ++++++ src/markdown/index.ts | 9 ------ 8 files changed, 67 insertions(+), 83 deletions(-) diff --git a/package.json b/package.json index 5e304546..1b656b80 100644 --- a/package.json +++ b/package.json @@ -101,7 +101,6 @@ "react-i18next": "^12.2.2", "react-parallax-tilt": "^1.7.137", "react-popper": "^2.3.0", - "react-scroll": "1.8.9", "react-sortablejs": "6.1.4", "react-string-replace": "^1.1.0", "react-virtuoso": "4.3.5", @@ -160,7 +159,6 @@ "@types/react": "18.2.6", "@types/react-dom": "18.2.4", "@types/react-infinite-scroller": "1.2.3", - "@types/react-scroll": "1.8.7", "@types/remove-markdown": "0.3.1", "@types/serialize-javascript": "^5.0.2", "@types/sortablejs": "1.15.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f91a7bbf..16b43eee 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -223,9 +223,6 @@ dependencies: react-popper: specifier: ^2.3.0 version: 2.3.0(@popperjs/core@2.11.7)(react-dom@18.2.0)(react@18.2.0) - react-scroll: - specifier: 1.8.9 - version: 1.8.9(react-dom@18.2.0)(react@18.2.0) react-sortablejs: specifier: 6.1.4 version: 6.1.4(@types/sortablejs@1.15.1)(react-dom@18.2.0)(react@18.2.0)(sortablejs@1.15.0) @@ -396,9 +393,6 @@ devDependencies: '@types/react-infinite-scroller': specifier: 1.2.3 version: 1.2.3 - '@types/react-scroll': - specifier: 1.8.7 - version: 1.8.7 '@types/remove-markdown': specifier: 0.3.1 version: 0.3.1 @@ -4855,12 +4849,6 @@ packages: '@types/react': 18.2.6 dev: true - /@types/react-scroll@1.8.7: - resolution: {integrity: sha512-BB8g+hQL7OtBPWg/NcES6p5u6vduZonGl1BxrsGUwcefE53pfI0pFDd1lRFndgEUE6whYdFfhD+j0sZZT/6brQ==} - dependencies: - '@types/react': 18.2.6 - dev: true - /@types/react@18.2.6: resolution: {integrity: sha512-wRZClXn//zxCFW+ye/D2qY65UsYP1Fpex2YXorHc8awoNamkMZSvBxwxdYVInsHOZZd2Ppq8isnSzJL5Mpf8OA==} dependencies: @@ -10053,10 +10041,6 @@ packages: resolution: {integrity: sha512-HDWXG8isMntAyRF5vZ7xKuEvOhT4AhlRt/3czTSjvGUxjYCBVRQY48ViDHyfYz9VIoBkW4TMGQNapx+l3RUwdA==} dev: true - /lodash.throttle@4.1.1: - resolution: {integrity: sha512-wIkUCfVKpVsWo3JSZlc+8MB5it+2AN5W8J7YVMST30UrvcQNZ1Okbj+rbVniijTWE6FGYy4XJq/rHkas8qJMLQ==} - dev: false - /lodash.transform@4.6.0: resolution: {integrity: sha512-LO37ZnhmBVx0GvOU/caQuipEh4GN82TcWv3yHlebGDgOxbxiwwzW5Pcx2AcvpIv2WmvmSMoC492yQFNhy/l/UQ==} dev: false @@ -11872,18 +11856,6 @@ packages: use-sidecar: 1.1.2(@types/react@18.2.6)(react@18.2.0) dev: false - /react-scroll@1.8.9(react-dom@18.2.0)(react@18.2.0): - resolution: {integrity: sha512-9m7ztraiX/l6L7erzYAD3fhnveNckei6/NkWfqwN2e0FRdoE2W6Pk4oi2Nah7mWpPCPAeIgegfaqZACTimPOwg==} - peerDependencies: - react: ^15.5.4 || ^16.0.0 || ^17.0.0 || ^18.0.0 - react-dom: ^15.5.4 || ^16.0.0 || ^17.0.0 || ^18.0.0 - dependencies: - lodash.throttle: 4.1.1 - prop-types: 15.8.1 - react: 18.2.0 - react-dom: 18.2.0(react@18.2.0) - dev: false - /react-sortablejs@6.1.4(@types/sortablejs@1.15.1)(react-dom@18.2.0)(react@18.2.0)(sortablejs@1.15.0): resolution: {integrity: sha512-fc7cBosfhnbh53Mbm6a45W+F735jwZ1UFIYSrIqcO/gRIFoDyZeMtgKlpV4DdyQfbCzdh5LoALLTDRxhMpTyXQ==} peerDependencies: diff --git a/src/components/common/PageContent.tsx b/src/components/common/PageContent.tsx index 303f14b1..9e057898 100644 --- a/src/components/common/PageContent.tsx +++ b/src/components/common/PageContent.tsx @@ -1,11 +1,10 @@ "use client" import { MutableRefObject, useEffect, useMemo } from "react" -import { scroller } from "react-scroll" import { PostToc } from "~/components/site/PostToc" import { useCodeCopy } from "~/hooks/useCodeCopy" -import { cn } from "~/lib/utils" +import { calculateElementTop, cn } from "~/lib/utils" import { renderPageContent } from "~/markdown" export const PageContent: React.FC<{ @@ -44,10 +43,18 @@ export const PageContent: React.FC<{ const hashChangeHandler = () => { const hash = decodeURIComponent(location.hash.slice(1)) if (hash) { - scroller.scrollTo(`user-content-${decodeURIComponent(hash)}`, { - smooth: true, - offset: -20, - duration: 500, + if (history.state?.preventScrollToToc) { + history.state.preventScrollToToc = false + return + } + const targetElement = document.querySelector( + `#user-content-${decodeURIComponent(hash)}`, + ) as HTMLElement + if (!targetElement) return + + window.scrollTo({ + top: calculateElementTop(targetElement) - 20, + behavior: "smooth", }) } } diff --git a/src/components/site/PostLink.tsx b/src/components/site/PostLink.tsx index 0275bd90..21b86760 100644 --- a/src/components/site/PostLink.tsx +++ b/src/components/site/PostLink.tsx @@ -1,20 +1,22 @@ -import { Link } from "react-scroll" +// import { Link } from "react-scroll" -export const PostLink: React.FC = ({ children, ...props }) => { - if (props.href?.startsWith("#")) { - return ( - - {children} - - ) - } else { - return {children} - } -} +// export const PostLink: React.FC = ({ children, ...props }) => { +// if (props.href?.startsWith("#")) { +// return ( +// +// {children} +// +// ) +// } else { +// return {children} +// } +// } + +export {} diff --git a/src/components/site/PostToc.tsx b/src/components/site/PostToc.tsx index f176b5d8..20e32cef 100644 --- a/src/components/site/PostToc.tsx +++ b/src/components/site/PostToc.tsx @@ -4,8 +4,7 @@ import katex from "katex" import type { List } from "mdast" import { toHast } from "mdast-util-to-hast" import type { Result as TocResult } from "mdast-util-toc" -import React, { useEffect, useRef, useState } from "react" -import { Link } from "react-scroll" +import React, { createElement, useEffect, useRef, useState } from "react" const inlineElements = ["delete", "strong", "emphasis", "inlineCode"] @@ -40,6 +39,13 @@ function useActiveId(itemIds: string[]) { (entries) => { entries.forEach((entry) => { if (entry.isIntersecting) { + const state = history.state + + history.replaceState( + { ...state, preventScrollToToc: true }, + "", + entry.target.getAttribute("href"), + ) setActiveId(entry.target.getAttribute("href")) } }) @@ -63,12 +69,13 @@ function useActiveId(itemIds: string[]) { }, [itemIds]) return activeId } - -function renderItems( - items: TocResult["map"], - activeId?: string | null, - prefix = "", -) { +interface ItemsProps { + items: TocResult["map"] + activeId?: string | null + prefix?: string +} +function Items(props: ItemsProps) { + const { items, activeId, prefix = "" } = props return (
    {items?.children?.map((item, index) => ( @@ -91,14 +98,7 @@ function renderItems( return ( {child.type === "paragraph" && child.children?.[0]?.url && ( - - + )} {child.type === "list" && - renderItems(child, activeId, `${index + 1}.`)} + createElement(Items, { + items: child, + activeId, + prefix: `${index + 1}.`, + })} ) })} @@ -161,7 +165,7 @@ export const PostToc: React.FC<{ overflowY: "auto", }} > - {renderItems(data?.map, activeId)} + ) diff --git a/src/components/site/SiteHeader.tsx b/src/components/site/SiteHeader.tsx index 937bc40e..aa93446e 100644 --- a/src/components/site/SiteHeader.tsx +++ b/src/components/site/SiteHeader.tsx @@ -41,6 +41,7 @@ const fac = new FastAverageColor() const HeaderLink: React.FC<{ link: HeaderLinkType }> = ({ link }) => { const pathname = usePathname() const { t } = useTranslation("site") + const active = pathname === link.url // TODO return ( { } } } + +export const calculateElementTop = (el: HTMLElement) => { + let top = 0 + while (el) { + top += el.offsetTop + el = el.offsetParent as HTMLElement + } + return top +} diff --git a/src/markdown/index.ts b/src/markdown/index.ts index 4271bdfe..f941905d 100644 --- a/src/markdown/index.ts +++ b/src/markdown/index.ts @@ -155,15 +155,6 @@ export const renderPageContent = ( }, children: [], }, - // { - // type: "element", - // tagName: "anchor", - // properties: { - // name: node.properties?.id, - // }, - // children: [], - // }, - // TODO ] }, })