fix: md anchor (#508)

This commit is contained in:
Innei
2023-05-08 11:46:52 +01:00
committed by GitHub
parent 510e7fd5b1
commit fcf4f580d3
8 changed files with 67 additions and 83 deletions
-2
View File
@@ -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",
-28
View File
@@ -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:
+13 -6
View File
@@ -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",
})
}
}
+21 -19
View File
@@ -1,20 +1,22 @@
import { Link } from "react-scroll"
// import { Link } from "react-scroll"
export const PostLink: React.FC<any> = ({ children, ...props }) => {
if (props.href?.startsWith("#")) {
return (
<Link
to={decodeURI(props.href.slice(1))}
spy={true}
smooth={true}
duration={500}
offset={-20}
{...props}
>
{children}
</Link>
)
} else {
return <a {...props}>{children}</a>
}
}
// export const PostLink: React.FC<any> = ({ children, ...props }) => {
// if (props.href?.startsWith("#")) {
// return (
// <Link
// to={decodeURI(props.href.slice(1))}
// spy={true}
// smooth={true}
// duration={500}
// offset={-20}
// {...props}
// >
// {children}
// </Link>
// )
// } else {
// return <a {...props}>{children}</a>
// }
// }
export {}
+23 -19
View File
@@ -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 (
<ol className={prefix ? "pl-5" : ""}>
{items?.children?.map((item, index) => (
@@ -91,14 +98,7 @@ function renderItems(
return (
<span key={index + "-" + i}>
{child.type === "paragraph" && child.children?.[0]?.url && (
<Link
to={`user-content-${decodeURIComponent(
child.children[0].url.slice(1),
)}`}
spy={true}
smooth={true}
duration={500}
offset={-20}
<a
href={child.children[0].url}
title={content}
className={
@@ -113,10 +113,14 @@ function renderItems(
__html: content,
}}
/>
</Link>
</a>
)}
{child.type === "list" &&
renderItems(child, activeId, `${index + 1}.`)}
createElement(Items, {
items: child,
activeId,
prefix: `${index + 1}.`,
})}
</span>
)
})}
@@ -161,7 +165,7 @@ export const PostToc: React.FC<{
overflowY: "auto",
}}
>
{renderItems(data?.map, activeId)}
<Items items={data?.map} activeId={activeId} />
</div>
</div>
)
+1
View File
@@ -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 (
<UniLink
+9
View File
@@ -39,3 +39,12 @@ export const throttle = (func: Function, limit: number) => {
}
}
}
export const calculateElementTop = (el: HTMLElement) => {
let top = 0
while (el) {
top += el.offsetTop
el = el.offsetParent as HTMLElement
}
return top
}
-9
View File
@@ -155,15 +155,6 @@ export const renderPageContent = (
},
children: [],
},
// {
// type: "element",
// tagName: "anchor",
// properties: {
// name: node.properties?.id,
// },
// children: [],
// },
// TODO
]
},
})