From 108b356ea5828fec4eb64542a7fa1e637cc4cd12 Mon Sep 17 00:00:00 2001 From: tgreyuk Date: Fri, 26 Apr 2024 18:05:12 +0100 Subject: [PATCH] chore: updated remark docs --- docs/pages/plugins/remark.mdx | 16 +++++- docs/pages/plugins/remark/_meta.js | 1 - docs/pages/plugins/remark/quick-start.mdx | 17 ------- packages/typedoc-plugin-remark/src/index.ts | 51 ++----------------- .../src/options/helpers.ts | 50 ++++++++++++++++++ 5 files changed, 68 insertions(+), 67 deletions(-) delete mode 100644 docs/pages/plugins/remark/quick-start.mdx create mode 100644 packages/typedoc-plugin-remark/src/options/helpers.ts diff --git a/docs/pages/plugins/remark.mdx b/docs/pages/plugins/remark.mdx index 953f0c64..86d247b4 100644 --- a/docs/pages/plugins/remark.mdx +++ b/docs/pages/plugins/remark.mdx @@ -8,6 +8,20 @@ import { faGithub } from '@fortawesome/free-brands-svg-icons'; +## Installation + +```sh npm2yarn +npm install typedoc-plugin-remark@next --save-dev +``` + +## Usage + +```json filename="typedoc.json" +{ + "plugin": ["typedoc-plugin-markdown", "typedoc-plugin-remark"] +} +``` + ## Examples @@ -15,7 +29,7 @@ import { faGithub } from '@fortawesome/free-brands-svg-icons'; title="View on Github" icon={} arrow={true} - href="https://github.com/tgreyuk/typedoc-plugin-markdown-examples/tree/main/examples/03-typedoc-plugin-remark" + href="https://github.com/tgreyuk/typedoc-plugin-markdown-examples/tree/main/examples/09-remark-example#readme" target="_blank" /> diff --git a/docs/pages/plugins/remark/_meta.js b/docs/pages/plugins/remark/_meta.js index d4a18935..4eaee93c 100644 --- a/docs/pages/plugins/remark/_meta.js +++ b/docs/pages/plugins/remark/_meta.js @@ -1,5 +1,4 @@ export default { - 'quick-start': '', options: '', guide: '', }; diff --git a/docs/pages/plugins/remark/quick-start.mdx b/docs/pages/plugins/remark/quick-start.mdx deleted file mode 100644 index 8bccc43e..00000000 --- a/docs/pages/plugins/remark/quick-start.mdx +++ /dev/null @@ -1,17 +0,0 @@ -import { Callout } from 'nextra/components'; - -# Quick Start - -## Installation - -```sh npm2yarn -npm install typedoc-plugin-remark@next --save-dev -``` - -## Usage - -```json filename="typedoc.json" -{ - "plugin": ["typedoc-plugin-markdown", "typedoc-plugin-remark"] -} -``` diff --git a/packages/typedoc-plugin-remark/src/index.ts b/packages/typedoc-plugin-remark/src/index.ts index 2bb20ba0..14296ed9 100644 --- a/packages/typedoc-plugin-remark/src/index.ts +++ b/packages/typedoc-plugin-remark/src/index.ts @@ -1,12 +1,7 @@ -import { - Application, - DeclarationOption, - DeclarationReflection, - ReflectionKind, - RendererEvent, -} from 'typedoc'; +import { Application, DeclarationOption, RendererEvent } from 'typedoc'; import { MarkdownPageEvent } from 'typedoc-plugin-markdown'; import * as options from './options/declarations'; +import { addTableOfContents } from './options/helpers'; export function load(app: Application) { Object.entries(options).forEach(([name, option]) => { @@ -23,47 +18,7 @@ export function load(app: Application) { ) as string[]; if (remarkPluginsNames.includes('remark-toc')) { - const tocPluginIndex = remarkPluginsNames.findIndex( - (name) => name === 'remark-toc', - ); - const tocPlugin = remarkPlugins[tocPluginIndex]; - const options = Array.isArray(tocPlugin) ? tocPlugin[1] : {}; - - const isModulesOnly = ( - event?.model as DeclarationReflection - ).children?.every((child) => child.kind === ReflectionKind.Module); - const outputFileStrategy = app.options.getValue('outputFileStrategy'); - - const kindsWithToc = [ - ReflectionKind.Namespace, - ReflectionKind.Class, - ReflectionKind.Enum, - ReflectionKind.Interface, - ]; - - if (outputFileStrategy === 'modules') { - kindsWithToc.push(ReflectionKind.Module); - } - - if (!isModulesOnly) { - kindsWithToc.push(ReflectionKind.Project); - } - - if (kindsWithToc.includes(event.model?.kind)) { - const contents = event.contents; - const contentToLines = contents?.split('\n'); - const firstHeadingIndex = contentToLines?.findIndex((line) => - line.startsWith('##'), - ); - if (firstHeadingIndex && firstHeadingIndex > 0) { - contentToLines?.splice( - firstHeadingIndex, - 0, - `\n\n## ${(options as any)?.heading || 'Contents'}\n\n`, - ); - event.contents = contentToLines?.join('\n'); - } - } + addTableOfContents(event, remarkPlugins, remarkPluginsNames, app); } }); diff --git a/packages/typedoc-plugin-remark/src/options/helpers.ts b/packages/typedoc-plugin-remark/src/options/helpers.ts new file mode 100644 index 00000000..37f18ab7 --- /dev/null +++ b/packages/typedoc-plugin-remark/src/options/helpers.ts @@ -0,0 +1,50 @@ +import { DeclarationReflection, ReflectionKind } from 'typedoc'; + +export function addTableOfContents( + event: any, + remarkPlugins: any[], + remarkPluginsNames: string[], + app: any, +) { + const tocPluginIndex = remarkPluginsNames.findIndex( + (name) => name === 'remark-toc', + ); + const tocPlugin = remarkPlugins[tocPluginIndex]; + const options = Array.isArray(tocPlugin) ? tocPlugin[1] : {}; + + const isModulesOnly = (event?.model as DeclarationReflection).children?.every( + (child) => child.kind === ReflectionKind.Module, + ); + const outputFileStrategy = app.options.getValue('outputFileStrategy'); + + const kindsWithToc = [ + ReflectionKind.Namespace, + ReflectionKind.Class, + ReflectionKind.Enum, + ReflectionKind.Interface, + ]; + + if (outputFileStrategy === 'modules') { + kindsWithToc.push(ReflectionKind.Module); + } + + if (!isModulesOnly) { + kindsWithToc.push(ReflectionKind.Project); + } + + if (kindsWithToc.includes(event.model?.kind)) { + const contents = event.contents; + const contentToLines = contents?.split('\n'); + const firstHeadingIndex = contentToLines?.findIndex((line) => + line.startsWith('##'), + ); + if (firstHeadingIndex && firstHeadingIndex > 0) { + contentToLines?.splice( + firstHeadingIndex, + 0, + `\n\n## ${(options as any)?.heading || 'Contents'}\n\n`, + ); + event.contents = contentToLines?.join('\n'); + } + } +}