Skip to content

Commit

Permalink
refactor with set
Browse files Browse the repository at this point in the history
  • Loading branch information
endiliey committed Nov 16, 2019
1 parent 5ac4aef commit 2568db0
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,20 @@ Object {
`;

exports[`versioned website content 2`] = `
Object {
"1.0.0": Set {
"version-1.0.0/docs",
},
"1.0.1": Set {
"version-1.0.1/docs",
},
"next": Set {
"docs",
},
}
`;

exports[`versioned website content 3`] = `
Array [
Object {
"component": "@theme/DocPage",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ describe('simple website', () => {

test('content', async () => {
const content = await plugin.loadContent();
const {docsMetadata, docsSidebars} = content;
const {docsMetadata, docsSidebars, versionToSidebars} = content;
expect(versionToSidebars).toEqual({});
expect(docsMetadata.hello).toEqual({
id: 'hello',
permalink: '/docs/hello',
Expand Down Expand Up @@ -232,7 +233,7 @@ describe('versioned website', () => {

test('content', async () => {
const content = await plugin.loadContent();
const {docsMetadata, docsSidebars} = content;
const {docsMetadata, docsSidebars, versionToSidebars} = content;

// foo/baz.md only exists in version -1.0.0
expect(docsMetadata['foo/baz']).toBeUndefined();
Expand Down Expand Up @@ -307,6 +308,7 @@ describe('versioned website', () => {
});

expect(docsSidebars).toMatchSnapshot();
expect(versionToSidebars).toMatchSnapshot();
const routeConfigs = [];
const actions = createFakeActions(routeConfigs, pluginContentDir);
await plugin.contentLoaded({
Expand Down
35 changes: 17 additions & 18 deletions packages/docusaurus-plugin-content-docs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
MetadataRaw,
DocsMetadataRaw,
Metadata,
PermalinkToVersion,
VersionToSidebars,
} from './types';
import {Configuration} from 'webpack';

Expand All @@ -59,7 +59,7 @@ export default function pluginContentDocs(
const {siteDir, generatedFilesDir, baseUrl} = context;
const docsDir = path.resolve(siteDir, options.path);
const sourceToPermalink: SourceToPermalink = {};
const permalinkToVersion: PermalinkToVersion = {};

const dataDir = path.join(
generatedFilesDir,
'docusaurus-plugin-content-docs',
Expand Down Expand Up @@ -169,6 +169,7 @@ export default function pluginContentDocs(
// Construct inter-metadata relationship in docsMetadata
const docsMetadata: DocsMetadata = {};
const permalinkToSidebar: PermalinkToSidebar = {};
const versionToSidebars: VersionToSidebars = {};
Object.keys(docsMetadataRaw).forEach(currentID => {
const {next: nextID, previous: previousID, sidebar} =
order[currentID] || {};
Expand Down Expand Up @@ -196,9 +197,12 @@ export default function pluginContentDocs(
sourceToPermalink[source] = permalink;
if (sidebar) {
permalinkToSidebar[permalink] = sidebar;
}
if (version && versioning.enabled) {
permalinkToVersion[permalink] = version;
if (versioning.enabled && version) {
if (!versionToSidebars[version]) {
versionToSidebars[version] = new Set();
}
versionToSidebars[version].add(sidebar);
}
}
});

Expand Down Expand Up @@ -255,6 +259,7 @@ export default function pluginContentDocs(
docsDir,
docsSidebars,
permalinkToSidebar: objectWithKeySorted(permalinkToSidebar),
versionToSidebars,
};
},

Expand Down Expand Up @@ -335,23 +340,17 @@ export default function pluginContentDocs(
isLatestVersion ? '' : version,
]);
const docsBaseRoute = normalizeUrl([docsBasePermalink, ':route']);
const pickedSidebarsKeys: Set<string> = new Set();
const pickedPermalinkToSidebar: PermalinkToSidebar = _.pickBy(
content.permalinkToSidebar,
(sidebar, permalink) => {
const isPartOfRoute = permalinkToVersion[permalink] === version;
if (isPartOfRoute) {
pickedSidebarsKeys.add(sidebar);
}
return isPartOfRoute;
},
);
const neededSidebars: Set<string> =
content.versionToSidebars[version] || new Set();
const docsBaseMetadata: DocsBaseMetadata = {
docsSidebars: _.pick(
content.docsSidebars,
Array.from(pickedSidebarsKeys),
Array.from(neededSidebars),
),
permalinkToSidebar: _.pickBy(
content.permalinkToSidebar,
sidebar => neededSidebars.has(sidebar),
),
permalinkToSidebar: pickedPermalinkToSidebar,
version,
};

Expand Down
5 changes: 3 additions & 2 deletions packages/docusaurus-plugin-content-docs/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,16 @@ export interface PermalinkToSidebar {
[permalink: string]: string;
}

export interface PermalinkToVersion {
[permalink: string]: string;
export interface VersionToSidebars {
[version: string]: Set<string>;
}

export interface LoadedContent {
docsMetadata: DocsMetadata;
docsDir: string;
docsSidebars: Sidebar;
permalinkToSidebar: PermalinkToSidebar;
versionToSidebars: VersionToSidebars;
}

export type DocsBaseMetadata = Pick<
Expand Down

0 comments on commit 2568db0

Please sign in to comment.