mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-17 02:56:57 +02:00
chore(v2): use single method Lodash packages in docs plugin (#2519)
* chore(v2): use single method Lodash packages in docs plugin * reorder packages * TS types nightmare
This commit is contained in:
parent
36f4f9487e
commit
b4acb89888
4 changed files with 27 additions and 19 deletions
|
@ -35,7 +35,11 @@
|
||||||
"@types/jest": "^24.0.23",
|
"@types/jest": "^24.0.23",
|
||||||
"@types/loader-utils": "^1.1.3",
|
"@types/loader-utils": "^1.1.3",
|
||||||
"@types/lodash": "^4.14.149",
|
"@types/lodash": "^4.14.149",
|
||||||
|
"@types/lodash.flatmap": "^4.5.6",
|
||||||
|
"@types/lodash.groupby": "^4.6.6",
|
||||||
"@types/lodash.kebabcase": "^4.1.6",
|
"@types/lodash.kebabcase": "^4.1.6",
|
||||||
|
"@types/lodash.pick": "^4.4.6",
|
||||||
|
"@types/lodash.pickby": "^4.6.6",
|
||||||
"@types/node": "^12.12.14",
|
"@types/node": "^12.12.14",
|
||||||
"@types/react": "^16.9.13",
|
"@types/react": "^16.9.13",
|
||||||
"@types/react-dev-utils": "^9.0.1",
|
"@types/react-dev-utils": "^9.0.1",
|
||||||
|
|
|
@ -23,7 +23,10 @@
|
||||||
"globby": "^10.0.1",
|
"globby": "^10.0.1",
|
||||||
"import-fresh": "^3.2.1",
|
"import-fresh": "^3.2.1",
|
||||||
"loader-utils": "^1.2.3",
|
"loader-utils": "^1.2.3",
|
||||||
"lodash": "^4.17.15",
|
"lodash.flatmap": "^4.5.0",
|
||||||
|
"lodash.groupby": "^4.6.0",
|
||||||
|
"lodash.pick": "^4.4.0",
|
||||||
|
"lodash.pickby": "^4.6.0",
|
||||||
"shelljs": "^0.8.3"
|
"shelljs": "^0.8.3"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
|
|
|
@ -5,7 +5,9 @@
|
||||||
* LICENSE file in the root directory of this source tree.
|
* LICENSE file in the root directory of this source tree.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import _ from 'lodash';
|
import groupBy from 'lodash.groupby';
|
||||||
|
import pick from 'lodash.pick';
|
||||||
|
import pickBy from 'lodash.pickby';
|
||||||
import globby from 'globby';
|
import globby from 'globby';
|
||||||
import fs from 'fs-extra';
|
import fs from 'fs-extra';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
@ -101,13 +103,13 @@ export default function pluginContentDocs(
|
||||||
const {include} = options;
|
const {include} = options;
|
||||||
let globPattern = include.map(pattern => `${docsDir}/${pattern}`);
|
let globPattern = include.map(pattern => `${docsDir}/${pattern}`);
|
||||||
if (versioning.enabled) {
|
if (versioning.enabled) {
|
||||||
const docsGlob = _.flatten(
|
const docsGlob = include
|
||||||
include.map(pattern =>
|
.map(pattern =>
|
||||||
versionsNames.map(
|
versionsNames.map(
|
||||||
versionName => `${versionedDir}/${versionName}/${pattern}`,
|
versionName => `${versionedDir}/${versionName}/${pattern}`,
|
||||||
),
|
),
|
||||||
),
|
)
|
||||||
);
|
.reduce((a, b) => a.concat(b), []);
|
||||||
const sidebarsGlob = versionsNames.map(
|
const sidebarsGlob = versionsNames.map(
|
||||||
versionName => `${versionedSidebarsDir}/${versionName}-sidebars.json`,
|
versionName => `${versionedSidebarsDir}/${versionName}-sidebars.json`,
|
||||||
);
|
);
|
||||||
|
@ -149,11 +151,11 @@ export default function pluginContentDocs(
|
||||||
|
|
||||||
// Metadata for versioned docs.
|
// Metadata for versioned docs.
|
||||||
if (versioning.enabled) {
|
if (versioning.enabled) {
|
||||||
const versionedGlob = _.flatten(
|
const versionedGlob = include
|
||||||
include.map(pattern =>
|
.map(pattern =>
|
||||||
versionsNames.map(versionName => `${versionName}/${pattern}`),
|
versionsNames.map(versionName => `${versionName}/${pattern}`),
|
||||||
),
|
)
|
||||||
);
|
.reduce((a, b) => a.concat(b), []);
|
||||||
const versionedFiles = await globby(versionedGlob, {
|
const versionedFiles = await globby(versionedGlob, {
|
||||||
cwd: versionedDir,
|
cwd: versionedDir,
|
||||||
});
|
});
|
||||||
|
@ -338,7 +340,7 @@ export default function pluginContentDocs(
|
||||||
// If versioning is enabled, we cleverly chunk the generated routes
|
// If versioning is enabled, we cleverly chunk the generated routes
|
||||||
// to be by version and pick only needed base metadata.
|
// to be by version and pick only needed base metadata.
|
||||||
if (versioning.enabled) {
|
if (versioning.enabled) {
|
||||||
const docsMetadataByVersion = _.groupBy(
|
const docsMetadataByVersion = groupBy(
|
||||||
Object.values(content.docsMetadata),
|
Object.values(content.docsMetadata),
|
||||||
'version',
|
'version',
|
||||||
);
|
);
|
||||||
|
@ -358,13 +360,12 @@ export default function pluginContentDocs(
|
||||||
const neededSidebars: Set<string> =
|
const neededSidebars: Set<string> =
|
||||||
content.versionToSidebars[version] || new Set();
|
content.versionToSidebars[version] || new Set();
|
||||||
const docsBaseMetadata: DocsBaseMetadata = {
|
const docsBaseMetadata: DocsBaseMetadata = {
|
||||||
docsSidebars: _.pick(
|
docsSidebars: pick(
|
||||||
content.docsSidebars,
|
content.docsSidebars,
|
||||||
Array.from(neededSidebars),
|
Array.from(neededSidebars),
|
||||||
),
|
),
|
||||||
permalinkToSidebar: _.pickBy(
|
permalinkToSidebar: pickBy(content.permalinkToSidebar, sidebar =>
|
||||||
content.permalinkToSidebar,
|
neededSidebars.has(sidebar),
|
||||||
sidebar => neededSidebars.has(sidebar),
|
|
||||||
),
|
),
|
||||||
version,
|
version,
|
||||||
};
|
};
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
* LICENSE file in the root directory of this source tree.
|
* LICENSE file in the root directory of this source tree.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import _ from 'lodash';
|
import flatMap from 'lodash.flatmap';
|
||||||
import fs from 'fs-extra';
|
import fs from 'fs-extra';
|
||||||
import importFresh from 'import-fresh';
|
import importFresh from 'import-fresh';
|
||||||
import {
|
import {
|
||||||
|
@ -106,12 +106,12 @@ function normalizeItem(item: SidebarItemRaw): SidebarItem[] {
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
if (isCategoryShorthand(item)) {
|
if (isCategoryShorthand(item)) {
|
||||||
return _.flatMap(normalizeCategoryShorthand(item), normalizeItem);
|
return flatMap(normalizeCategoryShorthand(item), normalizeItem);
|
||||||
}
|
}
|
||||||
switch (item.type) {
|
switch (item.type) {
|
||||||
case 'category':
|
case 'category':
|
||||||
assertIsCategory(item);
|
assertIsCategory(item);
|
||||||
return [{...item, items: _.flatMap(item.items, normalizeItem)}];
|
return [{...item, items: flatMap(item.items, normalizeItem)}];
|
||||||
case 'link':
|
case 'link':
|
||||||
assertIsLink(item);
|
assertIsLink(item);
|
||||||
return [item];
|
return [item];
|
||||||
|
@ -134,7 +134,7 @@ function normalizeSidebar(sidebars: SidebarRaw): Sidebar {
|
||||||
? sidebar
|
? sidebar
|
||||||
: normalizeCategoryShorthand(sidebar);
|
: normalizeCategoryShorthand(sidebar);
|
||||||
|
|
||||||
acc[sidebarId] = _.flatMap(normalizedSidebar, normalizeItem);
|
acc[sidebarId] = flatMap(normalizedSidebar, normalizeItem);
|
||||||
|
|
||||||
return acc;
|
return acc;
|
||||||
},
|
},
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue