Skip to content

Commit 66dea4f

Browse files
committed
fix(remark): resolve local plugins from process.cwd()
1 parent 66cf898 commit 66dea4f

File tree

9 files changed

+42
-11
lines changed

9 files changed

+42
-11
lines changed

.changeset/bright-sloths-raise.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'typedoc-plugin-remark': patch
3+
---
4+
5+
- Resolve local plugins from `process.cwd()`.

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Application, DeclarationOption, RendererEvent } from 'typedoc';
88
import { MarkdownPageEvent } from 'typedoc-plugin-markdown';
99
import * as options from './options/declarations.js';
1010
import { addTableOfContents } from './options/helpers.js';
11+
import { parseContents } from './remark.js';
1112

1213
export function load(app: Application) {
1314
Object.entries(options).forEach(([name, option]) => {
@@ -33,7 +34,6 @@ export function load(app: Application) {
3334
if (output.urls?.length) {
3435
await Promise.all(
3536
output.urls?.map(async (urlMapping) => {
36-
const { parseContents } = await import('./remark.js');
3737
const filePath = `${output.outputDirectory}/${urlMapping.url}`;
3838
const remarkStringifyOptions = app.options.getValue(
3939
'remarkStringifyOptions',

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

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1+
import * as path from 'path';
12
import { remark } from 'remark';
23
import { read, writeSync } from 'to-vfile';
34

4-
/**
5-
* Parses contents with Remark
6-
* - The remark eco-system is esm only, therefore we have to import its modules synchronously.
7-
* - In addition we don't want Typescript to transpile this file.
8-
*/
95
export async function parseContents(
106
filePath: string,
117
remarkStringifyOptions = {},
@@ -23,8 +19,14 @@ export async function parseContents(
2319
const promises = plugins.map(async (plugin) => {
2420
return new Promise((resolve) => {
2521
const name = Array.isArray(plugin) ? plugin[0] : plugin;
22+
const isLocalPath =
23+
name !== './normalize-tables.mjs' &&
24+
/^\.{1,2}\/|^\//.test(name as string);
25+
const fullPath = isLocalPath
26+
? path.resolve(process.cwd(), name as string)
27+
: name;
2628
const options = Array.isArray(plugin) ? plugin[1] : {};
27-
import(name as string).then(({ default: pluginFn }) => {
29+
import(fullPath as string).then(({ default: pluginFn }) => {
2830
resolve({
2931
pluginFn,
3032
options,
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
function load(app) {
1+
export function load(app) {
22
app.renderer.markdownHooks.on(
33
'page.begin',
44
() => `---\ntitle: "test"\n---\n`,
55
);
66
}
7-
exports.load = load;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { visit } from 'unist-util-visit';
2+
3+
export default function remarkAddKeyword() {
4+
return (tree) => {
5+
visit(tree, 'code', (node) => {
6+
node.meta = `playground`;
7+
});
8+
};
9+
}

packages/typedoc-plugin-remark/test/specs/__snapshots__/remark.spec.ts.snap

+8
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ Comments form module comments
183183
184184
Thanks [**@tgrey**](https://github.com/tgrey) please see issue [#1](https://github.com/typedoc2md/typedoc-plugin-markdown/issues/1).
185185
186+
\`\`\`js
187+
const x = 1
188+
\`\`\`
189+
186190
## Index
187191
188192
### Classes
@@ -220,6 +224,10 @@ Comments form module comments
220224
221225
Thanks [**@tgrey**](https://github.com/tgrey) please see issue [#1](https://github.com/typedoc2md/typedoc-plugin-markdown/issues/1).
222226
227+
~~~js playground
228+
const x = 1
229+
~~~
230+
223231
## Contents
224232
225233
+ [Classes](#classes)

packages/typedoc-plugin-remark/test/stubs/module-1.ts

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
*
44
* Thanks \@tgrey please see issue #1.
55
*
6+
* ```js
7+
* const x = 1
8+
* ```
9+
*
610
* @module
711
*/
812

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"plugin": [
66
"typedoc-plugin-markdown",
77
"typedoc-plugin-remark",
8-
"./frontmatter-plugin.cjs"
8+
"./frontmatter-plugin.mjs"
99
],
1010
"readme": "none",
1111
"parametersFormat": "htmlTable",

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,9 @@
33
"entryPoints": ["./stubs/*.ts"],
44
"outputFileStrategy": "modules",
55
"remarkStringifyOptions": { "bullet": "+", "fence": "~" },
6-
"remarkPlugins": ["remark-github", ["remark-toc", { "maxDepth": 3 }]]
6+
"remarkPlugins": [
7+
"remark-github",
8+
["remark-toc", { "maxDepth": 3 }],
9+
"./test/local-plugin.mjs"
10+
]
711
}

0 commit comments

Comments
 (0)