Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow plugin options for both remark and rehype plugins #1699

Merged
merged 2 commits into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions packages/ember-repl/addon/src/compile/formats/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,11 @@ function buildCompiler(options: ParseMarkdownOptions) {
*/
if (options.remarkPlugins) {
options.remarkPlugins.forEach((plugin) => {
compiler = compiler.use(plugin) as any;
// Arrays are how plugins are passed options (for some reason?)
// why not just invoke the the function?
let p = Array.isArray(plugin) ? plugin : [plugin];

compiler = compiler.use(...(p as [any]));
});
}

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

if (options.rehypePlugins) {
options.rehypePlugins.forEach((plugin) => {
compiler = compiler.use(plugin) as any;
// Arrays are how plugins are passed options (for some reason?)
// why not just invoke the the function?
let p = Array.isArray(plugin) ? plugin : [plugin];

compiler = compiler.use(...(p as [any]));
});
}

Expand Down
4 changes: 2 additions & 2 deletions packages/ember-repl/addon/src/compile/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ComponentLike } from '@glint/template';
import type { Plugin } from 'unified';
import type { Pluggable } from 'unified';

export interface EvalImportMap {
[moduleName: string]: ScopeMap;
Expand All @@ -9,7 +9,7 @@ export interface ScopeMap {
[localName: string]: unknown;
}

export type UnifiedPlugin = Plugin; // Parameters<ReturnType<typeof unified>['use']>[0];
export type UnifiedPlugin = Pluggable;

export interface CompileResult {
component?: ComponentLike;
Expand Down
10 changes: 5 additions & 5 deletions packages/ember-repl/test-app/tests/rendering/markdown-test.gts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import { setupRenderingTest } from 'ember-qunit';
import { stripIndent } from 'common-tags';
import { compile } from 'ember-repl';
import { CACHE } from 'ember-repl/__PRIVATE__DO_NOT_USE__';
import { type Plugin } from 'unified';
import { visit } from 'unist-util-visit';

import type { ComponentLike } from '@glint/template';
import type { PluggableList } from 'unified';
import type { Parent } from 'unist';

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

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

remarkPlugins: [
function noH1(/* options */) {
return (tree) => {
return visit(tree, ['heading'], function (node) {
return (tree: Parameters<typeof visit>[0]) => {
visit(tree, ['heading'], function (node) {
if (!('depth' in node)) return;

if (node.depth === 1) {
Expand Down
53 changes: 53 additions & 0 deletions packages/ember-repl/test-app/tests/unit/markdown-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,32 @@ module('Unit | parseMarkdown()', function () {
assert.deepEqual(result.blocks, []);
});

test('remarkPlugins w/ options', async function (assert) {
let result = await parseMarkdown(`# Title`, {
remarkPlugins: [
[
function noH1(options: { depth: number }) {
return (tree) => {
return visit(tree, ['heading'], function (node) {
if (!('depth' in node)) return;

if (node.depth === 1) {
node.depth = options.depth;
}

return 'skip';
});
};
},
{ depth: 3 },
],
],
});

assertOutput(result.templateOnlyGlimdown, `<h3>Title</h3>`);

assert.deepEqual(result.blocks, []);
});
test('rehypePlugins', async function (assert) {
let result = await parseMarkdown(`# Title`, {
rehypePlugins: [
Expand Down Expand Up @@ -125,6 +151,33 @@ module('Unit | parseMarkdown()', function () {

assert.deepEqual(result.blocks, []);
});

test('rehypePlugins w/ options', async function (assert) {
let result = await parseMarkdown(`# Title`, {
rehypePlugins: [
[
function noH1(options: { depth: number }) {
return (tree) => {
return visit(tree, ['element'], function (node) {
if (!('tagName' in node)) return;

if (node.tagName === 'h1') {
node.tagName = `h${options.depth ?? 2}`;
}

return 'skip';
});
};
},
{ depth: 3 },
],
],
});

assertOutput(result.templateOnlyGlimdown, `<h3>Title</h3>`);

assert.deepEqual(result.blocks, []);
});
});

module('hbs', function () {
Expand Down
Loading