Skip to content

Commit

Permalink
Merge branch 'next'
Browse files Browse the repository at this point in the history
  • Loading branch information
kellyjosephprice committed Apr 8, 2024
2 parents cb652c6 + 9372f59 commit 5d8db5d
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 9 deletions.
55 changes: 55 additions & 0 deletions __tests__/transformers/reusable-content.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { mdast } from '../../index';

describe('reusable content transfomer', () => {
it('should unwrap the content when `wrap: false`', () => {
const tags = {
Test: `
# Test
[link](http://example.com)
`,
};
const md = `
Before
<Test />
After
`;

const tree = mdast(md, { reusableContent: { tags, wrap: false } });

expect(tree.children[0].children[0].value).toBe('Before');
expect(tree.children[1].type).toBe('heading');
expect(tree.children[2].type).toBe('paragraph');
expect(tree.children[3].children[0].value).toBe('After');
});

it('should unwrap consecutive the content when `wrap: false`', () => {
const tags = {
Test: `
# Test
[link](http://example.com)
`,
};
const md = `
Before
<Test />
<Test />
After
`;

const tree = mdast(md, { reusableContent: { tags, wrap: false } });

expect(tree.children[0].children[0].value).toBe('Before');
expect(tree.children[1].type).toBe('heading');
expect(tree.children[2].type).toBe('paragraph');
expect(tree.children[3].type).toBe('heading');
expect(tree.children[4].type).toBe('paragraph');
expect(tree.children[5].children[0].value).toBe('After');
});
});
14 changes: 6 additions & 8 deletions processor/parse/reusable-content-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { insertBlockTokenizerBefore } = require('./utils');
export const type = 'reusable-content';

function tokenizeReusableContent(eat, value, silent) {
const { tags, disabled, wrap = true } = this.data('reusableContent');
const { tags, disabled } = this.data('reusableContent');
if (disabled) return false;

// Modifies the regular expression to match from
Expand All @@ -16,13 +16,11 @@ function tokenizeReusableContent(eat, value, silent) {
/* istanbul ignore if */
if (silent) return true;

const node = wrap
? {
type: 'reusable-content',
tag,
children: tag in tags ? tags[tag] : [],
}
: tags[tag];
const node = {
type: 'reusable-content',
tag,
children: tag in tags ? tags[tag] : [],
};

return eat(match[0])(node);
}
Expand Down
3 changes: 2 additions & 1 deletion processor/transform/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import reusableContent from './reusable-content';
import singleCodeTabs from './single-code-tabs';
import tableCellInlineCode from './table-cell-inline-code';

export const remarkTransformers = [singleCodeTabs];
export const remarkTransformers = [singleCodeTabs, reusableContent];
export const rehypeTransformers = [tableCellInlineCode];
19 changes: 19 additions & 0 deletions processor/transform/reusable-content.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { visit } from 'unist-util-visit';

import { type } from '../parse/reusable-content-parser';

function reusableContent() {
const { wrap = true } = this.data('reusableContent');

return tree => {
if (wrap) return tree;

visit(tree, type, (node, index, parent) => {
parent.children.splice(index, 1, ...node.children);
});

return tree;
};
}

export default reusableContent;

0 comments on commit 5d8db5d

Please sign in to comment.