Skip to content

Commit

Permalink
feat(pages): add support for last update date & author
Browse files Browse the repository at this point in the history
  • Loading branch information
OzakIOne committed Apr 9, 2024
1 parent 73016d4 commit 426b8fc
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
28 changes: 22 additions & 6 deletions packages/docusaurus-plugin-content-pages/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
parseMarkdownFile,
isUnlisted,
isDraft,
readLastUpdateData,
} from '@docusaurus/utils';
import {validatePageFrontMatter} from './frontMatter';
import type {LoadContext, Plugin, RouteMetadata} from '@docusaurus/types';
Expand Down Expand Up @@ -159,12 +160,27 @@ export default function pluginContentPages(

const {addRoute, createData} = actions;

function createPageRouteMetadata(metadata: Metadata): RouteMetadata {
async function createPageRouteMetadata(
metadata: Metadata,
): Promise<RouteMetadata> {
if (metadata.type === 'mdx') {
const lastUpdate = await readLastUpdateData(
metadata.source,
options,
metadata.frontMatter.last_update,
);

return {
sourceFilePath: aliasedSitePathToRelativePath(metadata.source),
// TODO add support for last updated date in the page plugin
// at least for Markdown files
// lastUpdatedAt: metadata.lastUpdatedAt,
lastUpdatedAt: lastUpdate.lastUpdatedAt,
};
}

return {
sourceFilePath: aliasedSitePathToRelativePath(metadata.source),
// TODO add support for last updated date in the page plugin
// at least for Markdown files
// lastUpdatedAt: metadata.lastUpdatedAt,
lastUpdatedAt: undefined,
};
}
Expand All @@ -184,7 +200,7 @@ export default function pluginContentPages(
path: permalink,
component: options.mdxPageComponent,
exact: true,
metadata: routeMetadata,
metadata: await routeMetadata,
modules: {
content: source,
},
Expand All @@ -194,7 +210,7 @@ export default function pluginContentPages(
path: permalink,
component: source,
exact: true,
metadata: routeMetadata,
metadata: await routeMetadata,
modules: {
config: `@generated/docusaurus.config`,
},
Expand Down
6 changes: 6 additions & 0 deletions packages/docusaurus-plugin-content-pages/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export const DEFAULT_OPTIONS: PluginOptions = {
beforeDefaultRehypePlugins: [],
beforeDefaultRemarkPlugins: [],
admonitions: true,
showLastUpdateTime: false,
showLastUpdateAuthor: false,
};

const PluginOptionSchema = Joi.object<PluginOptions>({
Expand All @@ -44,6 +46,10 @@ const PluginOptionSchema = Joi.object<PluginOptions>({
DEFAULT_OPTIONS.beforeDefaultRemarkPlugins,
),
admonitions: AdmonitionsSchema.default(DEFAULT_OPTIONS.admonitions),
showLastUpdateTime: Joi.bool().default(DEFAULT_OPTIONS.showLastUpdateTime),
showLastUpdateAuthor: Joi.bool().default(
DEFAULT_OPTIONS.showLastUpdateAuthor,
),
});

export function validateOptions({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
declare module '@docusaurus/plugin-content-pages' {
import type {MDXOptions} from '@docusaurus/mdx-loader';
import type {LoadContext, Plugin} from '@docusaurus/types';
import type {FrontMatterLastUpdate} from '@docusaurus/utils';

export type Assets = {
image?: string;
Expand All @@ -20,6 +21,8 @@ declare module '@docusaurus/plugin-content-pages' {
include: string[];
exclude: string[];
mdxPageComponent: string;
showLastUpdateTime: boolean;
showLastUpdateAuthor: boolean;
};

export type Options = Partial<PluginOptions>;
Expand All @@ -35,6 +38,7 @@ declare module '@docusaurus/plugin-content-pages' {
readonly toc_max_heading_level?: number;
readonly draft?: boolean;
readonly unlisted?: boolean;
readonly last_update?: FrontMatterLastUpdate;
};

export type JSXPageMetadata = {
Expand Down
6 changes: 6 additions & 0 deletions packages/docusaurus-theme-classic/src/theme/MDXPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export default function MDXPage(props: Props): JSX.Element {
keywords,
wrapperClassName,
hide_table_of_contents: hideTableOfContents,
last_update: lastUpdate,
author,
} = frontMatter;
const image = assets.image ?? frontMatter.image;

Expand Down Expand Up @@ -67,6 +69,10 @@ export default function MDXPage(props: Props): JSX.Element {
)}
</div>
</main>
<div>Author 1 : {author as string}</div>
<div>Author 2 : {lastUpdate?.author}</div>
<div>Date : {String(lastUpdate?.date)}</div>
<div>Json : {JSON.stringify(frontMatter)}</div>
</Layout>
</HtmlClassNameProvider>
);
Expand Down

0 comments on commit 426b8fc

Please sign in to comment.