From 751a3579af57f7c9665cf1539ce72a84059cde1b Mon Sep 17 00:00:00 2001 From: endiliey Date: Wed, 9 Oct 2019 20:16:26 +0700 Subject: [PATCH 1/4] feat(v2): add editUrl option to docs plugin --- CHANGELOG-2.x.md | 1 + package.json | 6 ++++- .../src/__tests__/metadata.test.ts | 24 +++++++++++++++++ .../src/index.ts | 3 ++- .../src/metadata.ts | 5 ++++ .../src/types.ts | 2 ++ .../src/theme/DocLegacyItem/index.js | 11 +++++++- .../src/theme/DocLegacyItem/styles.module.css | 27 +++++++++++++++++++ website/docs/advanced-plugins.md | 4 +++ website/docs/migration-from-v1-to-v2.md | 5 ++-- website/docusaurus.config.js | 2 ++ 11 files changed, 85 insertions(+), 5 deletions(-) diff --git a/CHANGELOG-2.x.md b/CHANGELOG-2.x.md index b35ea12eb8cc..03c931a48157 100644 --- a/CHANGELOG-2.x.md +++ b/CHANGELOG-2.x.md @@ -5,6 +5,7 @@ - Docs, pages plugin is rewritten in TypeScript - Docs sidebar can now be more than one level deep, theoretically up to infinity - Collapsible docs sidebar! +- Add `editUrl` option (URL for editing) to docs plugin. If this field is set, there will be "Edit" button for each document page. Example: 'https://github.com/facebook/docusaurus/edit/master/docs'. - More documentation ... ## 2.0.0-alpha.25 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 b6916a87b23c..3cee555518af 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 (
@@ -66,6 +66,15 @@ function DocLegacyItem(props) {
+ {editUrl && ( + + {'Edit'} + + )}

{metadata.title}

diff --git a/packages/docusaurus-theme-classic/src/theme/DocLegacyItem/styles.module.css b/packages/docusaurus-theme-classic/src/theme/DocLegacyItem/styles.module.css index 8491fffc9634..6f2271432b4a 100644 --- a/packages/docusaurus-theme-classic/src/theme/DocLegacyItem/styles.module.css +++ b/packages/docusaurus-theme-classic/src/theme/DocLegacyItem/styles.module.css @@ -23,3 +23,30 @@ display: none; } } + +.editButton { + float: right; + margin-top: 5px; + display: flex; + align-items: center; + font-size: 14px; + font-weight: 500; + line-height: 1em; + height: 30px; + border-width: 1px; + border-style: solid; + border-radius: 3px; + transition: all 0.2s ease-out 0s; + text-decoration: none !important; + padding: 5px 16px; +} + +.editButton:hover { + color: var(--ifm-font-base-color); +} + +@media only screen and (max-width: 997px) { + .editButton { + display: none; + } +} \ No newline at end of file diff --git a/website/docs/advanced-plugins.md b/website/docs/advanced-plugins.md index 288f7be52fba..a58ec35f3860 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: '', /** * 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', From 318026264237d01354e2018e57cf328233b741cd Mon Sep 17 00:00:00 2001 From: endiliey Date: Wed, 9 Oct 2019 20:20:52 +0700 Subject: [PATCH 2/4] nits --- .../src/theme/DocLegacyItem/styles.module.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/docusaurus-theme-classic/src/theme/DocLegacyItem/styles.module.css b/packages/docusaurus-theme-classic/src/theme/DocLegacyItem/styles.module.css index 6f2271432b4a..10c3e79b32cb 100644 --- a/packages/docusaurus-theme-classic/src/theme/DocLegacyItem/styles.module.css +++ b/packages/docusaurus-theme-classic/src/theme/DocLegacyItem/styles.module.css @@ -49,4 +49,4 @@ .editButton { display: none; } -} \ No newline at end of file +} From 340d2329a8fc3e0dae494ac3d0512510a81d3229 Mon Sep 17 00:00:00 2001 From: Yangshun Tay Date: Wed, 9 Oct 2019 08:31:24 -0700 Subject: [PATCH 3/4] misc(v2): nit --- CHANGELOG-2.x.md | 8 +++--- .../src/theme/DocLegacyItem/index.js | 20 +++++++------- .../src/theme/DocLegacyItem/styles.module.css | 27 ------------------- website/docs/advanced-plugins.md | 2 +- 4 files changed, 15 insertions(+), 42 deletions(-) diff --git a/CHANGELOG-2.x.md b/CHANGELOG-2.x.md index 03c931a48157..cdf347dbf951 100644 --- a/CHANGELOG-2.x.md +++ b/CHANGELOG-2.x.md @@ -3,10 +3,10 @@ ## Unreleased - Docs, pages plugin is rewritten in TypeScript -- Docs sidebar can now be more than one level deep, theoretically up to infinity -- Collapsible docs sidebar! -- Add `editUrl` option (URL for editing) to docs plugin. If this field is set, there will be "Edit" button for each document page. Example: 'https://github.com/facebook/docusaurus/edit/master/docs'. -- More documentation ... +- Docs sidebar can now be more than one level deep - theoretically up to infinity, but 3-4 layers is the practical limit +- Collapsible docs sidebar! This is ideal for projects with a long list of docs +- 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... ## 2.0.0-alpha.25 diff --git a/packages/docusaurus-theme-classic/src/theme/DocLegacyItem/index.js b/packages/docusaurus-theme-classic/src/theme/DocLegacyItem/index.js index 3cee555518af..70305c1df616 100644 --- a/packages/docusaurus-theme-classic/src/theme/DocLegacyItem/index.js +++ b/packages/docusaurus-theme-classic/src/theme/DocLegacyItem/index.js @@ -66,15 +66,6 @@ function DocLegacyItem(props) {
- {editUrl && ( - - {'Edit'} - - )}

{metadata.title}

@@ -82,7 +73,16 @@ function DocLegacyItem(props) {
-
+ {editUrl && ( +
+ {editUrl && ( + + Edit this page + + )} +
+ )} +
diff --git a/packages/docusaurus-theme-classic/src/theme/DocLegacyItem/styles.module.css b/packages/docusaurus-theme-classic/src/theme/DocLegacyItem/styles.module.css index 10c3e79b32cb..8491fffc9634 100644 --- a/packages/docusaurus-theme-classic/src/theme/DocLegacyItem/styles.module.css +++ b/packages/docusaurus-theme-classic/src/theme/DocLegacyItem/styles.module.css @@ -23,30 +23,3 @@ display: none; } } - -.editButton { - float: right; - margin-top: 5px; - display: flex; - align-items: center; - font-size: 14px; - font-weight: 500; - line-height: 1em; - height: 30px; - border-width: 1px; - border-style: solid; - border-radius: 3px; - transition: all 0.2s ease-out 0s; - text-decoration: none !important; - padding: 5px 16px; -} - -.editButton:hover { - color: var(--ifm-font-base-color); -} - -@media only screen and (max-width: 997px) { - .editButton { - display: none; - } -} diff --git a/website/docs/advanced-plugins.md b/website/docs/advanced-plugins.md index a58ec35f3860..4ba547d83767 100644 --- a/website/docs/advanced-plugins.md +++ b/website/docs/advanced-plugins.md @@ -147,7 +147,7 @@ module.exports = { /** * URL for editing docs, example: 'https://github.com/facebook/docusaurus/edit/master/website/docs/' */ - editUrl: '', + editUrl: 'https://github.com/repo/project/website/docs/', /** * URL route for the blog section of your site * do not include trailing slash From 0f8d25c4be1222268604952e2a1788a8452ef700 Mon Sep 17 00:00:00 2001 From: Yangshun Tay Date: Wed, 9 Oct 2019 08:41:39 -0700 Subject: [PATCH 4/4] misc(v2): nit --- .../src/theme/DocLegacyItem/index.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/packages/docusaurus-theme-classic/src/theme/DocLegacyItem/index.js b/packages/docusaurus-theme-classic/src/theme/DocLegacyItem/index.js index 70305c1df616..488fae8b8279 100644 --- a/packages/docusaurus-theme-classic/src/theme/DocLegacyItem/index.js +++ b/packages/docusaurus-theme-classic/src/theme/DocLegacyItem/index.js @@ -74,12 +74,19 @@ function DocLegacyItem(props) {
{editUrl && ( -
- {editUrl && ( - - Edit this page - - )} +
+
+
+ {editUrl && ( + + Edit this page + + )} +
+
)}