From 87b75ea9593732e6801b8596a52f9f82dbde251a Mon Sep 17 00:00:00 2001 From: Shahed Nasser Date: Wed, 26 Mar 2025 08:52:44 +0200 Subject: [PATCH] docs: add sitemap to API reference (#11985) * docs: add sitemap to API reference * remove duplicate sitemap * update sitemap --- www/apps/api-reference/app/sitemap.ts | 65 ++++++++++++++++++++++++++ www/apps/api-reference/app/sitemap.xml | 9 ---- 2 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 www/apps/api-reference/app/sitemap.ts delete mode 100644 www/apps/api-reference/app/sitemap.xml diff --git a/www/apps/api-reference/app/sitemap.ts b/www/apps/api-reference/app/sitemap.ts new file mode 100644 index 0000000000..8029ba3f44 --- /dev/null +++ b/www/apps/api-reference/app/sitemap.ts @@ -0,0 +1,65 @@ +import type { MetadataRoute } from "next" +import path from "path" +import getUrl from "../utils/get-url" +import { findAllPageHeadings, getSectionId } from "docs-utils" +import OpenAPIParser from "@readme/openapi-parser" +import { OpenAPI } from "types" +import { readFile } from "fs/promises" + +export default async function sitemap(): Promise { + const items: MetadataRoute.Sitemap = [] + + const markdownPath = path.join(process.cwd(), "markdown") + + for (const area of ["store", "admin"]) { + // find and parse static headers from pages + const markdownContent = await readFile( + path.join(markdownPath, `${area}.mdx`), + "utf-8" + ) + + const headings = findAllPageHeadings({ content: markdownContent, level: 2 }) + headings.forEach((heading) => { + const objectID = getSectionId([heading]) + const url = getUrl(area, objectID) + items.push({ + url, + lastModified: new Date(), + changeFrequency: "weekly", + }) + }) + + // find and index tag and operations + const baseSpecs = (await OpenAPIParser.parse( + path.join(process.cwd(), `specs/${area}/openapi.full.yaml`) + )) as OpenAPI.ExpandedDocument + + baseSpecs.tags?.map((tag) => { + const tagName = getSectionId([tag.name]) + const url = getUrl(area, tagName) + items.push({ + url, + lastModified: new Date(), + changeFrequency: "weekly", + }) + }) + + const paths = baseSpecs.paths + + Object.values(paths).forEach((path) => { + Object.values(path).forEach((op) => { + const operation = op as OpenAPI.Operation + const tag = operation.tags?.[0] + const operationName = getSectionId([tag || "", operation.operationId]) + const url = getUrl(area, operationName) + items.push({ + url, + lastModified: new Date(), + changeFrequency: "weekly", + }) + }) + }) + } + + return items +} diff --git a/www/apps/api-reference/app/sitemap.xml b/www/apps/api-reference/app/sitemap.xml deleted file mode 100644 index 75897dfebc..0000000000 --- a/www/apps/api-reference/app/sitemap.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - https://docs.medusajs.com/assets/sitemap.xml - - - https://docs.medusajs.com/sitemap-docs.xml - - \ No newline at end of file