From 2a73d0ac0dc87a651543fedb914a2230016b5de7 Mon Sep 17 00:00:00 2001 From: Shahed Nasser Date: Tue, 17 Dec 2024 13:07:59 +0200 Subject: [PATCH] docs-util: support @tags comments (#10627) * support tags in tsdocs + add them in frontmatter * fixes --- www/utils/packages/typedoc-config/tsdoc.json | 4 ++ .../src/resources/helpers/comments.ts | 1 + .../src/resources/helpers/frontmatter.ts | 41 +++++++++++-------- www/utils/packages/types/lib/index.d.ts | 10 ++++- www/utils/packages/utils/src/index.ts | 1 + www/utils/packages/utils/src/tag-utils.ts | 25 +++++++++++ 6 files changed, 64 insertions(+), 18 deletions(-) create mode 100644 www/utils/packages/utils/src/tag-utils.ts diff --git a/www/utils/packages/typedoc-config/tsdoc.json b/www/utils/packages/typedoc-config/tsdoc.json index 6cd2912cfc53d..d86c97f0717e5 100644 --- a/www/utils/packages/typedoc-config/tsdoc.json +++ b/www/utils/packages/typedoc-config/tsdoc.json @@ -53,6 +53,10 @@ { "tagName": "@version", "syntaxKind": "block" + }, + { + "tagName": "@tags", + "syntaxKind": "block" } ] } \ No newline at end of file diff --git a/www/utils/packages/typedoc-plugin-markdown-medusa/src/resources/helpers/comments.ts b/www/utils/packages/typedoc-plugin-markdown-medusa/src/resources/helpers/comments.ts index d707decc6e097..41f015d559fd0 100644 --- a/www/utils/packages/typedoc-plugin-markdown-medusa/src/resources/helpers/comments.ts +++ b/www/utils/packages/typedoc-plugin-markdown-medusa/src/resources/helpers/comments.ts @@ -8,6 +8,7 @@ const EXCLUDED_TAGS = [ "@category", "@typeParamDefinition", "@version", + "@tags", ] export default function () { diff --git a/www/utils/packages/typedoc-plugin-markdown-medusa/src/resources/helpers/frontmatter.ts b/www/utils/packages/typedoc-plugin-markdown-medusa/src/resources/helpers/frontmatter.ts index 7af7af946ae30..ac80604a4e900 100644 --- a/www/utils/packages/typedoc-plugin-markdown-medusa/src/resources/helpers/frontmatter.ts +++ b/www/utils/packages/typedoc-plugin-markdown-medusa/src/resources/helpers/frontmatter.ts @@ -3,6 +3,8 @@ import { MarkdownTheme } from "../../theme" import { stringify } from "yaml" import { replaceTemplateVariables } from "../../utils/reflection-template-strings" import { Reflection } from "typedoc" +import { FrontmatterData } from "types" +import { getTagComments, getTagsAsArray } from "utils" export default function (theme: MarkdownTheme) { Handlebars.registerHelper("frontmatter", function (this: Reflection) { @@ -13,34 +15,39 @@ export default function (theme: MarkdownTheme) { } // format frontmatter data in case it has any template variables + const resolvedFrontmatter = resolveFrontmatterVariables( + frontmatterData, + this + ) - return `---\n${stringify( - resolveFrontmatterVariables(frontmatterData, this) - ).trim()}\n---\n\n` + // check if reflection has an `@tags` tag + const tagsComment = getTagComments(this) + if (tagsComment?.length && !("tags" in resolvedFrontmatter)) { + resolvedFrontmatter["tags"] = [] + } + tagsComment?.forEach((tag) => { + const tagContent = getTagsAsArray(tag) + resolvedFrontmatter["tags"]?.push(...tagContent) + }) + + return `---\n${stringify(resolvedFrontmatter).trim()}\n---\n\n` }) } function resolveFrontmatterVariables( - frontmatterData: Record, + frontmatterData: FrontmatterData, reflection: Reflection -): Record { - const tempFrontmatterData = Object.assign({}, frontmatterData) +): FrontmatterData { + const tempFrontmatterData: FrontmatterData = JSON.parse( + JSON.stringify(frontmatterData) + ) Object.keys(tempFrontmatterData).forEach((key) => { const value = tempFrontmatterData[key] - if (!value) { + if (!value || typeof value !== "string") { return } - switch (typeof value) { - case "object": - tempFrontmatterData[key] = resolveFrontmatterVariables( - value as Record, - reflection - ) - break - case "string": - tempFrontmatterData[key] = replaceTemplateVariables(reflection, value) - } + tempFrontmatterData[key] = replaceTemplateVariables(reflection, value) }) return tempFrontmatterData diff --git a/www/utils/packages/types/lib/index.d.ts b/www/utils/packages/types/lib/index.d.ts index 87599af8611bb..9736fb648ea58 100644 --- a/www/utils/packages/types/lib/index.d.ts +++ b/www/utils/packages/types/lib/index.d.ts @@ -50,6 +50,14 @@ export type FormattingOptionsType = { [k: string]: FormattingOptionType } +export type FrontmatterData = { + slug?: string + sidebar_label?: string + displayed_sidebar?: string + tags?: string[] + [k: string]: unknown +} + export type FormattingOptionType = { sections?: Sections reflectionGroups?: { @@ -74,7 +82,7 @@ export type FormattingOptionType = { showCommentsAsHeader?: boolean showCommentsAsDetails?: boolean parameterStyle?: ParameterStyle - frontmatterData?: Record + frontmatterData?: FrontmatterData parameterComponent?: string parameterComponentExtraProps?: Record mdxImports?: string[] diff --git a/www/utils/packages/utils/src/index.ts b/www/utils/packages/utils/src/index.ts index f8fe0ea9f97bd..f18aafee71735 100644 --- a/www/utils/packages/utils/src/index.ts +++ b/www/utils/packages/utils/src/index.ts @@ -6,4 +6,5 @@ export * from "./hooks-util" export * from "./step-utils" export * from "./str-formatting" export * from "./str-utils" +export * from "./tag-utils" export * from "./workflow-utils" diff --git a/www/utils/packages/utils/src/tag-utils.ts b/www/utils/packages/utils/src/tag-utils.ts new file mode 100644 index 0000000000000..6f95664d1256e --- /dev/null +++ b/www/utils/packages/utils/src/tag-utils.ts @@ -0,0 +1,25 @@ +import { CommentTag, DeclarationReflection, Reflection } from "typedoc" + +export const getTagsAsArray = (tag: CommentTag): string[] => { + return tag.content + .map((content) => content.text) + .join("") + .split(",") + .map((value) => value.trim()) +} + +export const getTagComments = (reflection: Reflection): CommentTag[] => { + const tagComments: CommentTag[] = [] + + reflection.comment?.blockTags + .filter((tag) => tag.tag === `@tags`) + .forEach((tag) => tagComments.push(tag)) + + if (reflection instanceof DeclarationReflection) { + reflection.signatures?.forEach((signature) => + tagComments.push(...getTagComments(signature)) + ) + } + + return tagComments +}