@@ -8,31 +8,38 @@ import CopiedSvg from "@site/static/icons/copied.svg";
88import { useDoc } from "@docusaurus/plugin-content-docs/client" ;
99import axios from "axios" ;
1010import $t from "@site/src/utils/tools" ;
11- // mark
12- // import TurndownService from "turndown";
13- // const turndownService = new TurndownService();
14- // const getPageContentAsHtml = (): string | null => {
15- // const contentElement = document.querySelector("article");
16- // return contentElement ? contentElement.innerHTML : null;
17- // };
11+ import TurndownService from "turndown" ;
12+ const SPECIAL_LINKS = [
13+ "/guides/" ,
14+ "/guides/products/dc/platforms" ,
15+ "/guides/products/dc/pricing" ,
16+ "guides/deploy/deploy/non-production/deploying-databend" ,
17+ "/guides/cloud/new-account" ,
18+ ] ;
1819
19- // const convertHtmlToMarkdown = (html: string): string => {
20- // return turndownService.turndown(html);
21- // };
20+ const getPageContentAsHtml = ( ) : string | null => {
21+ const contentElement = document . querySelector ( "article" ) ;
22+ return contentElement ? contentElement . innerHTML : null ;
23+ } ;
24+
25+ const convertHtmlToMarkdown = ( html : string ) : string => {
26+ const turndownService = new TurndownService ( ) ;
27+ return turndownService . turndown ( html ) ;
28+ } ;
2229const CopyDropdownButton : React . FC = ( ) => {
2330 const [ loading , setLoading ] = useState ( false ) ;
2431 const [ isCopied , setIsCopied ] = useState ( false ) ;
2532 const { metadata } = useDoc ( ) ;
26- const sourceUrl = useMemo ( ( ) => {
27- return (
28- metadata ?. source ?. replace (
29- "@site" ,
30- "https://raw.githubusercontent.com/databendlabs/databend-docs/refs/heads/main"
31- ) || ""
32- ) ;
33- } , [ metadata ] ) ;
34- const handleCopy = useCallback ( ( url : string ) => {
35- if ( ! url ) return ;
33+ function copyHtml ( ) {
34+ setIsCopied ( true ) ;
35+ const htmlContent = getPageContentAsHtml ( ) ;
36+ const markdownContent = convertHtmlToMarkdown ( htmlContent ) ;
37+ navigator . clipboard . writeText ( markdownContent ?. replace ( "Copy Page" , "" ) ) ;
38+ setTimeout ( ( ) => {
39+ setIsCopied ( false ) ;
40+ } , 3000 ) ;
41+ }
42+ function copyMarkdown ( url : string ) {
3643 setLoading ( true ) ;
3744 axios
3845 . get ( url )
@@ -50,18 +57,35 @@ const CopyDropdownButton: React.FC = () => {
5057 setIsCopied ( false ) ;
5158 } , 3000 ) ;
5259 } ) ;
60+ }
61+ const sourceUrl = useMemo ( ( ) => {
62+ return (
63+ metadata ?. source ?. replace (
64+ "@site" ,
65+ "https://raw.githubusercontent.com/databendlabs/databend-docs/refs/heads/main"
66+ ) || ""
67+ ) ;
68+ } , [ metadata ] ) ;
69+ const handleCopy = useCallback ( ( url : string ) => {
70+ const nowLink = metadata ?. permalink ;
71+ if ( SPECIAL_LINKS ?. some ( ( link ) => link === nowLink ) ) {
72+ copyHtml ( ) ;
73+ return ;
74+ }
75+ if ( ! url ) return ;
76+ copyMarkdown ( url ) ;
5377 } , [ ] ) ;
5478 const menu = useMemo ( ( ) => {
5579 const items = [
5680 {
5781 key : "copy" ,
58- icon : < CopySvg width = { 16 } /> ,
82+ icon : < CopySvg width = { 16 } height = { 16 } /> ,
5983 label : $t ( "Copy Page" ) ,
6084 description : $t ( "Copy page as Markdown for LLMs" ) ,
6185 } ,
6286 {
6387 key : "markdown" ,
64- icon : < MarkdownSvg width = { 18 } /> ,
88+ icon : < MarkdownSvg width = { 18 } height = { 18 } /> ,
6589 label : $t ( "View as Markdown" ) ,
6690 description : $t ( "View this page as plain text" ) ,
6791 } ,
@@ -90,9 +114,9 @@ const CopyDropdownButton: React.FC = () => {
90114 { loading ? (
91115 < Spin size = "small" />
92116 ) : isCopied ? (
93- < CopiedSvg width = { 16 } />
117+ < CopiedSvg width = { 16 } height = { 16 } />
94118 ) : (
95- < CopySvg width = { 16 } />
119+ < CopySvg width = { 16 } height = { 16 } />
96120 ) }
97121 < span > { loading ? $t ( "Copying..." ) : $t ( "Copy Page" ) } </ span >
98122 </ Flex >
@@ -105,7 +129,7 @@ const CopyDropdownButton: React.FC = () => {
105129 onClick = { ( ) => handleCopy ( sourceUrl ) }
106130 menu = { menu }
107131 placement = "bottomRight"
108- icon = { < DownArrow width = { 18 } height = { "auto" } /> }
132+ icon = { < DownArrow width = { 18 } height = { 18 } /> }
109133 className = { styles . buttonCainter }
110134 trigger = { [ "click" ] }
111135 >
0 commit comments