function createCategoryItem()

in packages/docusaurus-plugin-content-docs/src/sidebars/generator.ts [154:249]


    function createCategoryItem(
      dir: Dir,
      fullPath: string,
      folderName: string,
    ): WithPosition<NormalizedSidebarItemCategory> {
      const categoryMetadata =
        categoriesMetadata[path.posix.join(autogenDir, fullPath)];
      const allItems = Object.entries(dir).map(([key, content]) =>
        dirToItem(content, key, `${fullPath}/${key}`),
      );

      // Try to match a doc inside the category folder,
      // using the "local id" (myDoc) or "qualified id" (dirName/myDoc)
      function findDocByLocalId(localId: string): SidebarItemDoc | undefined {
        return allItems.find(
          (item): item is SidebarItemDoc =>
            item.type === 'doc' && getLocalDocId(item.id) === localId,
        );
      }

      function findConventionalCategoryDocLink(): SidebarItemDoc | undefined {
        return allItems.find((item): item is SidebarItemDoc => {
          if (item.type !== 'doc') {
            return false;
          }
          const doc = getDoc(item.id);
          return isCategoryIndex(toCategoryIndexMatcherParam(doc));
        });
      }

      // In addition to the ID, this function also retrieves metadata of the
      // linked doc that could be used as fallback values for category metadata
      function getCategoryLinkedDocMetadata():
        | {
            id: string;
            position?: number;
            label?: string;
            customProps?: {[key: string]: unknown};
            className?: string;
          }
        | undefined {
        const link = categoryMetadata?.link;
        if (link !== undefined && link?.type !== 'doc') {
          // If a link is explicitly specified, we won't apply conventions
          return undefined;
        }
        const id = link
          ? findDocByLocalId(link.id)?.id ?? getDoc(link.id).id
          : findConventionalCategoryDocLink()?.id;
        if (!id) {
          return undefined;
        }
        const doc = getDoc(id);
        return {
          id,
          position: doc.sidebarPosition,
          label: doc.frontMatter.sidebar_label ?? doc.title,
          customProps: doc.frontMatter.sidebar_custom_props,
          className: doc.frontMatter.sidebar_class_name,
        };
      }
      const categoryLinkedDoc = getCategoryLinkedDocMetadata();
      const link: SidebarItemCategoryLinkConfig | null | undefined =
        categoryLinkedDoc
          ? {
              type: 'doc',
              id: categoryLinkedDoc.id, // We "remap" a potentially "local id" to a "qualified id"
            }
          : categoryMetadata?.link;
      // If a doc is linked, remove it from the category subItems
      const items = allItems.filter(
        (item) => !(item.type === 'doc' && item.id === categoryLinkedDoc?.id),
      );

      const className =
        categoryMetadata?.className ?? categoryLinkedDoc?.className;
      const customProps =
        categoryMetadata?.customProps ?? categoryLinkedDoc?.customProps;
      const {filename, numberPrefix} = numberPrefixParser(folderName);

      return {
        type: 'category',
        label: categoryMetadata?.label ?? categoryLinkedDoc?.label ?? filename,
        collapsible: categoryMetadata?.collapsible,
        collapsed: categoryMetadata?.collapsed,
        position:
          categoryMetadata?.position ??
          categoryLinkedDoc?.position ??
          numberPrefix,
        source: folderName,
        ...(customProps !== undefined && {customProps}),
        ...(className !== undefined && {className}),
        items,
        ...(link && {link}),
      };
    }