Skip to content

Commit 3d54350

Browse files
committed
feat(remark): exposed "defaultRemarkPlugins" option
1 parent e2f06c4 commit 3d54350

26 files changed

+310
-159
lines changed

.changeset/nasty-mice-joke.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'typedoc-plugin-remark': minor
3+
---
4+
5+
- Exposed "defaultRemarkPlugins" option to configure which remark plugins are loaded by default (#735).

docs/pages/docs/CHANGELOG.md

+10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Changelog
22

3+
## 4.3.2 (2024-12-08)
4+
5+
### Patch Changes
6+
7+
- Enable `{@link}` resolution on type alias properties ([#732](https://github.com/typedoc2md/typedoc-plugin-markdown/issues/732)).
8+
- Remove superfluous name attribute when "useHtmlAnchors" is true..
9+
- Escape characters inside `@link` tags.
10+
- Fixed spacing around inline object declarations.
11+
- Always expose type arguments of reference types as per default theme ([#733](https://github.com/typedoc2md/typedoc-plugin-markdown/issues/733)).
12+
313
## 4.3.1 (2024-12-01)
414

515
### Patch Changes

docs/pages/plugins/remark/options.mdx

+30-8
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,19 @@ import { Callout } from "nextra/components";
44

55
## --remarkPlugins
66

7-
<Callout emoji="💡">An array of remark plugin names.</Callout>
7+
<Callout emoji="💡">An array of remark plugin names to be executed.</Callout>
88

99
> Accepts an Array.
1010
11-
You can provide any compatible [remark plugins](https://github.com/remarkjs/remark/blob/main/doc/plugins.md) or you can write your own and reference locally.
11+
You can include any compatible [remark plugins](https://github.com/remarkjs/remark/blob/main/doc/plugins.md) or create and reference your own locally.
1212

13-
Each required plugin should be individually installed.
13+
Each plugin you wish to use must be installed individually.
1414

15-
Options can be passed either as an array of strings or an array of string / options.
16-
17-
Please note that `remark-frontmatter`, `remark-gfm`, and `remark-mdx` are always included by default.
15+
Options can be provided as either an array of strings or an array of strings with associated options.
1816

1917
```json filename="typedoc.json"
2018
{
2119
"remarkPlugins": [
22-
"unified-prettier",
2320
"remark-github",
2421
[
2522
"remark-toc",
@@ -31,13 +28,38 @@ Please note that `remark-frontmatter`, `remark-gfm`, and `remark-mdx` are always
3128
}
3229
```
3330

31+
## --defaultRemarkPlugins
32+
33+
<Callout emoji="💡">
34+
A set of flags that control the enabling or disabling of remark plugins that
35+
are loaded by default.
36+
</Callout>
37+
38+
>
39+
40+
By default, the plugins [`remark-gfm`](https://github.com/remarkjs/remark-gfm), [`remark-frontmatter`](https://github.com/remarkjs/remark-frontmatter), and [`remark-mdx`](https://github.com/mdx-js/mdx/tree/main/packages/remark-mdx) are included, as these are considered the most common use cases.
41+
42+
However, these plugins modify the default parsing behavior of remark, which may not be ideal for all scenarios.
43+
44+
If you'd like to disable any of these default plugins, simply set the corresponding flag to `false`.
45+
46+
```json filename="typedoc.json"
47+
{
48+
"defaultRemarkPlugins": {
49+
"gfm": true,
50+
"frontmatter": true,
51+
"mdx": true
52+
}
53+
}
54+
```
55+
3456
## --remarkStringifyOptions
3557

3658
<Callout emoji="💡">Custom options for the remark-stringify plugin.</Callout>
3759

3860
> Accepts a key/value object.
3961
40-
Under the hood, the `remark-stringify` plugin is used to serialize the markdown into final output.
62+
Under the hood, the [`remark-stringify`](https://github.com/remarkjs/remark/tree/main/packages/remark-stringify) plugin is used to serialize the markdown into final output.
4163

4264
You can pass in options to the `remark-stringify` plugin using this option.
4365

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/typedoc-plugin-remark/lint.mdx.mjs

-44
This file was deleted.

packages/typedoc-plugin-remark/package.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@
1414
"prepublishOnly": "npm run lint && npm run build",
1515
"prebuild": "rm -rf dist && prebuild-options",
1616
"build": "tsc",
17-
"postbuild": "copyfiles --up 1 ./src/**/*.mjs ./dist/",
1817
"pretest": "tsx ./test/__scripts__/prepare.ts",
19-
"test": "node ./lint.mdx.mjs && jest",
18+
"test": "jest",
2019
"test:update": "npm run build && npm run test -- -u"
2120
},
2221
"repository": {
@@ -31,7 +30,7 @@
3130
"remark": "^15.0.1",
3231
"remark-frontmatter": "^5.0.0",
3332
"remark-gfm": "^4.0.0",
34-
"remark-mdx": "^3.0.1",
33+
"remark-mdx": "^3.1.0",
3534
"to-vfile": "^8.0.0"
3635
},
3736
"peerDependencies": {

packages/typedoc-plugin-remark/src/_typedoc.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { ManuallyValidatedOption } from 'typedoc';
33
declare module 'typedoc' {
44
export interface TypeDocOptionMap {
5+
defaultRemarkPlugins: { gfm: boolean; frontmatter: boolean; mdx: boolean };
56
remarkPlugins: ManuallyValidatedOption<Partial<any>>;
67
remarkStringifyOptions: ManuallyValidatedOption<Partial<any>>;
78
}

packages/typedoc-plugin-remark/src/options/helpers.ts renamed to packages/typedoc-plugin-remark/src/helpers/add-toc.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { DeclarationReflection, ReflectionKind } from 'typedoc';
1+
import { Application, DeclarationReflection, ReflectionKind } from 'typedoc';
2+
import { RemarkPlugin } from '../types/remark-plugin.js';
23

34
export function addTableOfContents(
45
event: any,
5-
remarkPlugins: any[],
6+
remarkPlugins: RemarkPlugin[],
67
remarkPluginsNames: string[],
7-
app: any,
8+
app: Application,
89
) {
910
const tocPluginIndex = remarkPluginsNames.findIndex(
1011
(name) => name === 'remark-toc',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { getDefaultPlugins } from './get-default-plugins';
2+
3+
describe('getDefaultPlugins', () => {
4+
it('should return an empty array when all flags are false', () => {
5+
const defaultPluginsFlag = {
6+
gfm: false,
7+
frontmatter: false,
8+
mdx: false,
9+
};
10+
11+
const result = getDefaultPlugins(defaultPluginsFlag);
12+
13+
expect(result).toEqual([]);
14+
});
15+
16+
it('should include remark-frontmatter when frontmatter flag is true', () => {
17+
const defaultPluginsFlag = {
18+
gfm: false,
19+
frontmatter: true,
20+
mdx: false,
21+
};
22+
23+
const result = getDefaultPlugins(defaultPluginsFlag);
24+
25+
expect(result).toEqual([['remark-frontmatter', ['yaml']]]);
26+
});
27+
28+
it('should include remark-gfm when gfm flag is true', () => {
29+
const defaultPluginsFlag = {
30+
gfm: true,
31+
frontmatter: false,
32+
mdx: false,
33+
};
34+
35+
const result = getDefaultPlugins(defaultPluginsFlag);
36+
37+
expect(result).toEqual(['remark-gfm']);
38+
});
39+
40+
it('should include remark-mdx when mdx flag is true', () => {
41+
const defaultPluginsFlag = {
42+
gfm: false,
43+
frontmatter: false,
44+
mdx: true,
45+
};
46+
47+
const result = getDefaultPlugins(defaultPluginsFlag);
48+
49+
expect(result).toEqual(['remark-mdx']);
50+
});
51+
52+
it('should include all plugins when all flags are true', () => {
53+
const defaultPluginsFlag = {
54+
gfm: true,
55+
frontmatter: true,
56+
mdx: true,
57+
};
58+
59+
const result = getDefaultPlugins(defaultPluginsFlag);
60+
61+
expect(result).toEqual([
62+
['remark-frontmatter', ['yaml']],
63+
'remark-gfm',
64+
'remark-mdx',
65+
]);
66+
});
67+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { RemarkPlugin } from '../types/remark-plugin.js';
2+
3+
export function getDefaultPlugins(defaultPluginsFlag: {
4+
gfm: boolean;
5+
frontmatter: boolean;
6+
mdx: boolean;
7+
}) {
8+
const defaultPlugins: RemarkPlugin[] = [];
9+
10+
if (defaultPluginsFlag.frontmatter) {
11+
defaultPlugins.push(['remark-frontmatter', ['yaml']]);
12+
}
13+
14+
if (defaultPluginsFlag.gfm) {
15+
defaultPlugins.push('remark-gfm');
16+
}
17+
18+
if (defaultPluginsFlag.mdx) {
19+
defaultPlugins.push('remark-mdx');
20+
}
21+
22+
return defaultPlugins;
23+
}

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

+14-10
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66

77
import { Application, DeclarationOption, RendererEvent } from 'typedoc';
88
import { MarkdownPageEvent } from 'typedoc-plugin-markdown';
9+
import { addTableOfContents } from './helpers/add-toc.js';
10+
import { getDefaultPlugins } from './helpers/get-default-plugins.js';
911
import * as options from './options/declarations.js';
10-
import { addTableOfContents } from './options/helpers.js';
11-
import { parseContents } from './remark.js';
12+
import { parse } from './parse.js';
1213

1314
export function load(app: Application) {
1415
Object.entries(options).forEach(([name, option]) => {
@@ -30,24 +31,27 @@ export function load(app: Application) {
3031
});
3132

3233
app.renderer.postRenderAsyncJobs.push(async (output: RendererEvent) => {
33-
const remarkPlugins = app.options.getValue('remarkPlugins') as any;
34+
const defaultPlugins = getDefaultPlugins(
35+
app.options.getValue('defaultRemarkPlugins'),
36+
);
37+
const userPlugins = app.options.getValue('remarkPlugins') as string[];
38+
const remarkStringifyOptions = app.options.getValue(
39+
'remarkStringifyOptions',
40+
);
3441
if (output.urls?.length) {
3542
await Promise.all(
3643
output.urls?.map(async (urlMapping) => {
3744
const filePath = `${output.outputDirectory}/${urlMapping.url}`;
38-
const remarkStringifyOptions = app.options.getValue(
39-
'remarkStringifyOptions',
40-
);
41-
return await parseContents(
45+
return await parse(
4246
filePath,
47+
[...defaultPlugins, ...userPlugins],
4348
remarkStringifyOptions,
44-
remarkPlugins,
4549
);
4650
}),
4751
);
48-
if (remarkPlugins.length) {
52+
if (userPlugins.length) {
4953
app.logger.info(
50-
`Output parsed using remark plugin(s) [${remarkPlugins
54+
`Output parsed using remark plugin(s) [${userPlugins
5155
.map((plugin) => `"${Array.isArray(plugin) ? plugin[0] : plugin}"`)
5256
.join(', ')}]`,
5357
);

packages/typedoc-plugin-remark/src/normalize-tables.mjs

-23
This file was deleted.

0 commit comments

Comments
 (0)