mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-09 23:27:28 +02:00
feat(v2): add disableVersioning config to docs plugin (#2989)
* add disableVersioning config to docs plugin * fix test * fix test
This commit is contained in:
parent
9265de982b
commit
a5b2b6056b
8 changed files with 44 additions and 16 deletions
|
@ -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(() => {
|
||||
|
|
|
@ -39,6 +39,7 @@ describe('normalizeDocsPluginOptions', () => {
|
|||
showLastUpdateAuthor: true,
|
||||
admonitions: {},
|
||||
excludeNextVersionDocs: true,
|
||||
disableVersioning: true,
|
||||
};
|
||||
|
||||
const {value} = await PluginOptionSchema.validate(userOptions);
|
||||
|
|
|
@ -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: [],
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -23,6 +23,7 @@ export const DEFAULT_OPTIONS: PluginOptions = {
|
|||
showLastUpdateAuthor: false,
|
||||
admonitions: {},
|
||||
excludeNextVersionDocs: false,
|
||||
disableVersioning: false,
|
||||
};
|
||||
|
||||
export const PluginOptionSchema = Joi.object({
|
||||
|
@ -51,4 +52,5 @@ export const PluginOptionSchema = Joi.object({
|
|||
excludeNextVersionDocs: Joi.bool().default(
|
||||
DEFAULT_OPTIONS.excludeNextVersionDocs,
|
||||
),
|
||||
disableVersioning: Joi.bool().default(DEFAULT_OPTIONS.disableVersioning),
|
||||
});
|
||||
|
|
|
@ -25,6 +25,7 @@ export interface PluginOptions extends MetadataOptions, PathOptions {
|
|||
remarkPlugins: ([Function, object] | Function)[];
|
||||
rehypePlugins: string[];
|
||||
admonitions: any;
|
||||
disableVersioning: boolean;
|
||||
excludeNextVersionDocs: boolean;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue