From 8d86d85dc418df7eef45b593e68d965724c702db Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Wed, 6 Apr 2022 13:42:30 +0800 Subject: [PATCH 1/6] refactor(content-{blog,docs}): unify handling of tags --- .../src/blogUtils.ts | 2 +- .../src/index.ts | 91 +++++++++---------- .../src/plugin-content-blog.d.ts | 23 ++--- .../src/types.ts | 13 +-- .../src/index.ts | 23 +++-- .../src/plugin-content-docs.d.ts | 26 ++---- .../src/props.ts | 8 +- .../src/types.ts | 5 +- .../src/theme-classic.d.ts | 4 +- .../src/theme/BlogTagsListPage/index.tsx | 5 +- .../src/theme/BlogTagsPostsPage/index.tsx | 12 ++- .../src/theme/DocTagDocListPage/index.tsx | 7 +- .../src/theme/Tag/index.tsx | 6 +- .../src/theme/TagsListInline/index.tsx | 2 +- packages/docusaurus-theme-common/src/index.ts | 1 - .../src/utils/tagsUtils.ts | 13 +-- packages/docusaurus-types/src/index.d.ts | 19 ++++ .../src/validationSchemas.ts | 2 +- packages/docusaurus-utils/src/index.ts | 1 - packages/docusaurus-utils/src/tags.ts | 7 +- 20 files changed, 126 insertions(+), 144 deletions(-) diff --git a/packages/docusaurus-plugin-content-blog/src/blogUtils.ts b/packages/docusaurus-plugin-content-blog/src/blogUtils.ts index b1823bb6d68d..8138d1ab8b00 100644 --- a/packages/docusaurus-plugin-content-blog/src/blogUtils.ts +++ b/packages/docusaurus-plugin-content-blog/src/blogUtils.ts @@ -114,7 +114,7 @@ export function getBlogTags({ ); return _.mapValues(groups, ({tag, items: tagBlogPosts}) => ({ - name: tag.label, + label: tag.label, items: tagBlogPosts.map((item) => item.id), permalink: tag.permalink, pages: paginateBlogPosts({ diff --git a/packages/docusaurus-plugin-content-blog/src/index.ts b/packages/docusaurus-plugin-content-blog/src/index.ts index 3414d2820a07..f5e22f5f6a37 100644 --- a/packages/docusaurus-plugin-content-blog/src/index.ts +++ b/packages/docusaurus-plugin-content-blog/src/index.ts @@ -30,7 +30,13 @@ import type { BlogContentPaths, BlogMarkdownLoaderOptions, } from './types'; -import type {LoadContext, Plugin, HtmlTags} from '@docusaurus/types'; +import type { + LoadContext, + Plugin, + HtmlTags, + TagsListItem, + TagModule, +} from '@docusaurus/types'; import { generateBlogPosts, getSourceToPermalink, @@ -43,7 +49,6 @@ import type { BlogPostFrontMatter, BlogPostMetadata, Assets, - TagModule, } from '@docusaurus/plugin-content-blog'; export default async function pluginContentBlog( @@ -117,6 +122,8 @@ export default async function pluginContentBlog( blogSidebarTitle, } = options; + const baseBlogUrl = normalizeUrl([baseUrl, routeBasePath]); + const blogTagsListPath = normalizeUrl([baseBlogUrl, tagsBasePath]); const blogPosts = await generateBlogPosts(contentPaths, context, options); if (!blogPosts.length) { @@ -125,7 +132,7 @@ export default async function pluginContentBlog( blogPosts: [], blogListPaginated: [], blogTags: {}, - blogTagsListPath: null, + blogTagsListPath, blogTagsPaginated: [], }; } @@ -150,8 +157,6 @@ export default async function pluginContentBlog( } }); - const baseBlogUrl = normalizeUrl([baseUrl, routeBasePath]); - const blogListPaginated: BlogPaginated[] = paginateBlogPosts({ blogPosts, blogTitle, @@ -167,11 +172,6 @@ export default async function pluginContentBlog( blogTitle, }); - const tagsPath = normalizeUrl([baseBlogUrl, tagsBasePath]); - - const blogTagsListPath = - Object.keys(blogTags).length > 0 ? tagsPath : null; - return { blogSidebarTitle, blogPosts, @@ -307,30 +307,46 @@ export default async function pluginContentBlog( }), ); - // Tags. - if (blogTagsListPath === null) { + // Tags. This is the last part so we early-return if there are no tags. + if (Object.keys(blogTags).length === 0) { return; } - const tagsModule: {[tagName: string]: TagModule} = Object.fromEntries( - Object.entries(blogTags).map(([, tag]) => { - const tagModule: TagModule = { - allTagsPath: blogTagsListPath, - name: tag.name, - count: tag.items.length, - permalink: tag.permalink, - }; - return [tag.name, tagModule]; - }), - ); + async function createTagsListPage() { + const tagsProp: TagsListItem[] = Object.values(blogTags).map((tag) => ({ + label: tag.label, + permalink: tag.permalink, + count: tag.items.length, + })); - async function createTagRoutes(tag: BlogTag): Promise { + const tagsPropPath = await createData( + `${docuHash(`${blogTagsListPath}-tags`)}.json`, + JSON.stringify(tagsProp, null, 2), + ); + + addRoute({ + path: blogTagsListPath, + component: blogTagsListComponent, + exact: true, + modules: { + sidebar: aliasedSource(sidebarProp), + tags: aliasedSource(tagsPropPath), + }, + }); + } + + async function createTagPostsListPage(tag: BlogTag): Promise { await Promise.all( tag.pages.map(async (blogPaginated) => { const {metadata, items} = blogPaginated; - const tagsMetadataPath = await createData( + const tagProp: TagModule = { + label: tag.label, + permalink: tag.permalink, + allTagsPath: blogTagsListPath, + }; + const tagPropPath = await createData( `${docuHash(metadata.permalink)}.json`, - JSON.stringify(tagsModule[tag.name], null, 2), + JSON.stringify(tagProp, null, 2), ); const listMetadataPath = await createData( @@ -356,7 +372,7 @@ export default async function pluginContentBlog( }, }; }), - metadata: aliasedSource(tagsMetadataPath), + tag: aliasedSource(tagPropPath), listMetadata: aliasedSource(listMetadataPath), }, }); @@ -364,25 +380,8 @@ export default async function pluginContentBlog( ); } - await Promise.all(Object.values(blogTags).map(createTagRoutes)); - - // Only create /tags page if there are tags. - if (Object.keys(blogTags).length > 0) { - const tagsListPath = await createData( - `${docuHash(`${blogTagsListPath}-tags`)}.json`, - JSON.stringify(tagsModule, null, 2), - ); - - addRoute({ - path: blogTagsListPath, - component: blogTagsListComponent, - exact: true, - modules: { - sidebar: aliasedSource(sidebarProp), - tags: aliasedSource(tagsListPath), - }, - }); - } + await createTagsListPage(); + await Promise.all(Object.values(blogTags).map(createTagPostsListPage)); }, translateContent({content, translationFiles}) { diff --git a/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts b/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts index 1733bff9bc16..24ba4b4ef0a4 100644 --- a/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts +++ b/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts @@ -406,17 +406,6 @@ declare module '@docusaurus/plugin-content-blog' { } >; - export type TagModule = { - /** Permalink of the tag's own page. */ - permalink: string; - /** Name of the tag. */ - name: string; - /** Number of posts with this tag. */ - count: number; - /** The tags list page. */ - allTagsPath: string; - }; - export type BlogSidebar = { title: string; items: {title: string; permalink: string}[]; @@ -511,28 +500,30 @@ declare module '@theme/BlogListPage' { } declare module '@theme/BlogTagsListPage' { - import type {BlogSidebar, TagModule} from '@docusaurus/plugin-content-blog'; + import type {BlogSidebar} from '@docusaurus/plugin-content-blog'; + import type {TagsListItem} from '@docusaurus/types'; export interface Props { /** Blog sidebar. */ readonly sidebar: BlogSidebar; - /** A map from tag names to the full tag module. */ - readonly tags: Readonly<{[tagName: string]: TagModule}>; + /** All tags declared in this blog. */ + readonly tags: TagsListItem[]; } export default function BlogTagsListPage(props: Props): JSX.Element; } declare module '@theme/BlogTagsPostsPage' { - import type {BlogSidebar, TagModule} from '@docusaurus/plugin-content-blog'; + import type {BlogSidebar} from '@docusaurus/plugin-content-blog'; import type {Content} from '@theme/BlogPostPage'; import type {Metadata} from '@theme/BlogListPage'; + import type {TagModule} from '@docusaurus/types'; export interface Props { /** Blog sidebar. */ readonly sidebar: BlogSidebar; /** Metadata of this tag. */ - readonly metadata: TagModule; + readonly tag: TagModule; /** Looks exactly the same as the posts list page */ readonly listMetadata: Metadata; /** diff --git a/packages/docusaurus-plugin-content-blog/src/types.ts b/packages/docusaurus-plugin-content-blog/src/types.ts index 220ddbf1d579..84f76fa92753 100644 --- a/packages/docusaurus-plugin-content-blog/src/types.ts +++ b/packages/docusaurus-plugin-content-blog/src/types.ts @@ -6,6 +6,7 @@ */ import type {BrokenMarkdownLink, ContentPaths} from '@docusaurus/utils'; +import type {Tag} from '@docusaurus/types'; import type {BlogPostMetadata} from '@docusaurus/plugin-content-blog'; import type {Metadata as BlogPaginatedMetadata} from '@theme/BlogListPage'; @@ -16,22 +17,16 @@ export type BlogContent = { blogPosts: BlogPost[]; blogListPaginated: BlogPaginated[]; blogTags: BlogTags; - blogTagsListPath: string | null; + blogTagsListPath: string; }; export type BlogTags = { - // TODO, the key is the tag slug/permalink - // This is due to legacy frontmatter: tags: - // [{label: "xyz", permalink: "/1"}, {label: "xyz", permalink: "/2"}] - // Soon we should forbid declaring permalink through frontmatter - [tagKey: string]: BlogTag; + [permalink: string]: BlogTag; }; -export type BlogTag = { - name: string; +export type BlogTag = Tag & { /** Blog post permalinks. */ items: string[]; - permalink: string; pages: BlogPaginated[]; }; diff --git a/packages/docusaurus-plugin-content-docs/src/index.ts b/packages/docusaurus-plugin-content-docs/src/index.ts index a0dc21768960..5a8e08ad64b4 100644 --- a/packages/docusaurus-plugin-content-docs/src/index.ts +++ b/packages/docusaurus-plugin-content-docs/src/index.ts @@ -19,7 +19,7 @@ import { createSlugger, DEFAULT_PLUGIN_ID, } from '@docusaurus/utils'; -import type {LoadContext, Plugin} from '@docusaurus/types'; +import type {LoadContext, Plugin, TagsListItem} from '@docusaurus/types'; import {loadSidebars} from './sidebars'; import {CategoryMetadataFilenamePattern} from './sidebars/generator'; import { @@ -50,7 +50,6 @@ import logger from '@docusaurus/logger'; import {getVersionTags} from './tags'; import {createVersionRoutes} from './routes'; import type { - PropTagsListPage, PluginOptions, DocMetadataBase, VersionMetadata, @@ -225,16 +224,16 @@ export default async function pluginContentDocs( // TODO tags should be a sub route of the version route async function createTagsListPage() { - const tagsProp: PropTagsListPage['tags'] = Object.values( - versionTags, - ).map((tagValue) => ({ - name: tagValue.label, - permalink: tagValue.permalink, - count: tagValue.docIds.length, - })); + const tagsProp: TagsListItem[] = Object.values(versionTags).map( + (tag) => ({ + label: tag.label, + permalink: tag.permalink, + count: tag.docIds.length, + }), + ); // Only create /tags page if there are tags. - if (Object.keys(tagsProp).length > 0) { + if (tagsProp.length > 0) { const tagsPropPath = await createData( `${docuHash(`tags-list-${version.versionName}-prop`)}.json`, JSON.stringify(tagsProp, null, 2), @@ -252,14 +251,14 @@ export default async function pluginContentDocs( // TODO tags should be a sub route of the version route async function createTagDocListPage(tag: VersionTag) { - const tagProps = toTagDocListProp({ + const tagItemsProp = toTagDocListProp({ allTagsPath: version.tagsPath, tag, docs: version.docs, }); const tagPropPath = await createData( `${docuHash(`tag-${tag.permalink}`)}.json`, - JSON.stringify(tagProps, null, 2), + JSON.stringify(tagItemsProp, null, 2), ); addRoute({ path: tag.permalink, diff --git a/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts b/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts index 271bfa4923db..cee80a460a43 100644 --- a/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts +++ b/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts @@ -8,6 +8,7 @@ declare module '@docusaurus/plugin-content-docs' { import type {MDXOptions} from '@docusaurus/mdx-loader'; import type {ContentPaths, Tag, FrontMatterTag} from '@docusaurus/utils'; + import type {TagsListItem, TagModule} from '@docusaurus/types'; import type {Required} from 'utility-types'; export type Assets = { @@ -483,25 +484,16 @@ declare module '@docusaurus/plugin-content-docs' { export type PropSidebar = import('./sidebars/types').PropSidebar; export type PropSidebars = import('./sidebars/types').PropSidebars; - export type PropTagDocListDoc = { - id: string; - title: string; - description: string; - permalink: string; - }; + export type PropTagDocListDoc = Pick< + DocMetadata, + 'id' | 'title' | 'description' | 'permalink' + >; export type PropTagDocList = { - allTagsPath: string; - name: string; // normalized name/label of the tag - permalink: string; // pathname of the tag - docs: PropTagDocListDoc[]; + tag: TagModule & {items: PropTagDocListDoc[]}; }; export type PropTagsListPage = { - tags: { - name: string; - permalink: string; - count: number; - }[]; + tags: TagsListItem[]; }; } @@ -559,9 +551,7 @@ declare module '@theme/DocTagsListPage' { declare module '@theme/DocTagDocListPage' { import type {PropTagDocList} from '@docusaurus/plugin-content-docs'; - export interface Props { - readonly tag: PropTagDocList; - } + export interface Props extends PropTagDocList {} export default function DocTagDocListPage(props: Props): JSX.Element; } diff --git a/packages/docusaurus-plugin-content-docs/src/props.ts b/packages/docusaurus-plugin-content-docs/src/props.ts index d25888db63a8..370c297b99d0 100644 --- a/packages/docusaurus-plugin-content-docs/src/props.ts +++ b/packages/docusaurus-plugin-content-docs/src/props.ts @@ -137,8 +137,8 @@ export function toTagDocListProp({ }: { allTagsPath: string; tag: VersionTag; - docs: Pick[]; -}): PropTagDocList { + docs: DocMetadata[]; +}): PropTagDocList['tag'] { function toDocListProp(): PropTagDocListDoc[] { const list = _.compact( tag.docIds.map((id) => docs.find((doc) => doc.id === id)), @@ -154,9 +154,9 @@ export function toTagDocListProp({ } return { - name: tag.label, + label: tag.label, permalink: tag.permalink, - docs: toDocListProp(), allTagsPath, + items: toDocListProp(), }; } diff --git a/packages/docusaurus-plugin-content-docs/src/types.ts b/packages/docusaurus-plugin-content-docs/src/types.ts index 909a200da7e4..bb9f8e8e15d1 100644 --- a/packages/docusaurus-plugin-content-docs/src/types.ts +++ b/packages/docusaurus-plugin-content-docs/src/types.ts @@ -8,13 +8,14 @@ /// import type {Sidebars} from './sidebars/types'; -import type {BrokenMarkdownLink, Tag} from '@docusaurus/utils'; +import type {BrokenMarkdownLink} from '@docusaurus/utils'; import type { VersionMetadata, LastUpdateData, DocMetadata, CategoryGeneratedIndexMetadata, } from '@docusaurus/plugin-content-docs'; +import type {Tag} from '@docusaurus/types'; export type DocFile = { contentPath: string; // /!\ may be localized @@ -33,7 +34,7 @@ export type VersionTag = Tag & { docIds: string[]; }; export type VersionTags = { - [key: string]: VersionTag; + [permalink: string]: VersionTag; }; export type LoadedVersion = VersionMetadata & { diff --git a/packages/docusaurus-theme-classic/src/theme-classic.d.ts b/packages/docusaurus-theme-classic/src/theme-classic.d.ts index 9f3c48ed3da0..454653583f7d 100644 --- a/packages/docusaurus-theme-classic/src/theme-classic.d.ts +++ b/packages/docusaurus-theme-classic/src/theme-classic.d.ts @@ -1010,7 +1010,7 @@ declare module '@theme/IconExternalLink' { } declare module '@theme/TagsListByLetter' { - import type {TagsListItem} from '@docusaurus/theme-common'; + import type {TagsListItem} from '@docusaurus/types'; export interface Props { readonly tags: readonly TagsListItem[]; @@ -1019,7 +1019,7 @@ declare module '@theme/TagsListByLetter' { } declare module '@theme/TagsListInline' { - import type {Tag} from '@docusaurus/utils'; + import type {Tag} from '@docusaurus/types'; export interface Props { readonly tags: readonly Tag[]; diff --git a/packages/docusaurus-theme-classic/src/theme/BlogTagsListPage/index.tsx b/packages/docusaurus-theme-classic/src/theme/BlogTagsListPage/index.tsx index 1124e06aaa37..2f53b6ee41fa 100644 --- a/packages/docusaurus-theme-classic/src/theme/BlogTagsListPage/index.tsx +++ b/packages/docusaurus-theme-classic/src/theme/BlogTagsListPage/index.tsx @@ -19,8 +19,7 @@ import { import SearchMetadata from '../SearchMetadata'; import clsx from 'clsx'; -export default function BlogTagsListPage(props: Props): JSX.Element { - const {tags, sidebar} = props; +export default function BlogTagsListPage({tags, sidebar}: Props): JSX.Element { const title = translateTagsPageTitle(); return (

{title}

- +
); diff --git a/packages/docusaurus-theme-classic/src/theme/BlogTagsPostsPage/index.tsx b/packages/docusaurus-theme-classic/src/theme/BlogTagsPostsPage/index.tsx index fa3bf26a2437..ca3fb2272b3b 100644 --- a/packages/docusaurus-theme-classic/src/theme/BlogTagsPostsPage/index.tsx +++ b/packages/docusaurus-theme-classic/src/theme/BlogTagsPostsPage/index.tsx @@ -40,9 +40,13 @@ function useBlogPostsPlural() { ); } -export default function BlogTagsPostsPage(props: Props): JSX.Element { - const {metadata, items, sidebar, listMetadata} = props; - const {allTagsPath, name: tagName, count} = metadata; +export default function BlogTagsPostsPage({ + tag, + items, + sidebar, + listMetadata, +}: Props): JSX.Element { + const {allTagsPath, label: tagName} = tag; const blogPostsPlural = useBlogPostsPlural(); const title = translate( { @@ -50,7 +54,7 @@ export default function BlogTagsPostsPage(props: Props): JSX.Element { description: 'The title of the page for a blog tag', message: '{nPosts} tagged with "{tagName}"', }, - {nPosts: blogPostsPlural(count), tagName}, + {nPosts: blogPostsPlural(items.length), tagName}, ); return ( diff --git a/packages/docusaurus-theme-classic/src/theme/DocTagDocListPage/index.tsx b/packages/docusaurus-theme-classic/src/theme/DocTagDocListPage/index.tsx index 512eabc4a892..adabcfb283e5 100644 --- a/packages/docusaurus-theme-classic/src/theme/DocTagDocListPage/index.tsx +++ b/packages/docusaurus-theme-classic/src/theme/DocTagDocListPage/index.tsx @@ -15,7 +15,6 @@ import { ThemeClassNames, usePluralForm, } from '@docusaurus/theme-common'; -import type {PropTagDocListDoc} from '@docusaurus/plugin-content-docs'; import Translate, {translate} from '@docusaurus/Translate'; import type {Props} from '@theme/DocTagDocListPage'; import SearchMetadata from '@theme/SearchMetadata'; @@ -39,7 +38,7 @@ function useNDocsTaggedPlural() { ); } -function DocItem({doc}: {doc: PropTagDocListDoc}): JSX.Element { +function DocItem({doc}: {doc: Props['tag']['items'][number]}): JSX.Element { return (
@@ -58,7 +57,7 @@ export default function DocTagDocListPage({tag}: Props): JSX.Element { description: 'The title of the page for a docs tag', message: '{nDocsTagged} with "{tagName}"', }, - {nDocsTagged: nDocsTaggedPlural(tag.docs.length), tagName: tag.name}, + {nDocsTagged: nDocsTaggedPlural(tag.items.length), tagName: tag.label}, ); return ( @@ -84,7 +83,7 @@ export default function DocTagDocListPage({tag}: Props): JSX.Element {
- {tag.docs.map((doc) => ( + {tag.items.map((doc) => ( ))}
diff --git a/packages/docusaurus-theme-classic/src/theme/Tag/index.tsx b/packages/docusaurus-theme-classic/src/theme/Tag/index.tsx index ffe3ea717df3..3b04395d3954 100644 --- a/packages/docusaurus-theme-classic/src/theme/Tag/index.tsx +++ b/packages/docusaurus-theme-classic/src/theme/Tag/index.tsx @@ -12,9 +12,7 @@ import type {Props} from '@theme/Tag'; import styles from './styles.module.css'; -export default function Tag(props: Props): JSX.Element { - const {permalink, name, count} = props; - +export default function Tag({permalink, label, count}: Props): JSX.Element { return ( - {name} + {label} {count && {count}} ); diff --git a/packages/docusaurus-theme-classic/src/theme/TagsListInline/index.tsx b/packages/docusaurus-theme-classic/src/theme/TagsListInline/index.tsx index abf7bb497f78..b0b6fdaff5eb 100644 --- a/packages/docusaurus-theme-classic/src/theme/TagsListInline/index.tsx +++ b/packages/docusaurus-theme-classic/src/theme/TagsListInline/index.tsx @@ -26,7 +26,7 @@ export default function TagsListInline({tags}: Props): JSX.Element {
    {tags.map(({label, permalink: tagPermalink}) => (
  • - +
  • ))}
diff --git a/packages/docusaurus-theme-common/src/index.ts b/packages/docusaurus-theme-common/src/index.ts index 77afa2fdfd21..97b0de33a583 100644 --- a/packages/docusaurus-theme-common/src/index.ts +++ b/packages/docusaurus-theme-common/src/index.ts @@ -86,7 +86,6 @@ export { translateTagsPageTitle, listTagsByLetters, type TagLetterEntry, - type TagsListItem, } from './utils/tagsUtils'; export {useHistoryPopHandler} from './utils/historyUtils'; diff --git a/packages/docusaurus-theme-common/src/utils/tagsUtils.ts b/packages/docusaurus-theme-common/src/utils/tagsUtils.ts index 99dfdc9d379f..c29afab1cf65 100644 --- a/packages/docusaurus-theme-common/src/utils/tagsUtils.ts +++ b/packages/docusaurus-theme-common/src/utils/tagsUtils.ts @@ -6,6 +6,7 @@ */ import {translate} from '@docusaurus/Translate'; +import type {TagsListItem} from '@docusaurus/types'; export const translateTagsPageTitle = (): string => translate({ @@ -14,13 +15,7 @@ export const translateTagsPageTitle = (): string => description: 'The title of the tag list page', }); -export type TagsListItem = Readonly<{ - name: string; - permalink: string; - count: number; -}>; - -export type TagLetterEntry = Readonly<{letter: string; tags: TagsListItem[]}>; +export type TagLetterEntry = {letter: string; tags: TagsListItem[]}; function getTagLetter(tag: string): string { return tag[0]!.toUpperCase(); @@ -35,7 +30,7 @@ export function listTagsByLetters( ): TagLetterEntry[] { const groups: {[initial: string]: TagsListItem[]} = {}; Object.values(tags).forEach((tag) => { - const initial = getTagLetter(tag.name); + const initial = getTagLetter(tag.label); groups[initial] ??= []; groups[initial]!.push(tag); }); @@ -47,7 +42,7 @@ export function listTagsByLetters( .map(([letter, letterTags]) => { // Sort tags inside a letter const sortedTags = letterTags.sort((tag1, tag2) => - tag1.name.localeCompare(tag2.name), + tag1.label.localeCompare(tag2.label), ); return {letter, tags: sortedTags}; }) diff --git a/packages/docusaurus-types/src/index.d.ts b/packages/docusaurus-types/src/index.d.ts index 7d24b70a47e7..c1f0357dc237 100644 --- a/packages/docusaurus-types/src/index.d.ts +++ b/packages/docusaurus-types/src/index.d.ts @@ -588,3 +588,22 @@ export type ClientModule = { }) => void; onRouteUpdateDelayed?: (args: {location: Location}) => void; }; + +/** What the user configures. */ +export type Tag = { + label: string; + /** Permalink to this tag's page, without the `/tags/` base path. */ + permalink: string; +}; + +/** What the tags list page should know about each tag. */ +export type TagsListItem = Tag & { + /** Number of posts/docs with this tag. */ + count: number; +}; + +/** What the tag's own page should know about the tag. */ +export type TagModule = Tag & { + /** The tags list page's permalink. */ + allTagsPath: string; +}; diff --git a/packages/docusaurus-utils-validation/src/validationSchemas.ts b/packages/docusaurus-utils-validation/src/validationSchemas.ts index 4e6134a7b2a1..385efd4ff226 100644 --- a/packages/docusaurus-utils-validation/src/validationSchemas.ts +++ b/packages/docusaurus-utils-validation/src/validationSchemas.ts @@ -7,7 +7,7 @@ import Joi from './Joi'; import {isValidPathname, DEFAULT_PLUGIN_ID} from '@docusaurus/utils'; -import type {Tag} from '@docusaurus/utils'; +import type {Tag} from '@docusaurus/types'; import {JoiFrontMatter} from './JoiFrontMatter'; export const PluginIdSchema = Joi.string() diff --git a/packages/docusaurus-utils/src/index.ts b/packages/docusaurus-utils/src/index.ts index 7cd871a631e8..575f67e62920 100644 --- a/packages/docusaurus-utils/src/index.ts +++ b/packages/docusaurus-utils/src/index.ts @@ -56,7 +56,6 @@ export { buildSshUrl, } from './urlUtils'; export { - type Tag, type FrontMatterTag, normalizeFrontMatterTags, groupTaggedItems, diff --git a/packages/docusaurus-utils/src/tags.ts b/packages/docusaurus-utils/src/tags.ts index fa5eb4eebd1e..e3eb2933e6f0 100644 --- a/packages/docusaurus-utils/src/tags.ts +++ b/packages/docusaurus-utils/src/tags.ts @@ -7,12 +7,7 @@ import _ from 'lodash'; import {normalizeUrl} from './urlUtils'; - -export type Tag = { - label: string; - /** Permalink to this tag's page, without the `/tags/` base path. */ - permalink: string; -}; +import type {Tag} from '@docusaurus/types'; export type FrontMatterTag = string | Tag; From 8a321042e652d1a2fcb6e0bba33e6ac3c6ef52f0 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Wed, 6 Apr 2022 13:46:55 +0800 Subject: [PATCH 2/6] fix tests --- .../__snapshots__/index.test.ts.snap | 8 +-- .../__snapshots__/index.test.ts.snap | 60 +++++++++---------- .../src/__tests__/props.test.ts | 4 +- .../src/utils/__tests__/tagUtils.test.ts | 12 ++-- 4 files changed, 42 insertions(+), 42 deletions(-) diff --git a/packages/docusaurus-plugin-content-blog/src/__tests__/__snapshots__/index.test.ts.snap b/packages/docusaurus-plugin-content-blog/src/__tests__/__snapshots__/index.test.ts.snap index 1eab791d4450..c19351278b5f 100644 --- a/packages/docusaurus-plugin-content-blog/src/__tests__/__snapshots__/index.test.ts.snap +++ b/packages/docusaurus-plugin-content-blog/src/__tests__/__snapshots__/index.test.ts.snap @@ -8,7 +8,7 @@ exports[`blog plugin works on blog tags without pagination 1`] = ` "/another/tags", "/another/tags2", ], - "name": "tag1", + "label": "tag1", "pages": [ { "items": [ @@ -36,7 +36,7 @@ exports[`blog plugin works on blog tags without pagination 1`] = ` "/another/tags", "/another/tags2", ], - "name": "tag2", + "label": "tag2", "pages": [ { "items": [ @@ -69,7 +69,7 @@ exports[`blog plugin works with blog tags 1`] = ` "/another/tags", "/another/tags2", ], - "name": "tag1", + "label": "tag1", "pages": [ { "items": [ @@ -112,7 +112,7 @@ exports[`blog plugin works with blog tags 1`] = ` "/another/tags", "/another/tags2", ], - "name": "tag2", + "label": "tag2", "pages": [ { "items": [ diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap b/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap index 55f087ee4e7d..cd72aa678441 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap @@ -714,9 +714,10 @@ exports[`simple website content: data 1`] = ` } }", "tag-docs-tags-tag-1-b3f.json": "{ - \\"name\\": \\"tag 1\\", + \\"label\\": \\"tag 1\\", \\"permalink\\": \\"/docs/tags/tag-1\\", - \\"docs\\": [ + \\"allTagsPath\\": \\"/docs/tags\\", + \\"items\\": [ { \\"id\\": \\"foo/baz\\", \\"title\\": \\"baz\\", @@ -729,48 +730,47 @@ exports[`simple website content: data 1`] = ` \\"description\\": \\"Hi, Endilie here :)\\", \\"permalink\\": \\"/docs/\\" } - ], - \\"allTagsPath\\": \\"/docs/tags\\" + ] }", "tag-docs-tags-tag-2-custom-permalink-825.json": "{ - \\"name\\": \\"tag 2\\", + \\"label\\": \\"tag 2\\", \\"permalink\\": \\"/docs/tags/tag2-custom-permalink\\", - \\"docs\\": [ + \\"allTagsPath\\": \\"/docs/tags\\", + \\"items\\": [ { \\"id\\": \\"foo/baz\\", \\"title\\": \\"baz\\", \\"description\\": \\"Images\\", \\"permalink\\": \\"/docs/foo/bazSlug.html\\" } - ], - \\"allTagsPath\\": \\"/docs/tags\\" + ] }", "tag-docs-tags-tag-3-ab5.json": "{ - \\"name\\": \\"tag 3\\", + \\"label\\": \\"tag 3\\", \\"permalink\\": \\"/docs/tags/tag-3\\", - \\"docs\\": [ + \\"allTagsPath\\": \\"/docs/tags\\", + \\"items\\": [ { \\"id\\": \\"hello\\", \\"title\\": \\"Hello, World !\\", \\"description\\": \\"Hi, Endilie here :)\\", \\"permalink\\": \\"/docs/\\" } - ], - \\"allTagsPath\\": \\"/docs/tags\\" + ] }", "tags-list-current-prop-15a.json": "[ { - \\"name\\": \\"tag 1\\", + \\"label\\": \\"tag 1\\", \\"permalink\\": \\"/docs/tags/tag-1\\", \\"count\\": 2 }, { - \\"name\\": \\"tag 2\\", + \\"label\\": \\"tag 2\\", \\"permalink\\": \\"/docs/tags/tag2-custom-permalink\\", \\"count\\": 1 }, { - \\"name\\": \\"tag 3\\", + \\"label\\": \\"tag 3\\", \\"permalink\\": \\"/docs/tags/tag-3\\", \\"count\\": 1 } @@ -3172,57 +3172,57 @@ exports[`versioned website content: data 1`] = ` } }", "tag-docs-next-tags-bar-tag-1-a8f.json": "{ - \\"name\\": \\"barTag 1\\", + \\"label\\": \\"barTag 1\\", \\"permalink\\": \\"/docs/next/tags/bar-tag-1\\", - \\"docs\\": [ + \\"allTagsPath\\": \\"/docs/next/tags\\", + \\"items\\": [ { \\"id\\": \\"foo/bar\\", \\"title\\": \\"bar\\", \\"description\\": \\"This is next version of bar.\\", \\"permalink\\": \\"/docs/next/foo/barSlug\\" } - ], - \\"allTagsPath\\": \\"/docs/next/tags\\" + ] }", "tag-docs-next-tags-bar-tag-2-216.json": "{ - \\"name\\": \\"barTag-2\\", + \\"label\\": \\"barTag-2\\", \\"permalink\\": \\"/docs/next/tags/bar-tag-2\\", - \\"docs\\": [ + \\"allTagsPath\\": \\"/docs/next/tags\\", + \\"items\\": [ { \\"id\\": \\"foo/bar\\", \\"title\\": \\"bar\\", \\"description\\": \\"This is next version of bar.\\", \\"permalink\\": \\"/docs/next/foo/barSlug\\" } - ], - \\"allTagsPath\\": \\"/docs/next/tags\\" + ] }", "tag-docs-next-tags-bar-tag-3-permalink-94a.json": "{ - \\"name\\": \\"barTag 3\\", + \\"label\\": \\"barTag 3\\", \\"permalink\\": \\"/docs/next/tags/barTag-3-permalink\\", - \\"docs\\": [ + \\"allTagsPath\\": \\"/docs/next/tags\\", + \\"items\\": [ { \\"id\\": \\"foo/bar\\", \\"title\\": \\"bar\\", \\"description\\": \\"This is next version of bar.\\", \\"permalink\\": \\"/docs/next/foo/barSlug\\" } - ], - \\"allTagsPath\\": \\"/docs/next/tags\\" + ] }", "tags-list-current-prop-15a.json": "[ { - \\"name\\": \\"barTag 1\\", + \\"label\\": \\"barTag 1\\", \\"permalink\\": \\"/docs/next/tags/bar-tag-1\\", \\"count\\": 1 }, { - \\"name\\": \\"barTag-2\\", + \\"label\\": \\"barTag-2\\", \\"permalink\\": \\"/docs/next/tags/bar-tag-2\\", \\"count\\": 1 }, { - \\"name\\": \\"barTag 3\\", + \\"label\\": \\"barTag 3\\", \\"permalink\\": \\"/docs/next/tags/barTag-3-permalink\\", \\"count\\": 1 } diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/props.test.ts b/packages/docusaurus-plugin-content-docs/src/__tests__/props.test.ts index 86bd7b88fc27..5fe9cf406ff1 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/props.test.ts +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/props.test.ts @@ -54,9 +54,9 @@ describe('toTagDocListProp', () => { expect(result).toEqual({ allTagsPath, - name: tag.label, + label: tag.label, permalink: tag.permalink, - docs: [doc3, doc1], // docs sorted by title, ignore "id5" absence + items: [doc3, doc1], // docs sorted by title, ignore "id5" absence }); }); }); diff --git a/packages/docusaurus-theme-common/src/utils/__tests__/tagUtils.test.ts b/packages/docusaurus-theme-common/src/utils/__tests__/tagUtils.test.ts index 2de931f7e047..4e1f614d7702 100644 --- a/packages/docusaurus-theme-common/src/utils/__tests__/tagUtils.test.ts +++ b/packages/docusaurus-theme-common/src/utils/__tests__/tagUtils.test.ts @@ -15,32 +15,32 @@ describe('listTagsByLetters', () => { it('creates letters list', () => { const tag1: Tag = { - name: 'tag1', + label: 'tag1', permalink: '/tag1', count: 1, }; const tag2: Tag = { - name: 'Tag2', + label: 'Tag2', permalink: '/tag2', count: 11, }; const tagZxy: Tag = { - name: 'zxy', + label: 'zxy', permalink: '/zxy', count: 987, }; const tagAbc: Tag = { - name: 'Abc', + label: 'Abc', permalink: '/abc', count: 123, }; const tagDef: Tag = { - name: 'def', + label: 'def', permalink: '/def', count: 1, }; const tagAaa: Tag = { - name: 'aaa', + label: 'aaa', permalink: '/aaa', count: 10, }; From c6c5355d8c9b7062074bc8d370df41ba1f1189a4 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Wed, 6 Apr 2022 13:53:47 +0800 Subject: [PATCH 3/6] fix --- .../src/plugin-content-blog.d.ts | 3 ++- .../src/plugin-content-docs.d.ts | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts b/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts index 24ba4b4ef0a4..939ae794289f 100644 --- a/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts +++ b/packages/docusaurus-plugin-content-blog/src/plugin-content-blog.d.ts @@ -7,8 +7,9 @@ declare module '@docusaurus/plugin-content-blog' { import type {MDXOptions} from '@docusaurus/mdx-loader'; - import type {FrontMatterTag, Tag} from '@docusaurus/utils'; + import type {FrontMatterTag} from '@docusaurus/utils'; import type {Overwrite} from 'utility-types'; + import type {Tag} from '@docusaurus/types'; export type Assets = { /** diff --git a/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts b/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts index cee80a460a43..efa5e033341c 100644 --- a/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts +++ b/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts @@ -7,8 +7,8 @@ declare module '@docusaurus/plugin-content-docs' { import type {MDXOptions} from '@docusaurus/mdx-loader'; - import type {ContentPaths, Tag, FrontMatterTag} from '@docusaurus/utils'; - import type {TagsListItem, TagModule} from '@docusaurus/types'; + import type {ContentPaths, FrontMatterTag} from '@docusaurus/utils'; + import type {TagsListItem, TagModule, Tag} from '@docusaurus/types'; import type {Required} from 'utility-types'; export type Assets = { From 7127b4d53a1fdd58524b64c2aa78a64539ef0ace Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Wed, 6 Apr 2022 14:06:31 +0800 Subject: [PATCH 4/6] refactor --- .../src/index.ts | 21 ++++++++++--------- .../src/plugin-content-docs.d.ts | 8 +++---- .../src/props.ts | 2 +- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/packages/docusaurus-plugin-content-docs/src/index.ts b/packages/docusaurus-plugin-content-docs/src/index.ts index 5a8e08ad64b4..b8e7c121148b 100644 --- a/packages/docusaurus-plugin-content-docs/src/index.ts +++ b/packages/docusaurus-plugin-content-docs/src/index.ts @@ -19,7 +19,7 @@ import { createSlugger, DEFAULT_PLUGIN_ID, } from '@docusaurus/utils'; -import type {LoadContext, Plugin, TagsListItem} from '@docusaurus/types'; +import type {LoadContext, Plugin} from '@docusaurus/types'; import {loadSidebars} from './sidebars'; import {CategoryMetadataFilenamePattern} from './sidebars/generator'; import { @@ -50,6 +50,7 @@ import logger from '@docusaurus/logger'; import {getVersionTags} from './tags'; import {createVersionRoutes} from './routes'; import type { + PropTagsListPage, PluginOptions, DocMetadataBase, VersionMetadata, @@ -224,13 +225,13 @@ export default async function pluginContentDocs( // TODO tags should be a sub route of the version route async function createTagsListPage() { - const tagsProp: TagsListItem[] = Object.values(versionTags).map( - (tag) => ({ - label: tag.label, - permalink: tag.permalink, - count: tag.docIds.length, - }), - ); + const tagsProp: PropTagsListPage['tags'] = Object.values( + versionTags, + ).map((tagValue) => ({ + label: tagValue.label, + permalink: tagValue.permalink, + count: tagValue.docIds.length, + })); // Only create /tags page if there are tags. if (tagsProp.length > 0) { @@ -251,14 +252,14 @@ export default async function pluginContentDocs( // TODO tags should be a sub route of the version route async function createTagDocListPage(tag: VersionTag) { - const tagItemsProp = toTagDocListProp({ + const tagProps = toTagDocListProp({ allTagsPath: version.tagsPath, tag, docs: version.docs, }); const tagPropPath = await createData( `${docuHash(`tag-${tag.permalink}`)}.json`, - JSON.stringify(tagItemsProp, null, 2), + JSON.stringify(tagProps, null, 2), ); addRoute({ path: tag.permalink, diff --git a/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts b/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts index efa5e033341c..77d4b7b894c0 100644 --- a/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts +++ b/packages/docusaurus-plugin-content-docs/src/plugin-content-docs.d.ts @@ -488,9 +488,7 @@ declare module '@docusaurus/plugin-content-docs' { DocMetadata, 'id' | 'title' | 'description' | 'permalink' >; - export type PropTagDocList = { - tag: TagModule & {items: PropTagDocListDoc[]}; - }; + export type PropTagDocList = TagModule & {items: PropTagDocListDoc[]}; export type PropTagsListPage = { tags: TagsListItem[]; @@ -551,7 +549,9 @@ declare module '@theme/DocTagsListPage' { declare module '@theme/DocTagDocListPage' { import type {PropTagDocList} from '@docusaurus/plugin-content-docs'; - export interface Props extends PropTagDocList {} + export interface Props { + readonly tag: PropTagDocList; + } export default function DocTagDocListPage(props: Props): JSX.Element; } diff --git a/packages/docusaurus-plugin-content-docs/src/props.ts b/packages/docusaurus-plugin-content-docs/src/props.ts index 370c297b99d0..7529b9b22874 100644 --- a/packages/docusaurus-plugin-content-docs/src/props.ts +++ b/packages/docusaurus-plugin-content-docs/src/props.ts @@ -138,7 +138,7 @@ export function toTagDocListProp({ allTagsPath: string; tag: VersionTag; docs: DocMetadata[]; -}): PropTagDocList['tag'] { +}): PropTagDocList { function toDocListProp(): PropTagDocListDoc[] { const list = _.compact( tag.docIds.map((id) => docs.find((doc) => doc.id === id)), From ba181a0b8ccc6dba1a7bc3ac6eb8241dcb128ef5 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Wed, 6 Apr 2022 14:14:48 +0800 Subject: [PATCH 5/6] fix --- packages/docusaurus-theme-classic/src/theme-classic.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/docusaurus-theme-classic/src/theme-classic.d.ts b/packages/docusaurus-theme-classic/src/theme-classic.d.ts index 454653583f7d..d5d2e05d0ab9 100644 --- a/packages/docusaurus-theme-classic/src/theme-classic.d.ts +++ b/packages/docusaurus-theme-classic/src/theme-classic.d.ts @@ -1028,7 +1028,7 @@ declare module '@theme/TagsListInline' { } declare module '@theme/Tag' { - import type {TagsListItem} from '@docusaurus/theme-common'; + import type {TagsListItem} from '@docusaurus/types'; import type {Optional} from 'utility-types'; export interface Props extends Optional {} From 5d09121262787083c317e7a5f5e5bf44caa5c6d7 Mon Sep 17 00:00:00 2001 From: Joshua Chen Date: Wed, 6 Apr 2022 14:32:38 +0800 Subject: [PATCH 6/6] fix! --- packages/docusaurus-plugin-content-blog/src/index.ts | 1 + .../src/__tests__/__snapshots__/index.test.ts.snap | 6 ++++++ .../src/__tests__/props.test.ts | 1 + packages/docusaurus-plugin-content-docs/src/props.ts | 1 + .../src/theme/BlogTagsPostsPage/index.tsx | 5 ++--- .../src/theme/DocTagDocListPage/index.tsx | 2 +- packages/docusaurus-types/src/index.d.ts | 2 +- 7 files changed, 13 insertions(+), 5 deletions(-) diff --git a/packages/docusaurus-plugin-content-blog/src/index.ts b/packages/docusaurus-plugin-content-blog/src/index.ts index f5e22f5f6a37..c5538be08235 100644 --- a/packages/docusaurus-plugin-content-blog/src/index.ts +++ b/packages/docusaurus-plugin-content-blog/src/index.ts @@ -343,6 +343,7 @@ export default async function pluginContentBlog( label: tag.label, permalink: tag.permalink, allTagsPath: blogTagsListPath, + count: tag.items.length, }; const tagPropPath = await createData( `${docuHash(metadata.permalink)}.json`, diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap b/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap index cd72aa678441..2af71ead7b26 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/__snapshots__/index.test.ts.snap @@ -717,6 +717,7 @@ exports[`simple website content: data 1`] = ` \\"label\\": \\"tag 1\\", \\"permalink\\": \\"/docs/tags/tag-1\\", \\"allTagsPath\\": \\"/docs/tags\\", + \\"count\\": 2, \\"items\\": [ { \\"id\\": \\"foo/baz\\", @@ -736,6 +737,7 @@ exports[`simple website content: data 1`] = ` \\"label\\": \\"tag 2\\", \\"permalink\\": \\"/docs/tags/tag2-custom-permalink\\", \\"allTagsPath\\": \\"/docs/tags\\", + \\"count\\": 1, \\"items\\": [ { \\"id\\": \\"foo/baz\\", @@ -749,6 +751,7 @@ exports[`simple website content: data 1`] = ` \\"label\\": \\"tag 3\\", \\"permalink\\": \\"/docs/tags/tag-3\\", \\"allTagsPath\\": \\"/docs/tags\\", + \\"count\\": 1, \\"items\\": [ { \\"id\\": \\"hello\\", @@ -3175,6 +3178,7 @@ exports[`versioned website content: data 1`] = ` \\"label\\": \\"barTag 1\\", \\"permalink\\": \\"/docs/next/tags/bar-tag-1\\", \\"allTagsPath\\": \\"/docs/next/tags\\", + \\"count\\": 1, \\"items\\": [ { \\"id\\": \\"foo/bar\\", @@ -3188,6 +3192,7 @@ exports[`versioned website content: data 1`] = ` \\"label\\": \\"barTag-2\\", \\"permalink\\": \\"/docs/next/tags/bar-tag-2\\", \\"allTagsPath\\": \\"/docs/next/tags\\", + \\"count\\": 1, \\"items\\": [ { \\"id\\": \\"foo/bar\\", @@ -3201,6 +3206,7 @@ exports[`versioned website content: data 1`] = ` \\"label\\": \\"barTag 3\\", \\"permalink\\": \\"/docs/next/tags/barTag-3-permalink\\", \\"allTagsPath\\": \\"/docs/next/tags\\", + \\"count\\": 1, \\"items\\": [ { \\"id\\": \\"foo/bar\\", diff --git a/packages/docusaurus-plugin-content-docs/src/__tests__/props.test.ts b/packages/docusaurus-plugin-content-docs/src/__tests__/props.test.ts index 5fe9cf406ff1..db2648c5e5d7 100644 --- a/packages/docusaurus-plugin-content-docs/src/__tests__/props.test.ts +++ b/packages/docusaurus-plugin-content-docs/src/__tests__/props.test.ts @@ -54,6 +54,7 @@ describe('toTagDocListProp', () => { expect(result).toEqual({ allTagsPath, + count: 2, label: tag.label, permalink: tag.permalink, items: [doc3, doc1], // docs sorted by title, ignore "id5" absence diff --git a/packages/docusaurus-plugin-content-docs/src/props.ts b/packages/docusaurus-plugin-content-docs/src/props.ts index 7529b9b22874..43d3028d455c 100644 --- a/packages/docusaurus-plugin-content-docs/src/props.ts +++ b/packages/docusaurus-plugin-content-docs/src/props.ts @@ -157,6 +157,7 @@ export function toTagDocListProp({ label: tag.label, permalink: tag.permalink, allTagsPath, + count: tag.docIds.length, items: toDocListProp(), }; } diff --git a/packages/docusaurus-theme-classic/src/theme/BlogTagsPostsPage/index.tsx b/packages/docusaurus-theme-classic/src/theme/BlogTagsPostsPage/index.tsx index ca3fb2272b3b..fddda0795d75 100644 --- a/packages/docusaurus-theme-classic/src/theme/BlogTagsPostsPage/index.tsx +++ b/packages/docusaurus-theme-classic/src/theme/BlogTagsPostsPage/index.tsx @@ -46,7 +46,6 @@ export default function BlogTagsPostsPage({ sidebar, listMetadata, }: Props): JSX.Element { - const {allTagsPath, label: tagName} = tag; const blogPostsPlural = useBlogPostsPlural(); const title = translate( { @@ -54,7 +53,7 @@ export default function BlogTagsPostsPage({ description: 'The title of the page for a blog tag', message: '{nPosts} tagged with "{tagName}"', }, - {nPosts: blogPostsPlural(items.length), tagName}, + {nPosts: blogPostsPlural(tag.count), tagName: tag.label}, ); return ( @@ -69,7 +68,7 @@ export default function BlogTagsPostsPage({

{title}

- + diff --git a/packages/docusaurus-theme-classic/src/theme/DocTagDocListPage/index.tsx b/packages/docusaurus-theme-classic/src/theme/DocTagDocListPage/index.tsx index adabcfb283e5..22cf3ff7ac9b 100644 --- a/packages/docusaurus-theme-classic/src/theme/DocTagDocListPage/index.tsx +++ b/packages/docusaurus-theme-classic/src/theme/DocTagDocListPage/index.tsx @@ -57,7 +57,7 @@ export default function DocTagDocListPage({tag}: Props): JSX.Element { description: 'The title of the page for a docs tag', message: '{nDocsTagged} with "{tagName}"', }, - {nDocsTagged: nDocsTaggedPlural(tag.items.length), tagName: tag.label}, + {nDocsTagged: nDocsTaggedPlural(tag.count), tagName: tag.label}, ); return ( diff --git a/packages/docusaurus-types/src/index.d.ts b/packages/docusaurus-types/src/index.d.ts index c1f0357dc237..a490322fa0d0 100644 --- a/packages/docusaurus-types/src/index.d.ts +++ b/packages/docusaurus-types/src/index.d.ts @@ -603,7 +603,7 @@ export type TagsListItem = Tag & { }; /** What the tag's own page should know about the tag. */ -export type TagModule = Tag & { +export type TagModule = TagsListItem & { /** The tags list page's permalink. */ allTagsPath: string; };