From 5d9a51d882a153012fed36dea0379a505143530d Mon Sep 17 00:00:00 2001 From: Gerrit Birkeland Date: Sat, 30 Jul 2022 08:26:21 -0600 Subject: [PATCH] markdown helper now accepts CommentDisplayPart Ref: #2004 --- CHANGELOG.md | 1 + .../default/DefaultThemeRenderContext.ts | 18 +++++++++++++++--- .../output/themes/default/partials/comment.tsx | 8 ++++---- .../output/themes/default/partials/index.tsx | 4 ++-- .../output/themes/default/templates/index.tsx | 5 ++--- src/lib/output/themes/lib.tsx | 5 ++++- 6 files changed, 28 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a0200942f..e08de0257 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Added support for using the comment directly before a constructor parameter that declares a property as the property comment, #2019. - Improved schema generation to give better autocomplete for the `sort` option. - Optional properties are now visually distinguished in the index/sidebar by rendering `prop` as `prop?`, #2023. +- `DefaultThemeRenderContext.markdown` now also accepts a `CommentDisplayPart[]` for rendering, #2004. ### Bug Fixes diff --git a/src/lib/output/themes/default/DefaultThemeRenderContext.ts b/src/lib/output/themes/default/DefaultThemeRenderContext.ts index 4cc19dbd8..1e7dc11b2 100644 --- a/src/lib/output/themes/default/DefaultThemeRenderContext.ts +++ b/src/lib/output/themes/default/DefaultThemeRenderContext.ts @@ -1,6 +1,11 @@ import type { RendererHooks } from "../.."; -import type { ReferenceType, Reflection } from "../../../models"; -import type { Options } from "../../../utils"; +import type { + CommentDisplayPart, + ReferenceType, + Reflection, +} from "../../../models"; +import type { NeverIfInternal, Options } from "../../../utils"; +import { displayPartsToMarkdown } from "../lib"; import type { DefaultTheme } from "./DefaultTheme"; import { defaultLayout } from "./layouts/default"; import { index } from "./partials"; @@ -58,7 +63,14 @@ export class DefaultThemeRenderContext { urlTo = (reflection: Reflection) => this.relativeURL(reflection.url); - markdown = (md: string | undefined) => { + markdown = ( + md: readonly CommentDisplayPart[] | NeverIfInternal + ) => { + if (md instanceof Array) { + return this.theme.markedPlugin.parseMarkdown( + displayPartsToMarkdown(md, this.urlTo) + ); + } return md ? this.theme.markedPlugin.parseMarkdown(md) : ""; }; diff --git a/src/lib/output/themes/default/partials/comment.tsx b/src/lib/output/themes/default/partials/comment.tsx index e5ba21dc3..797ab74c3 100644 --- a/src/lib/output/themes/default/partials/comment.tsx +++ b/src/lib/output/themes/default/partials/comment.tsx @@ -1,20 +1,20 @@ import type { DefaultThemeRenderContext } from "../DefaultThemeRenderContext"; import { JSX, Raw } from "../../../../utils"; import type { Reflection } from "../../../../models"; -import { camelToTitleCase, displayPartsToMarkdown } from "../../lib"; +import { camelToTitleCase } from "../../lib"; -export function comment({ markdown, urlTo }: DefaultThemeRenderContext, props: Reflection) { +export function comment({ markdown }: DefaultThemeRenderContext, props: Reflection) { if (!props.comment?.hasVisibleComponent()) return; // Note: Comment modifiers are handled in `renderFlags` return (
- + {props.comment.blockTags.map((item) => ( <>

{camelToTitleCase(item.tag.substring(1))}

- + ))}
diff --git a/src/lib/output/themes/default/partials/index.tsx b/src/lib/output/themes/default/partials/index.tsx index 40b3d7f4d..d7fcb4ef7 100644 --- a/src/lib/output/themes/default/partials/index.tsx +++ b/src/lib/output/themes/default/partials/index.tsx @@ -1,4 +1,4 @@ -import { classNames, displayPartsToMarkdown, renderName } from "../../lib"; +import { classNames, renderName } from "../../lib"; import type { DefaultThemeRenderContext } from "../DefaultThemeRenderContext"; import { JSX, Raw } from "../../../../utils"; import { ContainerReflection, DeclarationReflection, ReflectionCategory, ReflectionKind } from "../../../../models"; @@ -73,7 +73,7 @@ export function index(context: DefaultThemeRenderContext, props: ContainerReflec props.readme?.length !== 0 && (
- +
)} diff --git a/src/lib/output/themes/default/templates/index.tsx b/src/lib/output/themes/default/templates/index.tsx index d57b5aaae..65ad88748 100644 --- a/src/lib/output/themes/default/templates/index.tsx +++ b/src/lib/output/themes/default/templates/index.tsx @@ -2,10 +2,9 @@ import type { DefaultThemeRenderContext } from "../DefaultThemeRenderContext"; import type { ProjectReflection } from "../../../../models"; import type { PageEvent } from "../../../events"; import { JSX, Raw } from "../../../../utils"; -import { displayPartsToMarkdown } from "../../lib"; -export const indexTemplate = ({ markdown, urlTo }: DefaultThemeRenderContext, props: PageEvent) => ( +export const indexTemplate = ({ markdown }: DefaultThemeRenderContext, props: PageEvent) => (
- +
); diff --git a/src/lib/output/themes/lib.tsx b/src/lib/output/themes/lib.tsx index 834c7faa2..6f2492c68 100644 --- a/src/lib/output/themes/lib.tsx +++ b/src/lib/output/themes/lib.tsx @@ -117,7 +117,10 @@ export function camelToTitleCase(text: string) { return text.substring(0, 1).toUpperCase() + text.substring(1).replace(/[a-z][A-Z]/g, (x) => `${x[0]} ${x[1]}`); } -export function displayPartsToMarkdown(parts: CommentDisplayPart[], urlTo: DefaultThemeRenderContext["urlTo"]) { +export function displayPartsToMarkdown( + parts: readonly CommentDisplayPart[], + urlTo: DefaultThemeRenderContext["urlTo"] +) { const result: string[] = []; for (const part of parts) {