diff --git a/CHANGELOG-2.x.md b/CHANGELOG-2.x.md index d14696b42fca..27099971c351 100644 --- a/CHANGELOG-2.x.md +++ b/CHANGELOG-2.x.md @@ -7,6 +7,7 @@ - Docs sidebar can now be more than one level deep, theoretically up to infinity - Collapsible docs sidebar! - Make doc page title larger + - Add `editUrl` option (URL for editing) to docs plugin. If this field is set, there will be an "Edit this page" link for each doc page. Example: 'https://github.com/facebook/docusaurus/edit/master/docs'. - More documentation ... - Slight tweaks to the Blog components - blog title is larger now diff --git a/package.json b/package.json index e7353261f020..5f8d2796babb 100644 --- a/package.json +++ b/package.json @@ -65,10 +65,14 @@ }, "lint-staged": { "linters": { - "*.{js,jsx,ts,tsx}": [ + "*.{js,jsx}": [ "yarn lint --fix", "yarn prettier", "git add" + ], + "*.{ts,tsx}": [ + "yarn prettier", + "git add" ] } }, diff --git a/packages/docusaurus-plugin-content-docs-legacy/src/__tests__/metadata.test.ts b/packages/docusaurus-plugin-content-docs-legacy/src/__tests__/metadata.test.ts index 117fe3a9d7f2..ff3ec530d3dd 100644 --- a/packages/docusaurus-plugin-content-docs-legacy/src/__tests__/metadata.test.ts +++ b/packages/docusaurus-plugin-content-docs-legacy/src/__tests__/metadata.test.ts @@ -60,4 +60,28 @@ describe('processMetadata', () => { description: 'This has a different permalink', }); }); + + test('docs with editUrl', async () => { + const editUrl = + 'https://github.com/facebook/docusaurus/edit/master/website/docs/'; + const source = path.join('foo', 'baz.md'); + const data = await processMetadata( + source, + docsDir, + {}, + siteConfig, + pluginPath, + siteDir, + editUrl, + ); + expect(data).toEqual({ + id: 'foo/baz', + permalink: '/docs/foo/baz', + source: path.join('@site', pluginPath, source), + title: 'baz', + editUrl: + 'https://github.com/facebook/docusaurus/edit/master/website/docs/foo/baz.md', + description: '## Images', + }); + }); }); diff --git a/packages/docusaurus-plugin-content-docs-legacy/src/index.ts b/packages/docusaurus-plugin-content-docs-legacy/src/index.ts index 790368818ae3..bb0ef63f1522 100644 --- a/packages/docusaurus-plugin-content-docs-legacy/src/index.ts +++ b/packages/docusaurus-plugin-content-docs-legacy/src/index.ts @@ -62,7 +62,7 @@ export default function pluginContentDocs( // Fetches blog contents and returns metadata for the contents. async loadContent() { - const {include, routeBasePath, sidebarPath} = options; + const {include, routeBasePath, sidebarPath, editUrl} = options; const {siteConfig, siteDir} = context; const docsDir = contentPath; @@ -93,6 +93,7 @@ export default function pluginContentDocs( siteConfig, routeBasePath, siteDir, + editUrl, ); docsMetadataRaw[metadata.id] = metadata; }), diff --git a/packages/docusaurus-plugin-content-docs-legacy/src/metadata.ts b/packages/docusaurus-plugin-content-docs-legacy/src/metadata.ts index d148a48840e0..dddc92c9162d 100644 --- a/packages/docusaurus-plugin-content-docs-legacy/src/metadata.ts +++ b/packages/docusaurus-plugin-content-docs-legacy/src/metadata.ts @@ -18,6 +18,7 @@ export default async function processMetadata( siteConfig: Partial, docsBasePath: string, siteDir: string, + editUrl?: string, ): Promise { const filepath = path.join(docsDir, source); @@ -82,5 +83,9 @@ export default async function processMetadata( } } + if (editUrl) { + metadata.editUrl = normalizeUrl([editUrl, source]); + } + return metadata as MetadataRaw; } diff --git a/packages/docusaurus-plugin-content-docs-legacy/src/types.ts b/packages/docusaurus-plugin-content-docs-legacy/src/types.ts index 96194af2e74f..bf29eb855312 100644 --- a/packages/docusaurus-plugin-content-docs-legacy/src/types.ts +++ b/packages/docusaurus-plugin-content-docs-legacy/src/types.ts @@ -14,6 +14,7 @@ export interface PluginOptions { docItemComponent: string; remarkPlugins: string[]; rehypePlugins: string[]; + editUrl?: string; } export type SidebarItemDoc = { @@ -88,6 +89,7 @@ export interface MetadataRaw extends OrderMetadata { source: string; permalink: string; sidebar_label?: string; + editUrl?: string; [key: string]: any; } diff --git a/packages/docusaurus-theme-classic/src/theme/DocLegacyItem/index.js b/packages/docusaurus-theme-classic/src/theme/DocLegacyItem/index.js index 143820bc051f..8db2241c64db 100644 --- a/packages/docusaurus-theme-classic/src/theme/DocLegacyItem/index.js +++ b/packages/docusaurus-theme-classic/src/theme/DocLegacyItem/index.js @@ -34,7 +34,7 @@ function DocLegacyItem(props) { const {siteConfig = {}} = useDocusaurusContext(); const {url: siteUrl} = siteConfig; const {metadata, content: DocContent} = props; - const {description, title, permalink, image: metaImage} = metadata; + const {description, title, permalink, image: metaImage, editUrl} = metadata; return (
@@ -74,7 +74,23 @@ function DocLegacyItem(props) {
-
+ {editUrl && ( +
+
+
+ {editUrl && ( + + Edit this page + + )} +
+
+
+ )} +
diff --git a/website/docs/advanced-plugins.md b/website/docs/advanced-plugins.md index 288f7be52fba..4ba547d83767 100644 --- a/website/docs/advanced-plugins.md +++ b/website/docs/advanced-plugins.md @@ -144,6 +144,10 @@ module.exports = { * relative to site dir */ path: 'docs', + /** + * URL for editing docs, example: 'https://github.com/facebook/docusaurus/edit/master/website/docs/' + */ + editUrl: 'https://github.com/repo/project/website/docs/', /** * URL route for the blog section of your site * do not include trailing slash diff --git a/website/docs/migration-from-v1-to-v2.md b/website/docs/migration-from-v1-to-v2.md index 18d9162f0996..707c59ac2e10 100644 --- a/website/docs/migration-from-v1-to-v2.md +++ b/website/docs/migration-from-v1-to-v2.md @@ -265,7 +265,7 @@ module.exports = { Deprecated. Create a `CNAME` file in your `static` folder instead. Files in the `static` folder will be copied into the root of the `build` folder during execution of the build command. -#### `customDocsPath`, `docsUrl` +#### `customDocsPath`, `docsUrl`, `editUrl` Deprecated. Pass it as an option to `@docusaurus/preset-classic` docs instead: @@ -279,6 +279,8 @@ module.exports = { docs: { // Equivalent to `customDocsPath`. path: 'docs', + // Equivalent to `editUrl` + editUrl: 'https://github.com/facebook/docusaurus/edit/master/website/docs/', // Equivalent to `docsUrl`. routeBasePath: 'docs', // Remark and Rehype plugins passed to MDX. Replaces `markdownOptions` and `markdownPlugins`. @@ -321,7 +323,6 @@ module.exports = { ### Deprecated fields that may be implemented using a plugin - `enableUpdateBy` -- `editUrl` - `enableUpdateTime` - `scripts` - `stylesheets` diff --git a/website/docusaurus.config.js b/website/docusaurus.config.js index f77c0430183d..2d6648c7a8df 100644 --- a/website/docusaurus.config.js +++ b/website/docusaurus.config.js @@ -32,6 +32,8 @@ module.exports = { docs: { path: 'docs', sidebarPath: require.resolve('./sidebars.js'), + editUrl: + 'https://github.com/facebook/docusaurus/edit/master/website/docs/', }, blog: { path: '../website-1.x/blog',