Skip to content

Commit

Permalink
chore: updated remark docs
Browse files Browse the repository at this point in the history
  • Loading branch information
tgreyuk committed Apr 26, 2024
1 parent 6ea429a commit 108b356
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 67 deletions.
16 changes: 15 additions & 1 deletion docs/pages/plugins/remark.mdx
Expand Up @@ -8,14 +8,28 @@ import { faGithub } from '@fortawesome/free-brands-svg-icons';

<PackageDescription pjson={pjson} />

## Installation

```sh npm2yarn
npm install typedoc-plugin-remark@next --save-dev
```

## Usage

```json filename="typedoc.json"
{
"plugin": ["typedoc-plugin-markdown", "typedoc-plugin-remark"]
}
```

## Examples

<Cards num={2}>
<Cards.Card
title="View on Github"
icon={<FontAwesomeIcon icon={faGithub} />}
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"
/>
</Cards>
1 change: 0 additions & 1 deletion docs/pages/plugins/remark/_meta.js
@@ -1,5 +1,4 @@
export default {
'quick-start': '',
options: '',
guide: '',
};
17 changes: 0 additions & 17 deletions docs/pages/plugins/remark/quick-start.mdx

This file was deleted.

51 changes: 3 additions & 48 deletions 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]) => {
Expand All @@ -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);
}
});

Expand Down
50 changes: 50 additions & 0 deletions 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');
}
}
}

0 comments on commit 108b356

Please sign in to comment.