Skip to content

Commit 319cf3b

Browse files
committed
fix: special urls
1 parent 2a8cb58 commit 319cf3b

File tree

5 files changed

+73
-25
lines changed

5 files changed

+73
-25
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"@docusaurus/preset-classic": "^3.8.1",
3737
"@docusaurus/theme-mermaid": "^3.8.1",
3838
"@mdx-js/react": "^3.0.0",
39+
"@types/turndown": "^5.0.5",
3940
"ahooks": "^3.8.0",
4041
"antd": "^5.24.8",
4142
"axios": "^1.7.2",
@@ -54,6 +55,7 @@
5455
"react-scroll-progress-bar": "^2.0.3",
5556
"sass": "^1.77.8",
5657
"sass-resources-loader": "^2.2.5",
58+
"turndown": "^7.2.0",
5759
"vanilla-cookieconsent": "^3.1.0",
5860
"xml2js": "^0.6.2"
5961
},

src/components/CopyPageButton/index.tsx

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,38 @@ import CopiedSvg from "@site/static/icons/copied.svg";
88
import { useDoc } from "@docusaurus/plugin-content-docs/client";
99
import axios from "axios";
1010
import $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+
};
2229
const 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
>

src/components/CopyPageButton/styles.module.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.buttonCainter {
22
max-width: 140px;
3+
max-height: 33px;
34
button {
45
font-weight: 500;
56
border: 1px solid var(--color-border) !important;

src/css/custom.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,3 +368,7 @@ textarea {
368368
#cc-main .cm {
369369
border: 1px solid var(--color-border);
370370
}
371+
.ant-space-compact-block {
372+
display: flex;
373+
width: 100%;
374+
}

yarn.lock

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3268,6 +3268,11 @@
32683268
dependencies:
32693269
langium "3.3.1"
32703270

3271+
"@mixmark-io/domino@^2.2.0":
3272+
version "2.2.0"
3273+
resolved "https://registry.npmmirror.com/@mixmark-io/domino/-/domino-2.2.0.tgz#4e8ec69bf1afeb7a14f0628b7e2c0f35bdb336c3"
3274+
integrity sha512-Y28PR25bHXUg88kCV7nivXrP2Nj2RueZ3/l/jdx6J9f8J4nsEGcgX0Qe6lt7Pa+J79+kPiJU3LguR6O/6zrLOw==
3275+
32713276
"@module-federation/[email protected]":
32723277
version "0.16.0"
32733278
resolved "https://registry.yarnpkg.com/@module-federation/error-codes/-/error-codes-0.16.0.tgz#e375b2d10405cf24bae9a798337e4805cbcd69ee"
@@ -4381,6 +4386,11 @@
43814386
resolved "https://registry.yarnpkg.com/@types/trusted-types/-/trusted-types-2.0.7.tgz#baccb07a970b91707df3a3e8ba6896c57ead2d11"
43824387
integrity sha512-ScaPdn1dQczgbl0QFTeTOmVHFULt394XJgOQNoyVhZ6r2vLnMLJfBPd53SB52T/3G36VI1/g2MZaX0cwDuXsfw==
43834388

4389+
"@types/turndown@^5.0.5":
4390+
version "5.0.5"
4391+
resolved "https://registry.npmmirror.com/@types/turndown/-/turndown-5.0.5.tgz#614de24fc9ace4d8c0d9483ba81dc8c1976dd26f"
4392+
integrity sha512-TL2IgGgc7B5j78rIccBtlYAnkuv8nUQqhQc+DSYV5j9Be9XOcm/SKOVRuA47xAVI3680Tk9B1d8flK2GWT2+4w==
4393+
43844394
"@types/unist@*", "@types/unist@^3.0.0":
43854395
version "3.0.2"
43864396
resolved "https://registry.yarnpkg.com/@types/unist/-/unist-3.0.2.tgz#6dd61e43ef60b34086287f83683a5c1b2dc53d20"
@@ -11890,6 +11900,13 @@ tslib@^2.4.0:
1189011900
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.8.1.tgz#612efe4ed235d567e8aba5f2a5fab70280ade83f"
1189111901
integrity sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==
1189211902

11903+
turndown@^7.2.0:
11904+
version "7.2.0"
11905+
resolved "https://registry.npmmirror.com/turndown/-/turndown-7.2.0.tgz#67d614fe8371fb511079a93345abfd156c0ffcf4"
11906+
integrity sha512-eCZGBN4nNNqM9Owkv9HAtWRYfLA4h909E/WGAWWBpmB275ehNhZyk87/Tpvjbp0jjNl9XwCsbe6bm6CqFsgD+A==
11907+
dependencies:
11908+
"@mixmark-io/domino" "^2.2.0"
11909+
1189311910
type-fest@^0.21.3:
1189411911
version "0.21.3"
1189511912
resolved "https://registry.yarnpkg.com/type-fest/-/type-fest-0.21.3.tgz#d260a24b0198436e133fa26a524a6d65fa3b2e37"

0 commit comments

Comments
 (0)