feat(v2): add editUrl option to docs plugin (#1818)

* feat(v2): add editUrl option to docs plugin

* nits

* misc(v2): nit

* misc(v2): nit
This commit is contained in:
Endi 2019-10-11 02:10:18 +08:00 committed by Yangshun Tay
parent aba9edc12c
commit ae678c9dad
10 changed files with 66 additions and 6 deletions

View file

@ -7,6 +7,7 @@
- Docs sidebar can now be more than one level deep, theoretically up to infinity - Docs sidebar can now be more than one level deep, theoretically up to infinity
- Collapsible docs sidebar! - Collapsible docs sidebar!
- Make doc page title larger - 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 ... - More documentation ...
- Slight tweaks to the Blog components - blog title is larger now - Slight tweaks to the Blog components - blog title is larger now

View file

@ -65,10 +65,14 @@
}, },
"lint-staged": { "lint-staged": {
"linters": { "linters": {
"*.{js,jsx,ts,tsx}": [ "*.{js,jsx}": [
"yarn lint --fix", "yarn lint --fix",
"yarn prettier", "yarn prettier",
"git add" "git add"
],
"*.{ts,tsx}": [
"yarn prettier",
"git add"
] ]
} }
}, },

View file

@ -60,4 +60,28 @@ describe('processMetadata', () => {
description: 'This has a different permalink', 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',
});
});
}); });

View file

@ -62,7 +62,7 @@ export default function pluginContentDocs(
// Fetches blog contents and returns metadata for the contents. // Fetches blog contents and returns metadata for the contents.
async loadContent() { async loadContent() {
const {include, routeBasePath, sidebarPath} = options; const {include, routeBasePath, sidebarPath, editUrl} = options;
const {siteConfig, siteDir} = context; const {siteConfig, siteDir} = context;
const docsDir = contentPath; const docsDir = contentPath;
@ -93,6 +93,7 @@ export default function pluginContentDocs(
siteConfig, siteConfig,
routeBasePath, routeBasePath,
siteDir, siteDir,
editUrl,
); );
docsMetadataRaw[metadata.id] = metadata; docsMetadataRaw[metadata.id] = metadata;
}), }),

View file

@ -18,6 +18,7 @@ export default async function processMetadata(
siteConfig: Partial<DocusaurusConfig>, siteConfig: Partial<DocusaurusConfig>,
docsBasePath: string, docsBasePath: string,
siteDir: string, siteDir: string,
editUrl?: string,
): Promise<MetadataRaw> { ): Promise<MetadataRaw> {
const filepath = path.join(docsDir, source); 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; return metadata as MetadataRaw;
} }

View file

@ -14,6 +14,7 @@ export interface PluginOptions {
docItemComponent: string; docItemComponent: string;
remarkPlugins: string[]; remarkPlugins: string[];
rehypePlugins: string[]; rehypePlugins: string[];
editUrl?: string;
} }
export type SidebarItemDoc = { export type SidebarItemDoc = {
@ -88,6 +89,7 @@ export interface MetadataRaw extends OrderMetadata {
source: string; source: string;
permalink: string; permalink: string;
sidebar_label?: string; sidebar_label?: string;
editUrl?: string;
[key: string]: any; [key: string]: any;
} }

View file

@ -34,7 +34,7 @@ function DocLegacyItem(props) {
const {siteConfig = {}} = useDocusaurusContext(); const {siteConfig = {}} = useDocusaurusContext();
const {url: siteUrl} = siteConfig; const {url: siteUrl} = siteConfig;
const {metadata, content: DocContent} = props; const {metadata, content: DocContent} = props;
const {description, title, permalink, image: metaImage} = metadata; const {description, title, permalink, image: metaImage, editUrl} = metadata;
return ( return (
<div> <div>
@ -74,7 +74,23 @@ function DocLegacyItem(props) {
<DocContent /> <DocContent />
</div> </div>
</article> </article>
<div className="margin-top--xl margin-bottom--lg"> {editUrl && (
<div className="margin-vert--xl">
<div className="row">
<div className="col">
{editUrl && (
<a
href={editUrl}
target="_blank"
rel="noreferrer noopener">
Edit this page
</a>
)}
</div>
</div>
</div>
)}
<div className="margin-vert--lg">
<DocLegacyPaginator metadata={metadata} /> <DocLegacyPaginator metadata={metadata} />
</div> </div>
</div> </div>

View file

@ -144,6 +144,10 @@ module.exports = {
* relative to site dir * relative to site dir
*/ */
path: 'docs', 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 * URL route for the blog section of your site
* do not include trailing slash * do not include trailing slash

View file

@ -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. 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: Deprecated. Pass it as an option to `@docusaurus/preset-classic` docs instead:
@ -279,6 +279,8 @@ module.exports = {
docs: { docs: {
// Equivalent to `customDocsPath`. // Equivalent to `customDocsPath`.
path: 'docs', path: 'docs',
// Equivalent to `editUrl`
editUrl: 'https://github.com/facebook/docusaurus/edit/master/website/docs/',
// Equivalent to `docsUrl`. // Equivalent to `docsUrl`.
routeBasePath: 'docs', routeBasePath: 'docs',
// Remark and Rehype plugins passed to MDX. Replaces `markdownOptions` and `markdownPlugins`. // 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 ### Deprecated fields that may be implemented using a plugin
- `enableUpdateBy` - `enableUpdateBy`
- `editUrl`
- `enableUpdateTime` - `enableUpdateTime`
- `scripts` - `scripts`
- `stylesheets` - `stylesheets`

View file

@ -32,6 +32,8 @@ module.exports = {
docs: { docs: {
path: 'docs', path: 'docs',
sidebarPath: require.resolve('./sidebars.js'), sidebarPath: require.resolve('./sidebars.js'),
editUrl:
'https://github.com/facebook/docusaurus/edit/master/website/docs/',
}, },
blog: { blog: {
path: '../website-1.x/blog', path: '../website-1.x/blog',