Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(pages): add LastUpdateAuthor & LastUpdateTime & editUrl #10032

Merged
merged 20 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/docusaurus-plugin-content-pages/src/frontMatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
FrontMatterTOCHeadingLevels,
ContentVisibilitySchema,
URISchema,
FrontMatterLastUpdateSchema,
} from '@docusaurus/utils-validation';
import type {PageFrontMatter} from '@docusaurus/plugin-content-pages';

Expand All @@ -24,6 +25,7 @@ const PageFrontMatterSchema = Joi.object<PageFrontMatter>({
wrapperClassName: Joi.string(),
hide_table_of_contents: Joi.boolean(),
...FrontMatterTOCHeadingLevels,
last_update: FrontMatterLastUpdateSchema,
}).concat(ContentVisibilitySchema);

export function validatePageFrontMatter(frontMatter: {
Expand Down
25 changes: 20 additions & 5 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 @@ -120,6 +121,12 @@ export default function pluginContentPages(
});
const frontMatter = validatePageFrontMatter(unsafeFrontMatter);

const lastUpdatedData = await readLastUpdateData(
source,
options,
frontMatter.last_update,
);

if (isDraft({frontMatter})) {
return undefined;
}
Expand All @@ -132,6 +139,8 @@ export default function pluginContentPages(
title: frontMatter.title ?? contentTitle,
description: frontMatter.description ?? excerpt,
frontMatter,
lastUpdatedBy: lastUpdatedData.lastUpdatedBy,
lastUpdatedAt: lastUpdatedData.lastUpdatedAt,
unlisted,
};
}
Expand Down Expand Up @@ -159,20 +168,26 @@ export default function pluginContentPages(

const {addRoute, createData} = actions;

function createPageRouteMetadata(metadata: Metadata): RouteMetadata {
async function createPageRouteMetadata(
metadata: Metadata,
): Promise<RouteMetadata> {
if (metadata.type === 'mdx') {
return {
sourceFilePath: aliasedSitePathToRelativePath(metadata.source),
lastUpdatedAt: metadata.lastUpdatedAt,
};
}

OzakIOne marked this conversation as resolved.
Show resolved Hide resolved
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,
};
}

await Promise.all(
content.map(async (metadata) => {
const {permalink, source} = metadata;
const routeMetadata = createPageRouteMetadata(metadata);
const routeMetadata = await createPageRouteMetadata(metadata);
if (metadata.type === 'mdx') {
await createData(
// Note that this created data path must be in sync with
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, LastUpdateData} 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 All @@ -43,7 +47,7 @@ declare module '@docusaurus/plugin-content-pages' {
source: string;
};

export type MDXPageMetadata = {
export type MDXPageMetadata = LastUpdateData & {
type: 'mdx';
permalink: string;
source: string;
Expand Down
23 changes: 22 additions & 1 deletion packages/docusaurus-theme-classic/src/theme/MDXPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@ import TOC from '@theme/TOC';
import Unlisted from '@theme/Unlisted';
import type {Props} from '@theme/MDXPage';

import EditMetaRow from '@theme/EditMetaRow';
import styles from './styles.module.css';

export default function MDXPage(props: Props): JSX.Element {
const {content: MDXPageContent} = props;
const {
metadata: {title, description, frontMatter, unlisted},
metadata: {
title,
description,
frontMatter,
unlisted,
lastUpdatedBy,
lastUpdatedAt,
},
assets,
} = MDXPageContent;
const {
Expand All @@ -33,6 +41,8 @@ export default function MDXPage(props: Props): JSX.Element {
} = frontMatter;
const image = assets.image ?? frontMatter.image;

const canDisplayEditMetaRow = !!(lastUpdatedAt || lastUpdatedBy);

return (
<HtmlClassNameProvider
className={clsx(
Expand Down Expand Up @@ -66,6 +76,17 @@ export default function MDXPage(props: Props): JSX.Element {
</div>
)}
</div>
{canDisplayEditMetaRow && (
<EditMetaRow
className={clsx(
'margin-top--sm',
ThemeClassNames.blog.blogFooterEditMetaRow,
OzakIOne marked this conversation as resolved.
Show resolved Hide resolved
)}
editUrl=""
lastUpdatedAt={lastUpdatedAt}
lastUpdatedBy={lastUpdatedBy}
/>
)}
</main>
</Layout>
</HtmlClassNameProvider>
Expand Down
1 change: 1 addition & 0 deletions project-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ opensearch
opensearchdescription
opensource
optimizt
Orama
orama
Orta
orta
Expand Down
2 changes: 2 additions & 0 deletions website/docusaurus.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,8 @@ export default async function createConfigAsync() {
} satisfies BlogOptions,
pages: {
remarkPlugins: [npm2yarn],
showLastUpdateAuthor: true,
showLastUpdateTime: true,
} satisfies PageOptions,
theme: {
customCss: [
Expand Down
Loading