|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + */ |
| 8 | + |
| 9 | +import fs from 'node:fs'; |
| 10 | +import path from 'node:path'; |
| 11 | + |
| 12 | +import {MARKDOWN_NAMESPACE, relativeMarkdownPath} from './markdownPath.mjs'; |
| 13 | + |
| 14 | +const SITE_ALIAS = '@site'; |
| 15 | + |
| 16 | +function stripFrontMatter(raw) { |
| 17 | + return raw.replace(/^\uFEFF?---\r?\n[\s\S]*?\r?\n---\r?\n?/, ''); |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Drop the leading block of MDX `import`/`export` statements (and blank lines) |
| 22 | + * that appear before the first piece of real content. Only the leading block is |
| 23 | + * removed so `import`/`export` lines inside code fences are left untouched. |
| 24 | + */ |
| 25 | +function stripLeadingMdxStatements(body) { |
| 26 | + const lines = body.split('\n'); |
| 27 | + let index = 0; |
| 28 | + for (; index < lines.length; index++) { |
| 29 | + const trimmed = lines[index].trim(); |
| 30 | + if (trimmed === '' || /^(?:import|export)\b/.test(trimmed)) { |
| 31 | + continue; |
| 32 | + } |
| 33 | + break; |
| 34 | + } |
| 35 | + return lines.slice(index).join('\n'); |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Emit a clean Markdown copy of every doc page at build time so the |
| 40 | + * server-rendered CopyPageButton can link to / copy / hand off real Markdown |
| 41 | + * without any client-side DOM scraping. |
| 42 | + * |
| 43 | + * @type {import('@docusaurus/types').PluginModule} |
| 44 | + */ |
| 45 | +const copyPageButtonPlugin = async function (context) { |
| 46 | + const {siteDir, siteConfig} = context; |
| 47 | + const {baseUrl, url: siteUrl} = siteConfig; |
| 48 | + const outputRoot = path.join(siteDir, 'static', MARKDOWN_NAMESPACE); |
| 49 | + |
| 50 | + const resolveSource = source => { |
| 51 | + if (source.startsWith(SITE_ALIAS)) { |
| 52 | + return path.join(siteDir, source.slice(SITE_ALIAS.length)); |
| 53 | + } |
| 54 | + return path.isAbsolute(source) ? source : path.join(siteDir, source); |
| 55 | + }; |
| 56 | + |
| 57 | + return { |
| 58 | + // Runs in both dev and production, after every plugin has loaded its |
| 59 | + // content, so we have the authoritative permalink -> source mapping for |
| 60 | + // every doc (including the generated API reference). |
| 61 | + allContentLoaded({allContent}) { |
| 62 | + const docsContent = allContent['docusaurus-plugin-content-docs']; |
| 63 | + if (!docsContent) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + // Regenerate from scratch so renamed/removed pages don't leave orphans. |
| 68 | + fs.rmSync(outputRoot, {force: true, recursive: true}); |
| 69 | + |
| 70 | + const normalizedSiteUrl = String(siteUrl || '').replace(/\/$/, ''); |
| 71 | + |
| 72 | + for (const instance of Object.values(docsContent)) { |
| 73 | + const loadedVersions = (instance && instance.loadedVersions) || []; |
| 74 | + for (const version of loadedVersions) { |
| 75 | + for (const doc of version.docs || []) { |
| 76 | + const sourcePath = resolveSource(doc.source); |
| 77 | + let raw; |
| 78 | + try { |
| 79 | + raw = fs.readFileSync(sourcePath, 'utf-8'); |
| 80 | + } catch { |
| 81 | + continue; |
| 82 | + } |
| 83 | + |
| 84 | + let body = stripFrontMatter(raw); |
| 85 | + if (sourcePath.endsWith('.mdx')) { |
| 86 | + body = stripLeadingMdxStatements(body); |
| 87 | + } |
| 88 | + body = body.trim(); |
| 89 | + |
| 90 | + const pageUrl = `${normalizedSiteUrl}${doc.permalink}`; |
| 91 | + const header = /^#\s/.test(body) |
| 92 | + ? `URL: ${pageUrl}\n\n` |
| 93 | + : `# ${doc.title}\n\nURL: ${pageUrl}\n\n`; |
| 94 | + |
| 95 | + const outputPath = path.join( |
| 96 | + outputRoot, |
| 97 | + `${relativeMarkdownPath(doc.permalink, baseUrl)}.md`, |
| 98 | + ); |
| 99 | + fs.mkdirSync(path.dirname(outputPath), {recursive: true}); |
| 100 | + fs.writeFileSync(outputPath, `${header}${body}\n`); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + }, |
| 105 | + |
| 106 | + name: 'copy-page-button', |
| 107 | + }; |
| 108 | +}; |
| 109 | + |
| 110 | +export default copyPageButtonPlugin; |
0 commit comments