fix: strip api/ prefix from externalDocs, remove harmful filtering, fix nested folder click
This commit is contained in:
@@ -65,22 +65,13 @@ export const ApiDocViewer = ({ onDocsPathChange, showAddModal, onCloseAddModal }
|
|||||||
for (const file of files) {
|
for (const file of files) {
|
||||||
const content = await window.electronAPI.readDocFile(file.path)
|
const content = await window.electronAPI.readDocFile(file.path)
|
||||||
if (content) {
|
if (content) {
|
||||||
docs.push({ name: file.name, path: file.path, relativePath: file.relativePath, content })
|
docs.push({ name: file.name, path: file.path, relativePath: file.relativePath.replace(/^api\//, ''), content })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setExternalDocs(docs)
|
setExternalDocs(docs)
|
||||||
|
|
||||||
// 过滤掉与父文件夹同名的 md 文件,只用于目录树显示
|
const fileList = docs.map(d => d.relativePath)
|
||||||
const filteredDocs = docs.filter(doc => {
|
|
||||||
const parts = doc.relativePath.split('/')
|
|
||||||
if (parts.length < 2) return true
|
|
||||||
const filename = parts[parts.length - 1].replace(/\.md$/, '')
|
|
||||||
const parentFolder = parts[parts.length - 2]
|
|
||||||
return filename !== parentFolder
|
|
||||||
})
|
|
||||||
|
|
||||||
const fileList = filteredDocs.map(d => d.relativePath.replace(/^api\//, ''))
|
|
||||||
const tree = buildFileTree(fileList, '/')
|
const tree = buildFileTree(fileList, '/')
|
||||||
setFileTree(tree)
|
setFileTree(tree)
|
||||||
|
|
||||||
@@ -106,12 +97,14 @@ export const ApiDocViewer = ({ onDocsPathChange, showAddModal, onCloseAddModal }
|
|||||||
}, [externalDocs])
|
}, [externalDocs])
|
||||||
|
|
||||||
const handleFolderClick = useCallback((folderPath: string) => {
|
const handleFolderClick = useCallback((folderPath: string) => {
|
||||||
|
const parts = folderPath.split('/')
|
||||||
|
const basename = parts[parts.length - 1]
|
||||||
const sameNameDoc = externalDocs.find(d =>
|
const sameNameDoc = externalDocs.find(d =>
|
||||||
d.relativePath.endsWith(`/${folderPath}.md`)
|
d.relativePath.endsWith(`/${basename}.md`)
|
||||||
)
|
)
|
||||||
|
|
||||||
if (sameNameDoc) {
|
if (sameNameDoc) {
|
||||||
setSelectedPath(sameNameDoc.relativePath.replace(/^api\//, ''))
|
setSelectedPath(sameNameDoc.relativePath)
|
||||||
setCurrentContent(sameNameDoc.content)
|
setCurrentContent(sameNameDoc.content)
|
||||||
}
|
}
|
||||||
}, [externalDocs])
|
}, [externalDocs])
|
||||||
@@ -133,7 +126,7 @@ export const ApiDocViewer = ({ onDocsPathChange, showAddModal, onCloseAddModal }
|
|||||||
return docPath === ref || docPath.endsWith('/' + ref.split('/').pop())
|
return docPath === ref || docPath.endsWith('/' + ref.split('/').pop())
|
||||||
})
|
})
|
||||||
if (match) {
|
if (match) {
|
||||||
setSelectedPath(match.relativePath.replace(/^api\//, ''))
|
setSelectedPath(match.relativePath)
|
||||||
setCurrentContent(match.content)
|
setCurrentContent(match.content)
|
||||||
}
|
}
|
||||||
}, [externalDocs])
|
}, [externalDocs])
|
||||||
|
|||||||
Reference in New Issue
Block a user