diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/__fixtures__/versioned-site/docusaurus.config.js b/packages/docusaurus-plugin-content-docs/src/__tests__/__fixtures__/versioned-site/docusaurus.config.js index 184b9c592b02..3f3fbaad3f0c 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/__fixtures__/versioned-site/docusaurus.config.js +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/__fixtures__/versioned-site/docusaurus.config.js @@ -11,4 +11,8 @@ module.exports = { url: 'https://your-docusaurus-test-site.com', baseUrl: '/', favicon: 'img/favicon.ico', + i18n: { + defaultLocale: 'en', + locales: ['en', 'fr'], + }, }; diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/__fixtures__/versioned-site/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md b/packages/docusaurus-plugin-content-docs/src/__tests__/__fixtures__/versioned-site/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md index 3589befaf984..9f7d1032ab3c 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/__fixtures__/versioned-site/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/__fixtures__/versioned-site/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md @@ -1 +1 @@ -Hello `1.0.0` ! (translated) +Hello `1.0.0` ! (translated en) diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/__fixtures__/versioned-site/i18n/fr/docusaurus-plugin-content-docs/version-1.0.0/hello.md b/packages/docusaurus-plugin-content-docs/src/__tests__/__fixtures__/versioned-site/i18n/fr/docusaurus-plugin-content-docs/version-1.0.0/hello.md new file mode 100644 index 000000000000..75ded9ec70c1 --- /dev/null +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/__fixtures__/versioned-site/i18n/fr/docusaurus-plugin-content-docs/version-1.0.0/hello.md @@ -0,0 +1 @@ +Hello `1.0.0` ! (translated fr) diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap b/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap index 8fff55146a69..fe937af19184 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap @@ -936,7 +936,7 @@ Object { \\"id\\": \\"version-1.0.0/hello\\", \\"isDocsHomePage\\": true, \\"title\\": \\"hello\\", - \\"description\\": \\"Hello 1.0.0 ! (translated)\\", + \\"description\\": \\"Hello 1.0.0 ! (translated en)\\", \\"source\\": \\"@site/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md\\", \\"slug\\": \\"/\\", \\"permalink\\": \\"/docs/1.0.0/\\", diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/docs.test.ts b/packages/docusaurus-plugin-content-docs/src/__tests__/docs.test.ts index d5fbb71427e6..5c5458318635 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/docs.test.ts +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/docs.test.ts @@ -14,6 +14,7 @@ import { DocMetadataBase, MetadataOptions, VersionMetadata, + PluginOptions, } from '../types'; import {LoadContext} from '@docusaurus/types'; import {DEFAULT_PLUGIN_ID} from '@docusaurus/core/lib/constants'; @@ -42,6 +43,7 @@ ${markdown} source, content, lastUpdate: {}, + docsDirPath: 'docs', filePath: source, }; }; @@ -111,19 +113,19 @@ function createTestUtils({ } describe('simple site', () => { - async function loadSite() { + async function loadSite( + loadSiteOptions: {options: Partial} = {options: {}}, + ) { const siteDir = path.join(fixtureDir, 'simple-site'); const context = await loadContext(siteDir); const options = { id: DEFAULT_PLUGIN_ID, ...DEFAULT_OPTIONS, + ...loadSiteOptions.options, }; const versionsMetadata = readVersionsMetadata({ context, - options: { - id: DEFAULT_PLUGIN_ID, - ...DEFAULT_OPTIONS, - }, + options, }); expect(versionsMetadata.length).toEqual(1); const [currentVersion] = versionsMetadata; @@ -191,12 +193,14 @@ describe('simple site', () => { }); test('homePageId doc', async () => { - const {siteDir, context, options, currentVersion} = await loadSite(); + const {siteDir, context, options, currentVersion} = await loadSite({ + options: {homePageId: 'hello'}, + }); const testUtilsLocal = createTestUtils({ siteDir, context, - options: {...options, homePageId: 'hello'}, + options, versionMetadata: currentVersion, }); @@ -213,12 +217,14 @@ describe('simple site', () => { }); test('homePageId doc nested', async () => { - const {siteDir, context, options, currentVersion} = await loadSite(); + const {siteDir, context, options, currentVersion} = await loadSite({ + options: {homePageId: 'foo/bar'}, + }); const testUtilsLocal = createTestUtils({ siteDir, context, - options: {...options, homePageId: 'foo/bar'}, + options, versionMetadata: currentVersion, }); @@ -235,15 +241,16 @@ describe('simple site', () => { }); test('docs with editUrl', async () => { - const {siteDir, context, options, currentVersion} = await loadSite(); + const {siteDir, context, options, currentVersion} = await loadSite({ + options: { + editUrl: 'https://github.com/facebook/docusaurus/edit/master/website', + }, + }); const testUtilsLocal = createTestUtils({ siteDir, context, - options: { - ...options, - editUrl: 'https://github.com/facebook/docusaurus/edit/master/website', - }, + options, versionMetadata: currentVersion, }); @@ -278,16 +285,17 @@ describe('simple site', () => { }); test('docs with last update time and author', async () => { - const {siteDir, context, options, currentVersion} = await loadSite(); - - const testUtilsLocal = createTestUtils({ - siteDir, - context, + const {siteDir, context, options, currentVersion} = await loadSite({ options: { - ...options, showLastUpdateAuthor: true, showLastUpdateTime: true, }, + }); + + const testUtilsLocal = createTestUtils({ + siteDir, + context, + options, versionMetadata: currentVersion, }); @@ -361,15 +369,16 @@ describe('simple site', () => { }); test('docs with slug on doc home', async () => { - const {siteDir, context, options, currentVersion} = await loadSite(); + const {siteDir, context, options, currentVersion} = await loadSite({ + options: { + homePageId: 'homePageId', + }, + }); const testUtilsLocal = createTestUtils({ siteDir, context, - options: { - ...options, - homePageId: 'homePageId', - }, + options, versionMetadata: currentVersion, }); expect(() => { @@ -388,19 +397,23 @@ describe('simple site', () => { }); describe('versioned site', () => { - async function loadSite() { + async function loadSite( + loadSiteOptions: {options: Partial; locale?: string} = { + options: {}, + }, + ) { const siteDir = path.join(fixtureDir, 'versioned-site'); - const context = await loadContext(siteDir); + const context = await loadContext(siteDir, { + locale: loadSiteOptions.locale, + }); const options = { id: DEFAULT_PLUGIN_ID, ...DEFAULT_OPTIONS, + ...loadSiteOptions.options, }; const versionsMetadata = readVersionsMetadata({ context, - options: { - id: DEFAULT_PLUGIN_ID, - ...DEFAULT_OPTIONS, - }, + options, }); expect(versionsMetadata.length).toEqual(4); const [ @@ -495,7 +508,7 @@ describe('versioned site', () => { permalink: '/docs/1.0.0/hello', slug: '/hello', title: 'hello', - description: 'Hello 1.0.0 ! (translated)', + description: 'Hello 1.0.0 ! (translated en)', version: '1.0.0', source: '@site/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md', @@ -582,15 +595,48 @@ describe('versioned site', () => { }); test('translated doc with editUrl', async () => { - const {siteDir, context, options, version100} = await loadSite(); + const {siteDir, context, options, version100} = await loadSite({ + options: { + editUrl: 'https://github.com/facebook/docusaurus/edit/master/website', + // editCurrentVersion: true, + }, + }); const testUtilsLocal = createTestUtils({ siteDir, context, + options, + versionMetadata: version100, + }); + + await testUtilsLocal.testMeta(path.join('hello.md'), { + id: 'version-1.0.0/hello', + unversionedId: 'hello', + isDocsHomePage: false, + permalink: '/docs/1.0.0/hello', + slug: '/hello', + title: 'hello', + description: 'Hello 1.0.0 ! (translated en)', + version: '1.0.0', + source: + '@site/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md', + editUrl: + 'https://github.com/facebook/docusaurus/edit/master/website/versioned_docs/version-1.0.0/hello.md', + }); + }); + + test('translated en doc with editUrl and editCurrentVersion=true', async () => { + const {siteDir, context, options, version100} = await loadSite({ options: { - ...options, editUrl: 'https://github.com/facebook/docusaurus/edit/master/website', + editCurrentVersion: true, }, + }); + + const testUtilsLocal = createTestUtils({ + siteDir, + context, + options, versionMetadata: version100, }); @@ -601,12 +647,77 @@ describe('versioned site', () => { permalink: '/docs/1.0.0/hello', slug: '/hello', title: 'hello', - description: 'Hello 1.0.0 ! (translated)', + description: 'Hello 1.0.0 ! (translated en)', version: '1.0.0', source: '@site/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md', editUrl: - 'https://github.com/facebook/docusaurus/edit/master/website/i18n/en/docusaurus-plugin-content-docs/version-1.0.0/hello.md', + 'https://github.com/facebook/docusaurus/edit/master/website/docs/hello.md', + }); + }); + + test('translated fr doc with editUrl and editLocalizedDocs=true', async () => { + const {siteDir, context, options, version100} = await loadSite({ + options: { + editUrl: 'https://github.com/facebook/docusaurus/edit/master/website', + editLocalizedDocs: true, + }, + locale: 'fr', + }); + + const testUtilsLocal = createTestUtils({ + siteDir, + context, + options, + versionMetadata: version100, + }); + + await testUtilsLocal.testMeta(path.join('hello.md'), { + id: 'version-1.0.0/hello', + unversionedId: 'hello', + isDocsHomePage: false, + permalink: '/fr/docs/1.0.0/hello', + slug: '/hello', + title: 'hello', + description: 'Hello 1.0.0 ! (translated fr)', + version: '1.0.0', + source: + '@site/i18n/fr/docusaurus-plugin-content-docs/version-1.0.0/hello.md', + editUrl: + 'https://github.com/facebook/docusaurus/edit/master/website/i18n/fr/docusaurus-plugin-content-docs/version-1.0.0/hello.md', + }); + }); + + test('translated fr doc with editUrl and editLocalizedDocs=true + editCurrentVersion=true', async () => { + const {siteDir, context, options, version100} = await loadSite({ + options: { + editUrl: 'https://github.com/facebook/docusaurus/edit/master/website', + editCurrentVersion: true, + editLocalizedDocs: true, + }, + locale: 'fr', + }); + + const testUtilsLocal = createTestUtils({ + siteDir, + context, + options, + versionMetadata: version100, + }); + + await testUtilsLocal.testMeta(path.join('hello.md'), { + id: 'version-1.0.0/hello', + unversionedId: 'hello', + isDocsHomePage: false, + permalink: '/fr/docs/1.0.0/hello', + slug: '/hello', + title: 'hello', + description: 'Hello 1.0.0 ! (translated fr)', + version: '1.0.0', + source: + '@site/i18n/fr/docusaurus-plugin-content-docs/version-1.0.0/hello.md', + editUrl: + 'https://github.com/facebook/docusaurus/edit/master/website/i18n/fr/docusaurus-plugin-content-docs/current/hello.md', }); }); }); diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/options.test.ts b/packages/docusaurus-plugin-content-docs/src/__tests__/options.test.ts index ef1cb969b25b..94a1a3d46b60 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/options.test.ts +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/options.test.ts @@ -38,6 +38,8 @@ describe('normalizeDocsPluginOptions', () => { excludeNextVersionDocs: true, includeCurrentVersion: false, disableVersioning: true, + editCurrentVersion: true, + editLocalizedDocs: true, versions: { current: { path: 'next', diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/versions.test.ts b/packages/docusaurus-plugin-content-docs/src/__tests__/versions.test.ts index 2d973cc57cef..188c0875dde2 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/versions.test.ts +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/versions.test.ts @@ -381,6 +381,107 @@ describe('versioned site, pluginId=default', () => { ]); }); + test('readVersionsMetadata versioned site with editUrl', async () => { + const { + defaultOptions, + defaultContext, + vCurrent, + v101, + v100, + vwithSlugs, + } = await loadSite(); + + const versionsMetadata = readVersionsMetadata({ + options: { + ...defaultOptions, + editUrl: 'https://github.com/facebook/docusaurus/edit/master/website/', + }, + context: defaultContext, + }); + + expect(versionsMetadata).toEqual([ + { + ...vCurrent, + versionEditUrl: + 'https://github.com/facebook/docusaurus/edit/master/website/docs', + versionEditUrlLocalized: + 'https://github.com/facebook/docusaurus/edit/master/website/i18n/en/docusaurus-plugin-content-docs/current', + }, + { + ...v101, + versionEditUrl: + 'https://github.com/facebook/docusaurus/edit/master/website/versioned_docs/version-1.0.1', + versionEditUrlLocalized: + 'https://github.com/facebook/docusaurus/edit/master/website/i18n/en/docusaurus-plugin-content-docs/version-1.0.1', + }, + { + ...v100, + versionEditUrl: + 'https://github.com/facebook/docusaurus/edit/master/website/versioned_docs/version-1.0.0', + versionEditUrlLocalized: + 'https://github.com/facebook/docusaurus/edit/master/website/i18n/en/docusaurus-plugin-content-docs/version-1.0.0', + }, + { + ...vwithSlugs, + versionEditUrl: + 'https://github.com/facebook/docusaurus/edit/master/website/versioned_docs/version-withSlugs', + versionEditUrlLocalized: + 'https://github.com/facebook/docusaurus/edit/master/website/i18n/en/docusaurus-plugin-content-docs/version-withSlugs', + }, + ]); + }); + + test('readVersionsMetadata versioned site with editUrl and editCurrentVersion=true', async () => { + const { + defaultOptions, + defaultContext, + vCurrent, + v101, + v100, + vwithSlugs, + } = await loadSite(); + + const versionsMetadata = readVersionsMetadata({ + options: { + ...defaultOptions, + editUrl: 'https://github.com/facebook/docusaurus/edit/master/website/', + editCurrentVersion: true, + }, + context: defaultContext, + }); + + expect(versionsMetadata).toEqual([ + { + ...vCurrent, + versionEditUrl: + 'https://github.com/facebook/docusaurus/edit/master/website/docs', + versionEditUrlLocalized: + 'https://github.com/facebook/docusaurus/edit/master/website/i18n/en/docusaurus-plugin-content-docs/current', + }, + { + ...v101, + versionEditUrl: + 'https://github.com/facebook/docusaurus/edit/master/website/docs', + versionEditUrlLocalized: + 'https://github.com/facebook/docusaurus/edit/master/website/i18n/en/docusaurus-plugin-content-docs/current', + }, + { + ...v100, + versionEditUrl: + 'https://github.com/facebook/docusaurus/edit/master/website/docs', + versionEditUrlLocalized: + 'https://github.com/facebook/docusaurus/edit/master/website/i18n/en/docusaurus-plugin-content-docs/current', + }, + { + ...vwithSlugs, + versionEditUrl: + 'https://github.com/facebook/docusaurus/edit/master/website/docs', + versionEditUrlLocalized: + 'https://github.com/facebook/docusaurus/edit/master/website/i18n/en/docusaurus-plugin-content-docs/current', + }, + ]); + }); + test('readVersionsMetadata versioned site with onlyIncludeVersions option', async () => { const {defaultOptions, defaultContext, v101, vwithSlugs} = await loadSite(); diff --git a/packages/docusaurus-plugin-content-docs/src/docs.ts b/packages/docusaurus-plugin-content-docs/src/docs.ts index a635f91737cb..bf6dfc0cad70 100644 --- a/packages/docusaurus-plugin-content-docs/src/docs.ts +++ b/packages/docusaurus-plugin-content-docs/src/docs.ts @@ -70,18 +70,18 @@ export async function readDocFile( source: string, options: LastUpdateOptions, ): Promise { - const folderPath = await getFolderContainingFile( + const docsDirPath = await getFolderContainingFile( getDocsDirPaths(versionMetadata), source, ); - const filePath = path.join(folderPath, source); + const filePath = path.join(docsDirPath, source); const [content, lastUpdate] = await Promise.all([ fs.readFile(filePath, 'utf-8'), readLastUpdateData(filePath, options), ]); - return {source, content, lastUpdate, filePath}; + return {source, content, lastUpdate, docsDirPath, filePath}; } export async function readVersionDocs( @@ -110,15 +110,24 @@ export function processDocMetadata({ context: LoadContext; options: MetadataOptions; }): DocMetadataBase { - const {source, content, lastUpdate, filePath} = docFile; - const {editUrl, homePageId} = options; + const {source, content, lastUpdate, docsDirPath, filePath} = docFile; + const {homePageId} = options; const {siteDir} = context; // ex: api/myDoc -> api // ex: myDoc -> . const docsFileDirName = path.dirname(source); - const docsEditUrl = getEditUrl(path.relative(siteDir, filePath), editUrl); + const relativeFilePath = path.relative(docsDirPath, filePath); + + const isLocalized = docsDirPath === versionMetadata.docsDirPathLocalized; + + const versionEditUrl = + isLocalized && options.editLocalizedDocs + ? versionMetadata.versionEditUrlLocalized + : versionMetadata.versionEditUrl; + + const docsEditUrl = getEditUrl(relativeFilePath, versionEditUrl); const {frontMatter = {}, excerpt} = parseMarkdownString(content); const {sidebar_label, custom_edit_url} = frontMatter; diff --git a/packages/docusaurus-plugin-content-docs/src/options.ts b/packages/docusaurus-plugin-content-docs/src/options.ts index e06ca1666ec2..9af7993fb27f 100644 --- a/packages/docusaurus-plugin-content-docs/src/options.ts +++ b/packages/docusaurus-plugin-content-docs/src/options.ts @@ -37,6 +37,8 @@ export const DEFAULT_OPTIONS: Omit = { disableVersioning: false, lastVersion: undefined, versions: {}, + editCurrentVersion: false, + editLocalizedDocs: false, }; const VersionOptionsSchema = Joi.object({ @@ -51,6 +53,8 @@ const VersionsOptionsSchema = Joi.object() export const OptionsSchema = Joi.object({ path: Joi.string().default(DEFAULT_OPTIONS.path), editUrl: URISchema, + editCurrentVersion: Joi.boolean().default(DEFAULT_OPTIONS.editCurrentVersion), + editLocalizedDocs: Joi.boolean().default(DEFAULT_OPTIONS.editLocalizedDocs), routeBasePath: Joi.string() // '' not allowed, see https://github.com/facebook/docusaurus/issues/3374 // .allow('') "" diff --git a/packages/docusaurus-plugin-content-docs/src/types.ts b/packages/docusaurus-plugin-content-docs/src/types.ts index eb21b96a18cb..ca5729a0e1b7 100644 --- a/packages/docusaurus-plugin-content-docs/src/types.ts +++ b/packages/docusaurus-plugin-content-docs/src/types.ts @@ -9,7 +9,8 @@ /// export type DocFile = { - filePath: string; + docsDirPath: string; // /!\ may be localized + filePath: string; // /!\ may be localized source: string; content: string; lastUpdate: LastUpdateData; @@ -21,6 +22,8 @@ export type VersionMetadata = { versionName: VersionName; // 1.0.0 versionLabel: string; // Version 1.0.0 versionPath: string; // /baseUrl/docs/1.0.0 + versionEditUrl?: string | undefined; + versionEditUrlLocalized?: string | undefined; isLast: boolean; docsDirPath: string; // "versioned_docs/version-1.0.0" docsDirPathLocalized: string; // "i18n/fr/version-1.0.0/default" @@ -32,6 +35,8 @@ export type MetadataOptions = { routeBasePath: string; homePageId?: string; editUrl?: string; + editCurrentVersion: boolean; + editLocalizedDocs: boolean; showLastUpdateTime?: boolean; showLastUpdateAuthor?: boolean; }; diff --git a/packages/docusaurus-plugin-content-docs/src/versions.ts b/packages/docusaurus-plugin-content-docs/src/versions.ts index 90034f435f2d..c47fa566381d 100644 --- a/packages/docusaurus-plugin-content-docs/src/versions.ts +++ b/packages/docusaurus-plugin-content-docs/src/versions.ts @@ -22,7 +22,7 @@ import { import {DEFAULT_PLUGIN_ID} from '@docusaurus/core/lib/constants'; import {LoadContext} from '@docusaurus/types'; -import {getPluginI18nPath, normalizeUrl} from '@docusaurus/utils'; +import {getPluginI18nPath, normalizeUrl, posixPath} from '@docusaurus/utils'; import {difference} from 'lodash'; import chalk from 'chalk'; @@ -131,6 +131,30 @@ function readVersionNames( return versions; } +function getDocsDirPathLocalized({ + siteDir, + locale, + pluginId, + versionName, +}: { + siteDir: string; + locale: string; + pluginId: string; + versionName: string; +}) { + return getPluginI18nPath({ + siteDir, + locale, + pluginName: 'docusaurus-plugin-content-docs', + pluginId, + subPaths: [ + versionName === CURRENT_VERSION_NAME + ? CURRENT_VERSION_NAME + : `version-${versionName}`, + ], + }); +} + function getVersionMetadataPaths({ versionName, context, @@ -152,16 +176,11 @@ function getVersionMetadataPaths({ `version-${versionName}`, ); - const docsDirPathLocalized = getPluginI18nPath({ + const docsDirPathLocalized = getDocsDirPathLocalized({ siteDir: context.siteDir, locale: context.i18n.currentLocale, - pluginName: 'docusaurus-plugin-content-docs', pluginId: options.id, - subPaths: [ - versionName === CURRENT_VERSION_NAME - ? CURRENT_VERSION_NAME - : `version-${versionName}`, - ], + versionName, }); const sidebarFilePath = isCurrentVersion @@ -174,6 +193,54 @@ function getVersionMetadataPaths({ return {docsDirPath, docsDirPathLocalized, sidebarFilePath}; } +function getVersionEditUrls({ + docsDirPath, + docsDirPathLocalized, + context: {siteDir, i18n}, + options: {id, path: currentVersionPath, editUrl, editCurrentVersion}, +}: { + docsDirPath: string; + docsDirPathLocalized: string; + context: Pick; + options: Pick< + PluginOptions, + 'id' | 'path' | 'editUrl' | 'editCurrentVersion' + >; +}): {versionEditUrl: string; versionEditUrlLocalized: string} | undefined { + if (!editUrl) { + return undefined; + } + + const editDirPath = editCurrentVersion ? currentVersionPath : docsDirPath; + const editDirPathLocalized = editCurrentVersion + ? getDocsDirPathLocalized({ + siteDir, + locale: i18n.currentLocale, + versionName: CURRENT_VERSION_NAME, + pluginId: id, + }) + : docsDirPathLocalized; + + const versionPathSegment = posixPath( + path.relative(siteDir, path.resolve(siteDir, editDirPath)), + ); + const versionPathSegmentLocalized = posixPath( + path.relative(siteDir, path.resolve(siteDir, editDirPathLocalized)), + ); + + const versionEditUrl = normalizeUrl([editUrl, versionPathSegment]); + + const versionEditUrlLocalized = normalizeUrl([ + editUrl, + versionPathSegmentLocalized, + ]); + + return { + versionEditUrl, + versionEditUrlLocalized, + }; +} + function createVersionMetadata({ versionName, isLast, @@ -185,7 +252,13 @@ function createVersionMetadata({ context: Pick; options: Pick< PluginOptions, - 'id' | 'path' | 'sidebarPath' | 'routeBasePath' | 'versions' + | 'id' + | 'path' + | 'sidebarPath' + | 'routeBasePath' + | 'versions' + | 'editUrl' + | 'editCurrentVersion' >; }): VersionMetadata { const { @@ -218,6 +291,13 @@ function createVersionMetadata({ versionPathPart, ]); + const versionEditUrls = getVersionEditUrls({ + docsDirPath, + docsDirPathLocalized, + context, + options, + }); + // Because /docs/:route` should always be after `/docs/versionName/:route`. const routePriority = versionPathPart === '' ? -1 : undefined; @@ -225,6 +305,8 @@ function createVersionMetadata({ versionName, versionLabel, versionPath, + versionEditUrl: versionEditUrls?.versionEditUrl, + versionEditUrlLocalized: versionEditUrls?.versionEditUrlLocalized, isLast, routePriority, sidebarFilePath, @@ -354,6 +436,8 @@ export function readVersionsMetadata({ | 'lastVersion' | 'versions' | 'onlyIncludeVersions' + | 'editUrl' + | 'editCurrentVersion' >; }): VersionMetadata[] { const versionNamesUnfiltered = readVersionNames(context.siteDir, options); diff --git a/website/docs/api/plugins/plugin-content-docs.md b/website/docs/api/plugins/plugin-content-docs.md index 7ecc423fe77e..43c2dd3d225d 100644 --- a/website/docs/api/plugins/plugin-content-docs.md +++ b/website/docs/api/plugins/plugin-content-docs.md @@ -35,6 +35,18 @@ module.exports = { * Example: 'https://github.com/facebook/docusaurus/edit/master/website/' */ editUrl: 'https://github.com/facebook/docusaurus/edit/master/website/', + /** + * When docs are versioned, the edit url will link to the doc + * in current version, instead of the versioned doc. + * Useful if you don't want users to submit doc pull-requests to older versions. + */ + editCurrentVersion: false, + /** + * When docs are localized, the edit url will target the localized doc, + * instead of the original unlocalized doc. + * Useful if you commit localized docs to git, instead of using a translation service. + */ + editLocalizedDocs: false, /** * URL route for the docs section of your site. * *DO NOT* include a trailing slash. diff --git a/website/docusaurus.config.js b/website/docusaurus.config.js index f5e25eb4b00e..148a35c03fe3 100644 --- a/website/docusaurus.config.js +++ b/website/docusaurus.config.js @@ -77,6 +77,7 @@ module.exports = { id: 'community', path: 'community', editUrl: 'https://github.com/facebook/docusaurus/edit/master/website/', + editCurrentVersion: true, routeBasePath: 'community', sidebarPath: require.resolve('./sidebarsCommunity.js'), showLastUpdateAuthor: true, @@ -205,20 +206,21 @@ module.exports = { sidebarPath: require.resolve('./sidebars.js'), editUrl: 'https://github.com/facebook/docusaurus/edit/master/website/', + editCurrentVersion: true, showLastUpdateAuthor: true, showLastUpdateTime: true, remarkPlugins: [ [require('@docusaurus/remark-plugin-npm2yarn'), {sync: true}], ], disableVersioning: isVersioningDisabled, - lastVersion: 'current', + lastVersion: isDev ? 'current' : undefined, onlyIncludeVersions: !isVersioningDisabled && (isDev || isDeployPreview) ? ['current', ...versions.slice(0, 2)] : undefined, versions: { current: { - label: `${getNextAlphaVersionName()} (unreleased)`, + label: `${getNextAlphaVersionName()} 🚧`, }, }, },