Skip to content

Commit d513dec

Browse files
authored
feat(v1): add deletedDocs config to escape from versioning fallback (#2955)
* feat: support for deletedDocs in siteConfig fixes #2429 * docs: document deletedDocs option * feat: allow array in deletedDocs config * docs: clarify deletedDocs version formatting
1 parent bdffd28 commit d513dec

File tree

4 files changed

+77
-3
lines changed

4 files changed

+77
-3
lines changed

docs/api-site-config.md

+20
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,26 @@ customDocsPath: 'website-docs';
128128

129129
The default version for the site to be shown. If this is not set, the latest version will be shown.
130130

131+
#### `deletedDocs` [object]
132+
133+
Even if you delete the main file for a documentation page and delete it from your sidebar, the page will still be created for every version and for the current version due to [fallback functionality](versioning#fallback-functionality). This can lead to confusion if people find the documentation by searching and it appears to be something relevant to a particular version but actually is not.
134+
135+
To force removal of content beginning with a certain version (including for current/next), add a `deletedDocs` object to your config, where each key is a version and the value is an array of document IDs that should not be generated for that version and all later versions.
136+
137+
Example:
138+
139+
```js
140+
{
141+
deletedDocs: {
142+
"2.0.0": [
143+
"tagging"
144+
]
145+
}
146+
}
147+
```
148+
149+
The version keys must match those in `versions.json`. Assuming the versions list in `versions.json` is `["3.0.0", "2.0.0", "1.1.0", "1.0.0"]`, the `docs/1.0.0/tagging` and `docs/1.1.0/tagging` URLs will work but `docs/2.0.0/tagging`, `docs/3.0.0/tagging`, and `docs/tagging` will not. The files and folders for those versions will not be generated during the build.
150+
131151
#### `docsUrl` [string]
132152

133153
The base URL for all docs file. Set this field to `''` to remove the `docs` prefix of the documentation URL. If unset, it is defaulted to `docs`.

docs/guides-versioning.md

+2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ Only files in the `docs` directory and sidebar files that differ from those of t
7272

7373
For example, a document with the original id `doc1` exists for the latest version, `1.0.0`, and has the same content as the document with the id `doc1` in the `docs` directory. When a new version `2.0.0` is created, the file for `doc1` will not be copied into `versioned_docs/version-2.0.0/`. There will still be a page for `docs/2.0.0/doc1.html`, but it will use the file from version `1.0.0`.
7474

75+
Because of the way this fallback works, pages that you delete are not really deleted from the website unless you tell Docusaurus to skip fallback after a certain version. To do this, use the [`deletedDocs`](api-site-config.md#deleteddocs-object) option in `siteConfig.js`.
76+
7577
## Renaming Existing Versions
7678

7779
To rename an existing version number to something else, first make sure the following script is in your `package.json` file:

packages/docusaurus-1.x/lib/server/readMetadata.js

+21
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ const utils = require('./utils.js');
2525

2626
const docsPart = `${siteConfig.docsUrl ? `${siteConfig.docsUrl}/` : ''}`;
2727

28+
// Get a list of all IDs that have been deleted in any version.
29+
// We will assume these should not be in the current/next version.
30+
const allDeletedIds = new Set();
31+
if (siteConfig.deletedDocs) {
32+
Object.values(siteConfig.deletedDocs).forEach((idList) => {
33+
idList.forEach((id) => allDeletedIds.add(id));
34+
});
35+
}
36+
2837
const SupportedHeaderFields = new Set([
2938
'id',
3039
'title',
@@ -238,6 +247,12 @@ function generateMetadataDocs() {
238247
return;
239248
}
240249
const metadata = res.metadata;
250+
if (
251+
allDeletedIds.has(metadata.id) ||
252+
(metadata.original_id && allDeletedIds.has(metadata.original_id))
253+
) {
254+
return;
255+
}
241256
metadatas[metadata.id] = metadata;
242257

243258
// create a default list of documents for each enabled language based on docs in English
@@ -290,6 +305,12 @@ function generateMetadataDocs() {
290305
return;
291306
}
292307
const metadata = res.metadata;
308+
if (
309+
allDeletedIds.has(metadata.id) ||
310+
(metadata.original_id && allDeletedIds.has(metadata.original_id))
311+
) {
312+
return;
313+
}
293314
metadatas[metadata.id] = metadata;
294315
}
295316
});

packages/docusaurus-1.x/lib/server/versionFallback.js

+34-3
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,46 @@ function docVersion(id, reqVersion) {
115115
// iterate through versions until a version less than or equal to the requested
116116
// is found, then check if that version has an available file to use
117117
let requestedFound = false;
118+
let availableVersion = null;
119+
let deletedDocs = null;
120+
if (siteConfig.deletedDocs) {
121+
// Config file may have either Array or Set for each version. Convert
122+
// all to Set to make the check faster in the versions loop below.
123+
deletedDocs = {};
124+
Object.keys(siteConfig.deletedDocs).forEach((deletedDocVersion) => {
125+
deletedDocs[deletedDocVersion] = new Set(
126+
siteConfig.deletedDocs[deletedDocVersion],
127+
);
128+
});
129+
}
118130
for (let i = 0; i < versions.length; i++) {
119131
if (versions[i] === reqVersion) {
120132
requestedFound = true;
121133
}
122-
if (requestedFound && available[id].has(versions[i])) {
123-
return versions[i];
134+
if (requestedFound) {
135+
// If this ID is deleted as of any version equal to or prior to
136+
// the requested, return null.
137+
if (
138+
deletedDocs &&
139+
deletedDocs[versions[i]] &&
140+
deletedDocs[versions[i]].has(id)
141+
) {
142+
return null;
143+
}
144+
if (!availableVersion && available[id].has(versions[i])) {
145+
availableVersion = versions[i];
146+
// Note the fallback version but keep looping in case this ID
147+
// was deleted as of a previous version.
148+
//
149+
// If `deletedDocs` config isn't used, we can return immediately
150+
// and avoid unnecessary looping.
151+
if (!deletedDocs) {
152+
break;
153+
}
154+
}
124155
}
125156
}
126-
return null;
157+
return availableVersion;
127158
}
128159

129160
// returns whether a given file has content that differ from the

0 commit comments

Comments
 (0)