Skip to content

Commit

Permalink
refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena committed Apr 9, 2022
1 parent 42fd11d commit a4fc23d
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 17 deletions.
2 changes: 1 addition & 1 deletion packages/docusaurus-plugin-content-docs/src/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export async function readVersionDocs(

export type DocEnv = 'production' | 'development';

// docs with draft front matter are only considered draft in production
/** Docs with draft front matter are only considered draft in production. */
function isDraftForEnvironment({
env,
frontMatter,
Expand Down
4 changes: 2 additions & 2 deletions packages/docusaurus-plugin-content-docs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ import type {
} from '@docusaurus/plugin-content-docs';
import {createSidebarsUtils} from './sidebars/utils';
import {getCategoryGeneratedIndexMetadataList} from './categoryGeneratedIndex';
import {partition} from 'lodash';
import _ from 'lodash';

export default async function pluginContentDocs(
context: LoadContext,
Expand Down Expand Up @@ -160,7 +160,7 @@ export default async function pluginContentDocs(
versionMetadata,
);

const [drafts, docs] = partition(docsBase, (doc) => doc.draft);
const [drafts, docs] = _.partition(docsBase, (doc) => doc.draft);

const sidebars = await loadSidebars(versionMetadata.sidebarFilePath, {
sidebarItemsGenerator: options.sidebarItemsGenerator,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,7 @@ declare module '@docusaurus/plugin-content-docs' {
/** Full URL to this doc, with base URL and version path. */
permalink: string;
/**
* Tells if this doc is a draft and should be filtered
* for current environment
* Draft docs will be excluded for production environment.
*/
draft: boolean;
/**
Expand Down Expand Up @@ -641,8 +640,10 @@ declare module '@docusaurus/plugin-content-docs/client' {
label: string;
isLast: boolean;
path: string;
mainDocId: string; // home doc (if docs homepage configured), or first doc
/** Home doc (if docs homepage configured), or first doc. */
mainDocId: string;
docs: GlobalDoc[];
/** Unversioned IDs. In development, this list is empty. */
draftIds: string[];
sidebars?: {[sidebarId: string]: GlobalSidebar};
};
Expand Down
15 changes: 4 additions & 11 deletions packages/docusaurus-plugin-content-docs/src/sidebars/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,26 +82,19 @@ async function processSidebar(
return processItems(generatedItems);
}

const allDraftIdsSet = new Set(drafts.flatMap(getDocIds));
const draftIds = new Set(drafts.flatMap(getDocIds));

const isDraftItem = (item: NormalizedSidebarItem): boolean => {
if (item.type === 'doc' || item.type === 'ref') {
return allDraftIdsSet.has(item.id);
return draftIds.has(item.id);
}
// if a category only contains draft items => it's a draft category
// If a category only contains draft items, it should be filtered entirely.
if (item.type === 'category') {
return item.items.every(isDraftItem);
}
return false;
};

function itemFilter(item: NormalizedSidebarItem): boolean {
if (isDraftItem(item)) {
return false;
}
return true;
}

async function processItem(
item: NormalizedSidebarItem,
): Promise<ProcessedSidebarItem[]> {
Expand All @@ -123,7 +116,7 @@ async function processSidebar(
items: NormalizedSidebarItem[],
): Promise<ProcessedSidebarItem[]> {
return (
await Promise.all(items.filter(itemFilter).map(processItem))
await Promise.all(items.filter((i) => !isDraftItem(i)).map(processItem))
).flat();
}

Expand Down

0 comments on commit a4fc23d

Please sign in to comment.