Skip to content

Commit 26b13e6

Browse files
Allow passing rehype plugins to the markdown renderer in ember-repl (#1687)
* Allow passing rehype plugins to the markdown renderer in ember-repl * Add tests
1 parent 382e762 commit 26b13e6

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,12 @@ function buildCompiler(options: ParseMarkdownOptions) {
294294
});
295295
});
296296

297+
if (options.rehypePlugins) {
298+
options.rehypePlugins.forEach((plugin) => {
299+
compiler = compiler.use(plugin) as any;
300+
});
301+
}
302+
297303
compiler = compiler.use(rehypeRaw, { passThrough: ['glimmer_raw', 'raw'] }).use(() => (tree) => {
298304
visit(tree, 'glimmer_raw', (node: Node) => {
299305
node.type = 'raw';
@@ -316,6 +322,7 @@ interface ParseMarkdownOptions {
316322
CopyComponent?: string;
317323
ShadowComponent?: string;
318324
remarkPlugins?: UnifiedPlugin[];
325+
rehypePlugins?: UnifiedPlugin[];
319326
}
320327

321328
/**

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

+62-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { module, test } from 'qunit';
33
import { stripIndent } from 'common-tags';
44
import { invocationOf, nameFor } from 'ember-repl';
55
import { parseMarkdown } from 'ember-repl/formats/markdown';
6+
import { visit } from 'unist-util-visit';
67

78
/**
89
* NOTE: there is a problem(?) with remark-hbs where all extra newlines and
@@ -66,8 +67,68 @@ module('Unit | parseMarkdown()', function () {
6667
assert.deepEqual(result.blocks, []);
6768
});
6869

70+
module('plugin options', function () {
71+
test('remarkPlugins', async function (assert) {
72+
let result = await parseMarkdown(`# Title`, {
73+
remarkPlugins: [
74+
function noH1(/* options */) {
75+
return (tree) => {
76+
return visit(tree, ['heading'], function (node) {
77+
if (!('depth' in node)) return;
78+
79+
if (node.depth === 1) {
80+
node.depth = 2;
81+
}
82+
83+
return 'skip';
84+
});
85+
};
86+
},
87+
],
88+
});
89+
90+
assertOutput(
91+
result.templateOnlyGlimdown,
92+
stripIndent`
93+
<h2>Title</h2>
94+
`
95+
);
96+
97+
assert.deepEqual(result.blocks, []);
98+
});
99+
100+
test('rehypePlugins', async function (assert) {
101+
let result = await parseMarkdown(`# Title`, {
102+
rehypePlugins: [
103+
function noH1(/* options */) {
104+
return (tree) => {
105+
return visit(tree, ['element'], function (node) {
106+
if (!('tagName' in node)) return;
107+
108+
if (node.tagName === 'h1') {
109+
node.tagName = 'h2';
110+
}
111+
112+
return 'skip';
113+
});
114+
};
115+
},
116+
],
117+
});
118+
119+
assertOutput(
120+
result.templateOnlyGlimdown,
121+
stripIndent`
122+
<h2>Title</h2>
123+
`
124+
);
125+
126+
assert.deepEqual(result.blocks, []);
127+
});
128+
});
129+
69130
module('hbs', function () {
70-
test('Code fence is live', async function (assert) {
131+
test('Codecontainer fence is live', async function (assert) {
71132
let snippet = `{{concat "hello" " " "there"}}`;
72133
let name = nameFor(snippet);
73134
let result = await parseMarkdown(

0 commit comments

Comments
 (0)