mirror of
https://git.mirrors.martin98.com/https://github.com/infiniflow/ragflow.git
synced 2025-08-14 01:15:51 +08:00
Feat: add toc to api doc (#5552)
### What problem does this PR solve? the api doc is too long, add a toc might be better  ### Type of change - [ ] Bug Fix (non-breaking change which fixes an issue) - [x] New Feature (non-breaking change which adds functionality) - [ ] Documentation Update - [ ] Refactoring - [ ] Performance Improvement - [ ] Other (please describe):
This commit is contained in:
parent
64e9702a26
commit
abac2ca2c5
@ -5,6 +5,7 @@ import { Button, Card, Flex, Space } from 'antd';
|
||||
import ChatApiKeyModal from '../chat-api-key-modal';
|
||||
import { usePreviewChat } from '../hooks';
|
||||
import BackendServiceApi from './backend-service-api';
|
||||
import MarkdownToc from './markdown-toc';
|
||||
|
||||
const ApiContent = ({
|
||||
id,
|
||||
@ -42,6 +43,9 @@ const ApiContent = ({
|
||||
</Flex>
|
||||
</Card>
|
||||
)}
|
||||
<div style={{ position: 'relative' }}>
|
||||
<MarkdownToc content={apiDoc} />
|
||||
</div>
|
||||
<MarkdownPreview source={apiDoc}></MarkdownPreview>
|
||||
</Flex>
|
||||
{apiKeyVisible && (
|
||||
|
@ -0,0 +1,74 @@
|
||||
import { Anchor } from 'antd';
|
||||
import type { AnchorLinkItemProps } from 'antd/es/anchor/Anchor';
|
||||
import React, { useEffect, useState } from 'react';
|
||||
|
||||
interface MarkdownTocProps {
|
||||
content: string;
|
||||
}
|
||||
|
||||
const MarkdownToc: React.FC<MarkdownTocProps> = ({ content }) => {
|
||||
const [items, setItems] = useState<AnchorLinkItemProps[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
const generateTocItems = () => {
|
||||
const headings = document.querySelectorAll(
|
||||
'.wmde-markdown h2, .wmde-markdown h3',
|
||||
);
|
||||
const tocItems: AnchorLinkItemProps[] = [];
|
||||
let currentH2Item: AnchorLinkItemProps | null = null;
|
||||
|
||||
headings.forEach((heading) => {
|
||||
const title = heading.textContent || '';
|
||||
const id = heading.id;
|
||||
const isH2 = heading.tagName.toLowerCase() === 'h2';
|
||||
|
||||
if (id && title) {
|
||||
const item: AnchorLinkItemProps = {
|
||||
key: id,
|
||||
href: `#${id}`,
|
||||
title,
|
||||
};
|
||||
|
||||
if (isH2) {
|
||||
currentH2Item = item;
|
||||
tocItems.push(item);
|
||||
} else {
|
||||
if (currentH2Item) {
|
||||
if (!currentH2Item.children) {
|
||||
currentH2Item.children = [];
|
||||
}
|
||||
currentH2Item.children.push(item);
|
||||
} else {
|
||||
tocItems.push(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
setItems(tocItems.slice(1));
|
||||
};
|
||||
|
||||
setTimeout(generateTocItems, 100);
|
||||
}, [content]);
|
||||
|
||||
return (
|
||||
<div
|
||||
className="markdown-toc"
|
||||
style={{
|
||||
position: 'fixed',
|
||||
right: 20,
|
||||
top: 100,
|
||||
width: 200,
|
||||
background: '#fff',
|
||||
padding: '10px',
|
||||
maxHeight: 'calc(100vh - 170px)',
|
||||
overflowY: 'auto',
|
||||
zIndex: 1000,
|
||||
}}
|
||||
>
|
||||
<Anchor items={items} affix={false} />
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default MarkdownToc;
|
Loading…
x
Reference in New Issue
Block a user