Skip to content

Commit c100fc7

Browse files
committed
fix(remark): remove heading count restriction when adding toc placeholder
1 parent 31d39ee commit c100fc7

File tree

6 files changed

+54
-19
lines changed

6 files changed

+54
-19
lines changed

.changeset/cold-yaks-know.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'typedoc-plugin-remark': patch
3+
---
4+
5+
- Remove heading count restriction when adding toc placeholder heading (for usage with remark-toc).

docs/pages/docs/CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
## 4.3.3 (2024-12-18)
4+
5+
### Patch Changes
6+
7+
- Correctly handle anchor resolutions with table formats.
8+
- Fix invalid typescript syntax for type aliases inside declaration code blocks when "useCodeBlocks" is true ([#741](https://github.com/typedoc2md/typedoc-plugin-markdown/issues/741)).
9+
310
## 4.3.2 (2024-12-08)
411

512
### Patch Changes

docs/pages/plugins/remark/useful-plugins.mdx

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ https://github.com/remarkjs/remark-toc
88

99
- Adds inline table of contents to pages.
1010

11+
Note: This plugin searches for a heading in the page to generate the table of contents.
12+
`typedoc-plugin-remark` inserts this heading automatically (currently to Module, Interface, Class and Enum pages).
13+
1114
```sh npm2yarn
1215
npm install remark-toc --save-dev
1316
```

packages/typedoc-plugin-remark/src/index.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,15 @@ export function load(app: Application) {
4343
(name) => name === 'remark-toc',
4444
);
4545
const tocPlugin = remarkPlugins[tocPluginIndex];
46-
const tocOptions = Array.isArray(tocPlugin) ? tocPlugin[1] : {};
46+
const remarkTocOptions = Array.isArray(tocPlugin)
47+
? tocPlugin[1]
48+
: {};
4749
defaultPlugins.push([
4850
path.join(__dirname, 'plugins', 'add-toc.js'),
4951
{
5052
reflection: urlMapping.model,
5153
typedocOptions: app.options,
52-
tocOptions,
54+
remarkTocOptions,
5355
},
5456
]);
5557
}

packages/typedoc-plugin-remark/src/plugins/add-toc.ts

+32-16
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@ import { Node } from 'unist';
44
import { visit } from 'unist-util-visit';
55

66
/**
7-
* This plugin is used internally to add a toc heading as required to be present in the page when using the remark-toc plugin.
7+
* This plugin internally adds a TOC heading to document as required by remark-toc.
88
*/
9-
export default function (options: any) {
10-
const { reflection, typedocOptions, tocOptions } = options;
9+
export default function (options: {
10+
reflection: DeclarationReflection;
11+
typedocOptions: Options;
12+
remarkTocOptions: { heading: string };
13+
}) {
14+
const { reflection, typedocOptions, remarkTocOptions } = options;
1115

1216
return (tree: Node) => {
13-
// If the current page is already an index page, do nothing.
14-
if (isIndexPage(reflection, typedocOptions)) {
17+
if (!shouldAddToc(reflection, typedocOptions)) {
1518
return tree;
1619
}
1720
visit(
@@ -25,7 +28,7 @@ export default function (options: any) {
2528
children: [
2629
{
2730
type: 'text',
28-
value: tocOptions?.heading || 'Contents',
31+
value: remarkTocOptions?.heading || 'Contents',
2932
},
3033
],
3134
};
@@ -39,24 +42,37 @@ export default function (options: any) {
3942

4043
/**
4144
* Determine if the current page is already an index page.
42-
* - Reflection is a project and all children are modules.
43-
* - Reflection is a module and outputFileStrategy is equal to "members".
45+
*
46+
* Returns false if
47+
*
48+
* - The reflection is a module or project and outputFileStrategy is equal to "members".
49+
* - The reflection is a project and all children are modules.
50+
* - The reflection kind is not in the list of allowed kinds.
51+
* -
4452
*/
45-
function isIndexPage(
53+
function shouldAddToc(
4654
reflection: DeclarationReflection,
4755
typedocOptions: Options,
4856
) {
4957
if (
50-
reflection.kind === ReflectionKind.Project &&
51-
reflection.children?.every((child) => child.kind === ReflectionKind.Module)
58+
[ReflectionKind.Project, ReflectionKind.Module].includes(reflection.kind) &&
59+
typedocOptions?.getValue('outputFileStrategy') === 'members'
5260
) {
53-
return true;
61+
return false;
5462
}
5563
if (
56-
[ReflectionKind.Project, ReflectionKind.Module].includes(reflection.kind) &&
57-
typedocOptions?.getValue('outputFileStrategy') === 'members'
64+
reflection.kind === ReflectionKind.Project &&
65+
reflection.children?.every((child) => child.kind === ReflectionKind.Module)
5866
) {
59-
return true;
67+
return false;
6068
}
61-
return false;
69+
return [
70+
ReflectionKind.Project,
71+
ReflectionKind.Module,
72+
ReflectionKind.Namespace,
73+
ReflectionKind.Class,
74+
ReflectionKind.Enum,
75+
ReflectionKind.Interface,
76+
ReflectionKind.Document,
77+
].includes(reflection.kind);
6278
}

packages/typedoc-plugin-remark/test/typedoc.toc.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
"tsconfig": "./stubs/tsconfig.json",
44
"plugin": ["typedoc-plugin-markdown", "typedoc-plugin-remark"],
55
"remarkPlugins": [["remark-toc", { "maxDepth": 3 }]],
6-
"readme": "none"
6+
"readme": "none",
7+
"disableSources": true,
8+
"hidePageHeader": true
79
}

0 commit comments

Comments
 (0)