优化大pdf加载

main
mula.liu 2026-01-01 23:05:43 +08:00
parent bb323f89e4
commit 0521bfdf0b
2 changed files with 72 additions and 1 deletions

View File

@ -1,5 +1,5 @@
# 构建阶段 # 构建阶段
FROM swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/node:18-alpine AS builder FROM swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/node:20-alpine AS builder
# 设置工作目录 # 设置工作目录
WORKDIR /app WORKDIR /app

View File

@ -202,6 +202,66 @@ function PreviewPage() {
setTocItems(headings) setTocItems(headings)
} }
}, [markdownContent]) }, [markdownContent])
//
const resolveRelativePath = (currentPath, relativePath) => {
if (relativePath.startsWith('/')) {
return relativePath.substring(1)
}
const lastSlashIndex = currentPath.lastIndexOf('/')
const currentDir = lastSlashIndex !== -1 ? currentPath.substring(0, lastSlashIndex) : ''
const parts = relativePath.split('/')
const dirParts = currentDir ? currentDir.split('/') : []
for (const part of parts) {
if (part === '..') {
dirParts.pop()
} else if (part !== '.' && part !== '') {
dirParts.push(part)
}
}
return dirParts.join('/')
}
// markdown
const handleMarkdownLink = (e, href) => {
if (!href || href.startsWith('http') || href.startsWith('//') || href.startsWith('#')) {
return
}
const isMd = href.endsWith('.md')
const isPdf = href.toLowerCase().endsWith('.pdf')
if (!isMd && !isPdf) return
e.preventDefault()
let decodedHref = href
try {
decodedHref = decodeURIComponent(href)
} catch (err) {
// ignore
}
const targetPath = resolveRelativePath(selectedFile, decodedHref)
const lastSlashIndex = targetPath.lastIndexOf('/')
const parentPath = lastSlashIndex !== -1 ? targetPath.substring(0, lastSlashIndex) : ''
if (parentPath && !openKeys.includes(parentPath)) {
const pathParts = parentPath.split('/')
const allParentPaths = []
let currentPath = ''
for (const part of pathParts) {
currentPath = currentPath ? `${currentPath}/${part}` : part
allParentPaths.push(currentPath)
}
setOpenKeys([...new Set([...openKeys, ...allParentPaths])])
}
handleMenuClick({ key: targetPath })
}
// //
const handleMenuClick = ({ key }) => { const handleMenuClick = ({ key }) => {
@ -325,6 +385,17 @@ function PreviewPage() {
<ReactMarkdown <ReactMarkdown
remarkPlugins={[remarkGfm]} remarkPlugins={[remarkGfm]}
rehypePlugins={[rehypeRaw, rehypeSlug, rehypeHighlight]} rehypePlugins={[rehypeRaw, rehypeSlug, rehypeHighlight]}
components={{
a: ({ node, href, children, ...props }) => (
<a
href={href}
onClick={(e) => handleMarkdownLink(e, href)}
{...props}
>
{children}
</a>
),
}}
> >
{markdownContent} {markdownContent}
</ReactMarkdown> </ReactMarkdown>