Skip to content

Commit 9e9491c

Browse files
committed
Allow plugin options for both remark and rehype plugins
1 parent d06d419 commit 9e9491c

File tree

3 files changed

+65
-4
lines changed

3 files changed

+65
-4
lines changed

packages/ember-repl/addon/src/compile/formats/markdown.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,11 @@ function buildCompiler(options: ParseMarkdownOptions) {
240240
*/
241241
if (options.remarkPlugins) {
242242
options.remarkPlugins.forEach((plugin) => {
243-
compiler = compiler.use(plugin) as any;
243+
// Arrays are how plugins are passed options (for some reason?)
244+
// why not just invoke the the function?
245+
let p = Array.isArray(plugin) ? plugin : [plugin];
246+
247+
compiler = compiler.use(...(p as Parameters<typeof compiler.use>));
244248
});
245249
}
246250

@@ -296,7 +300,11 @@ function buildCompiler(options: ParseMarkdownOptions) {
296300

297301
if (options.rehypePlugins) {
298302
options.rehypePlugins.forEach((plugin) => {
299-
compiler = compiler.use(plugin) as any;
303+
// Arrays are how plugins are passed options (for some reason?)
304+
// why not just invoke the the function?
305+
let p = Array.isArray(plugin) ? plugin : [plugin];
306+
307+
compiler = compiler.use(...(p as Parameters<typeof compiler.use>));
300308
});
301309
}
302310

packages/ember-repl/addon/src/compile/types.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ComponentLike } from '@glint/template';
2-
import type { Plugin } from 'unified';
2+
import type { unified } from 'unified';
33

44
export interface EvalImportMap {
55
[moduleName: string]: ScopeMap;
@@ -9,7 +9,7 @@ export interface ScopeMap {
99
[localName: string]: unknown;
1010
}
1111

12-
export type UnifiedPlugin = Plugin; // Parameters<ReturnType<typeof unified>['use']>[0];
12+
export type UnifiedPlugin = Parameters<ReturnType<typeof unified>['use']>[0];
1313

1414
export interface CompileResult {
1515
component?: ComponentLike;

packages/ember-repl/test-app/tests/unit/markdown-test.ts

+53
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,32 @@ module('Unit | parseMarkdown()', function () {
9797
assert.deepEqual(result.blocks, []);
9898
});
9999

100+
test('remarkPlugins w/ options', async function (assert) {
101+
let result = await parseMarkdown(`# Title`, {
102+
remarkPlugins: [
103+
[
104+
function noH1(options) {
105+
return (tree) => {
106+
return visit(tree, ['heading'], function (node) {
107+
if (!('depth' in node)) return;
108+
109+
if (node.depth === 1) {
110+
node.depth = options.depth;
111+
}
112+
113+
return 'skip';
114+
});
115+
};
116+
},
117+
{ depth: 3 },
118+
],
119+
],
120+
});
121+
122+
assertOutput(result.templateOnlyGlimdown, `<h3>Title</h3>`);
123+
124+
assert.deepEqual(result.blocks, []);
125+
});
100126
test('rehypePlugins', async function (assert) {
101127
let result = await parseMarkdown(`# Title`, {
102128
rehypePlugins: [
@@ -125,6 +151,33 @@ module('Unit | parseMarkdown()', function () {
125151

126152
assert.deepEqual(result.blocks, []);
127153
});
154+
155+
test('rehypePlugins w/ options', async function (assert) {
156+
let result = await parseMarkdown(`# Title`, {
157+
rehypePlugins: [
158+
[
159+
function noH1(options) {
160+
return (tree) => {
161+
return visit(tree, ['element'], function (node) {
162+
if (!('tagName' in node)) return;
163+
164+
if (node.tagName === 'h1') {
165+
node.tagName = `h${options.depth ?? 2}`;
166+
}
167+
168+
return 'skip';
169+
});
170+
};
171+
},
172+
{ depth: 3 },
173+
],
174+
],
175+
});
176+
177+
assertOutput(result.templateOnlyGlimdown, `<h3>Title</h3>`);
178+
179+
assert.deepEqual(result.blocks, []);
180+
});
128181
});
129182

130183
module('hbs', function () {

0 commit comments

Comments
 (0)