Skip to content

Commit

Permalink
Add optional archived field name configuration to hide archived pages…
Browse files Browse the repository at this point in the history
… from the page tree
  • Loading branch information
djohalo2 committed Dec 11, 2024
1 parent e4a138c commit af322ee
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ export const pageTreeConfig: PageTreeConfig = {
apiVersion: '2023-12-08',
/* Optionally provide the field name of the title field of your page documents, to be used to generate a slug automatically for example. */
titleFieldName: 'title',
/* Optionally provide the field name of the archived field. If configured, it will hide pages from the tree if this field is set to true. */
archivedFieldName: 'isArchived',
/* Used for showing the full url for a document and linking to it. */
/* optional, otherwise the path is shown */
/* Optional, otherwise the path is shown */
baseUrl: 'https://example.com',
};
```
Expand Down
7 changes: 6 additions & 1 deletion src/helpers/page-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ const mapPageTreeItems = (
};

/**
* Provides draft and published status. Filters out duplicate pages with the same id and invalid pages.
* Provides draft and published status. Filters out duplicate pages with the same id, invalid and archived pages.
*/
const getPublishedAndDraftRawPageMetadata = (
config: PageTreeConfig,
Expand All @@ -119,6 +119,7 @@ const getPublishedAndDraftRawPageMetadata = (

return pages
.filter(page => isValidPage(config, page))
.filter(page => !isArchivedPage(config, page))
.filter(p => !draftPages[p._id]) // filter out published versions for pages which have a draft
.map(p => {
const isDraft = p._id.startsWith(DRAFTS_PREFIX);
Expand All @@ -141,3 +142,7 @@ const isValidPage = (config: PageTreeConfig, page: RawPageMetadata): boolean =>
}
return true;
};

const isArchivedPage = (config: PageTreeConfig, page: RawPageMetadata): boolean => {
return config.archivedFieldName ? page[config.archivedFieldName] === true : false;
};
1 change: 1 addition & 0 deletions src/queries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ export const rawPageMetadataFragment = (config: PageTreeConfig) => `
parent,
slug,
title,
${config.archivedFieldName ? `${config.archivedFieldName},` : ''}
${getLanguageFieldName(config) ?? ''}`;
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ export type PageTreeConfig = {
rootSchemaType: string;
/* All your page schema type names, e.g. ["homePage", "contentPage"] */
pageSchemaTypes: string[];
/* Field name of your page documents */
/* Field name of the title your page documents */
titleFieldName?: string;
/* Field name of the archived field. If configured, it will hide pages from the tree if this field is set to true */
archivedFieldName?: string;
/* Optionally specify which document types can be the parent of a document type */
allowedParents?: Record<string, string[]>;
/* Used for creating page link on the editor page */
Expand Down

0 comments on commit af322ee

Please sign in to comment.