Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion plugins/raw-markdown/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ const {
prependFrontmatter,
normalizeNewLines,
} = require("./convert-components")
const { getMarkdownUrl } = require("../../src/utils/markdown-url")

module.exports = () => ({
name: "raw-markdown",
async postBuild({ outDir, plugins }) {
async postBuild({ siteConfig, outDir, plugins }) {
const docsPath = path.join(__dirname, "../../documentation")
const outputBase = outDir

Expand Down Expand Up @@ -110,6 +111,32 @@ module.exports = () => ({

fs.writeFileSync(outputFile, processedContent, "utf8")
fileCount++

// Advertise the .md companion in the rendered HTML's <head> so
// HTML-parsing agents can discover it without sniffing URL patterns.
// URL format matches CopyPageButton via the shared getMarkdownUrl
// helper. Assumes docusaurus.config.js keeps `trailingSlash: true`
// so HTML lands at ${urlPath}/index.html.
const htmlPath = urlPath
? path.join(outputBase, urlPath, "index.html")
: path.join(outputBase, "index.html")
if (fs.existsSync(htmlPath)) {
const pageUrl = urlPath
? path.posix.join(siteConfig.baseUrl, urlPath)
: siteConfig.baseUrl
const mdHref = getMarkdownUrl(pageUrl, siteConfig.baseUrl)
const tag = `<link rel="alternate" type="text/markdown" href="${mdHref}">`
const html = fs.readFileSync(htmlPath, "utf8")
if (!html.includes('type="text/markdown"')) {
const idx = html.lastIndexOf("</head>")
if (idx === -1) {
console.warn(`[raw-markdown] No </head> found in ${htmlPath}; skipping alternate link`)
} else {
const patched = html.slice(0, idx) + tag + html.slice(idx)
fs.writeFileSync(htmlPath, patched, "utf8")
}
}
}
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/theme/CopyPageButton/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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`
Expand Down
16 changes: 16 additions & 0 deletions src/utils/markdown-url.js
Original file line number Diff line number Diff line change
@@ -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 }
1 change: 1 addition & 0 deletions static/_headers
Original file line number Diff line number Diff line change
Expand Up @@ -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: *
Expand Down
Loading