diff --git a/src/theme/CopyPageButton/index.tsx b/src/theme/CopyPageButton/index.tsx index 2d19c92ff..ca350bfc6 100644 --- a/src/theme/CopyPageButton/index.tsx +++ b/src/theme/CopyPageButton/index.tsx @@ -6,6 +6,7 @@ import IconCopy from "@theme/Icon/Copy" import IconSuccess from "@theme/Icon/Success" import Chevron from "@theme/Chevron" import { ArrowTopRightOnSquareIcon } from "@heroicons/react/16/solid" +import { getMarkdownUrl } from "../../utils/markdown-url" import styles from "./styles.module.css" @@ -46,13 +47,10 @@ export default function CopyPageButton(): JSX.Element { const { siteConfig } = useDocusaurusContext() - const basePath = siteConfig.baseUrl.replace(/\/$/, "") const pathname = typeof window !== "undefined" - ? window.location.pathname.replace(/\/$/, "") + ? window.location.pathname : "" - const markdownUrl = pathname === basePath - ? basePath + "/index.md" - : pathname + ".md" + const markdownUrl = getMarkdownUrl(pathname, siteConfig.baseUrl) const pageUrl = siteConfig.url + markdownUrl const chatPrompt = `I'd like to ask some questions about ${pageUrl}.\n\n` diff --git a/src/theme/DocItem/Content/index.tsx b/src/theme/DocItem/Content/index.tsx index 022dbbf66..1000c3bd1 100644 --- a/src/theme/DocItem/Content/index.tsx +++ b/src/theme/DocItem/Content/index.tsx @@ -1,11 +1,14 @@ import React, { type ReactNode } from "react" import clsx from "clsx" +import Head from "@docusaurus/Head" +import useDocusaurusContext from "@docusaurus/useDocusaurusContext" import { ThemeClassNames } from "@docusaurus/theme-common" import { useDoc } from "@docusaurus/plugin-content-docs/client" import Heading from "@theme/Heading" import MDXContent from "@theme/MDXContent" import type { Props } from "@theme/DocItem/Content" import CopyPageButton from "@theme/CopyPageButton" +import { getMarkdownUrl } from "../../../utils/markdown-url" import styles from "./styles.module.css" @@ -21,8 +24,14 @@ function useSyntheticTitle(): string | null { export default function DocItemContent({ children }: Props): ReactNode { const syntheticTitle = useSyntheticTitle() + const { metadata } = useDoc() + const { siteConfig } = useDocusaurusContext() + const markdownUrl = getMarkdownUrl(metadata.permalink, siteConfig.baseUrl) return (
+ + +
{syntheticTitle && {syntheticTitle}} diff --git a/src/utils/markdown-url.js b/src/utils/markdown-url.js new file mode 100644 index 000000000..011e27926 --- /dev/null +++ b/src/utils/markdown-url.js @@ -0,0 +1,16 @@ +// Shared rule for translating a doc page URL to its `.md` companion. +// Used by: +// - src/theme/CopyPageButton (runtime, via window.location.pathname) +// - plugins/raw-markdown (build time, via siteConfig.baseUrl + urlPath) +// Plain JS (CommonJS) so the Docusaurus plugin can `require()` it directly; +// `allowJs: true` in tsconfig.json lets the TSX side import it with types. + +function getMarkdownUrl(pathname, basePath) { + const normalized = pathname.replace(/\/$/, "") + const normalizedBase = (basePath || "").replace(/\/$/, "") + return normalized === normalizedBase + ? `${normalizedBase}/index.md` + : `${normalized}.md` +} + +module.exports = { getMarkdownUrl } diff --git a/static/_headers b/static/_headers index e4e8d71ac..e7c818a0f 100644 --- a/static/_headers +++ b/static/_headers @@ -12,6 +12,7 @@ Access-Control-Allow-Origin: * Access-Control-Allow-Methods: GET, OPTIONS Cache-Control: public, max-age=60 + X-Robots-Tag: noindex /*/web-console/*.json Access-Control-Allow-Origin: *