Skip to content

Commit 886dadc

Browse files
Allow plugin options for both remark and rehype plugins (#1699)
* Allow plugin options for both remark and rehype plugins * Fix types
1 parent d06d419 commit 886dadc

File tree

4 files changed

+70
-9
lines changed

4 files changed

+70
-9
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 [any]));
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 [any]));
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 { Pluggable } 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 = Pluggable;
1313

1414
export interface CompileResult {
1515
component?: ComponentLike;

packages/ember-repl/test-app/tests/rendering/markdown-test.gts

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import { setupRenderingTest } from 'ember-qunit';
66
import { stripIndent } from 'common-tags';
77
import { compile } from 'ember-repl';
88
import { CACHE } from 'ember-repl/__PRIVATE__DO_NOT_USE__';
9-
import { type Plugin } from 'unified';
109
import { visit } from 'unist-util-visit';
1110

1211
import type { ComponentLike } from '@glint/template';
12+
import type { PluggableList } from 'unified';
1313
import type { Parent } from 'unist';
1414

15-
export type UnifiedPlugin = Plugin; // Parameters<ReturnType<(typeof unified)>['use']>[0];
15+
export type UnifiedPlugin = PluggableList[0];
1616
type Build = (plugin?: UnifiedPlugin) => Promise<void>;
1717

1818
module('Rendering | compile()', function (hooks) {
@@ -98,7 +98,7 @@ module('Rendering | compile()', function (hooks) {
9898
* unformatted mess in a p tag
9999
*/
100100
const uncodeSnippet: UnifiedPlugin = (/* options */) => {
101-
return function transformer(tree) {
101+
return function transformer(tree: Parameters<typeof visit>[0]) {
102102
visit(tree, ['code'], function (node, index, parent: Parent) {
103103
if (!parent) return;
104104
if (undefined === index) return;
@@ -263,8 +263,8 @@ module('Rendering | compile()', function (hooks) {
263263

264264
remarkPlugins: [
265265
function noH1(/* options */) {
266-
return (tree) => {
267-
return visit(tree, ['heading'], function (node) {
266+
return (tree: Parameters<typeof visit>[0]) => {
267+
visit(tree, ['heading'], function (node) {
268268
if (!('depth' in node)) return;
269269

270270
if (node.depth === 1) {

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: { depth: number }) {
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: { depth: number }) {
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)