Skip to content

Commit

Permalink
feat(v2): add disableVersioning config to docs plugin (#2989)
Browse files Browse the repository at this point in the history
* add disableVersioning config to docs plugin

* fix test

* fix test
  • Loading branch information
slorber committed Jul 1, 2020
1 parent 9265de9 commit a5b2b60
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ describe('loadEnv', () => {
expect(env.versioning.versions).toStrictEqual(['1.0.1', '1.0.0']);
});

test('website with versioning but disabled', () => {
const siteDir = path.join(__dirname, '__fixtures__', 'versioned-site');
const env = loadEnv(siteDir, {disableVersioning: true});
expect(env.versioning.enabled).toBe(false);
expect(env.versioning.versions).toStrictEqual([]);
});

test('website with invalid versions.json file', () => {
const siteDir = path.join(__dirname, '__fixtures__', 'versioned-site');
const mock = jest.spyOn(JSON, 'parse').mockImplementationOnce(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ describe('normalizeDocsPluginOptions', () => {
showLastUpdateAuthor: true,
admonitions: {},
excludeNextVersionDocs: true,
disableVersioning: true,
};

const {value} = await PluginOptionSchema.validate(userOptions);
Expand Down
29 changes: 18 additions & 11 deletions packages/docusaurus-plugin-content-docs/src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ export function getVersionsJSONFile(siteDir: string): string {
return path.join(siteDir, VERSIONS_JSON_FILE);
}

export default function (siteDir: string): Env {
type EnvOptions = Partial<{disableVersioning: boolean}>;

export default function (
siteDir: string,
options: EnvOptions = {disableVersioning: false},
): Env {
const versioning: VersioningEnv = {
enabled: false,
versions: [],
Expand All @@ -37,16 +42,18 @@ export default function (siteDir: string): Env {

const versionsJSONFile = getVersionsJSONFile(siteDir);
if (fs.existsSync(versionsJSONFile)) {
const parsedVersions = JSON.parse(
fs.readFileSync(versionsJSONFile, 'utf8'),
);
if (parsedVersions && parsedVersions.length > 0) {
// eslint-disable-next-line prefer-destructuring
versioning.latestVersion = parsedVersions[0];
versioning.enabled = true;
versioning.versions = parsedVersions;
versioning.docsDir = getVersionedDocsDir(siteDir);
versioning.sidebarsDir = getVersionedSidebarsDir(siteDir);
if (!options.disableVersioning) {
const parsedVersions = JSON.parse(
fs.readFileSync(versionsJSONFile, 'utf8'),
);
if (parsedVersions && parsedVersions.length > 0) {
// eslint-disable-next-line prefer-destructuring
versioning.latestVersion = parsedVersions[0];
versioning.enabled = true;
versioning.versions = parsedVersions;
versioning.docsDir = getVersionedDocsDir(siteDir);
versioning.sidebarsDir = getVersionedSidebarsDir(siteDir);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/docusaurus-plugin-content-docs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export default function pluginContentDocs(
);

// Versioning.
const env = loadEnv(siteDir);
const env = loadEnv(siteDir, {disableVersioning: options.disableVersioning});
const {versioning} = env;
const {
versions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const DEFAULT_OPTIONS: PluginOptions = {
showLastUpdateAuthor: false,
admonitions: {},
excludeNextVersionDocs: false,
disableVersioning: false,
};

export const PluginOptionSchema = Joi.object({
Expand Down Expand Up @@ -51,4 +52,5 @@ export const PluginOptionSchema = Joi.object({
excludeNextVersionDocs: Joi.bool().default(
DEFAULT_OPTIONS.excludeNextVersionDocs,
),
disableVersioning: Joi.bool().default(DEFAULT_OPTIONS.disableVersioning),
});
1 change: 1 addition & 0 deletions packages/docusaurus-plugin-content-docs/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface PluginOptions extends MetadataOptions, PathOptions {
remarkPlugins: ([Function, object] | Function)[];
rehypePlugins: string[];
admonitions: any;
disableVersioning: boolean;
excludeNextVersionDocs: boolean;
}

Expand Down
17 changes: 13 additions & 4 deletions website/docs/using-plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,16 @@ module.exports = {
Then in the folder `my-plugin` you can create an index.js such as this

```js title="index.js"
module.exports = function(context, options) {
module.exports = function (context, options) {
// ...
return {
name: 'my-docusaurus-plugin',
async loadContent() { ... },
async contentLoaded({content, actions}) { ... },
async loadContent() {
/* ... */
},
async contentLoaded({content, actions}) {
/* ... */
},
/* other lifecycle API */
};
};
Expand All @@ -113,7 +117,7 @@ The `my-plugin` folder could also be a fully fledged package with it's own packa

`context` is plugin-agnostic and the same object will be passed into all plugins used for a Docusaurus website. The `context` object contains the following fields:

```js
```ts
interface LoadContext {
siteDir: string;
generatedFilesDir: string;
Expand Down Expand Up @@ -288,6 +292,11 @@ module.exports = {
* Whether to display the last date the doc was updated.
*/
showLastUpdateTime: false,
/**
* By default, versioning is enabled on versioned sites.
* This is a way to explicitly disable the versioning feature.
*/
disableVersioning: false,
/**
* Skip the next release docs when versioning is enabled.
* This will not generate HTML files in the production build for documents
Expand Down
1 change: 1 addition & 0 deletions website/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ module.exports = {
showLastUpdateAuthor: true,
showLastUpdateTime: true,
remarkPlugins: [require('./src/plugins/remark-npm2yarn')],
disableVersioning: !!process.env.DISABLE_VERSIONING,
},
blog: {
path: '../website-1.x/blog',
Expand Down

0 comments on commit a5b2b60

Please sign in to comment.