Skip to content

Commit

Permalink
fix: use slug instead of name (#815)
Browse files Browse the repository at this point in the history
Switching reusable contents attr from `name` to `slug` to match the backend.
  • Loading branch information
kellyjosephprice authored Sep 7, 2023
1 parent aa121e1 commit 48b7ecd
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG NODE_VERSION=14
ARG NODE_VERSION=18
FROM node:${NODE_VERSION}-buster

ARG NODE_VERSION
Expand Down
4 changes: 2 additions & 2 deletions __tests__/flavored-compilers/reusable-content.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { mdast, md } from '../../index';

describe('reusable content compiler', () => {
it('writes an undefined reusable content block back to markdown', () => {
const doc = '<RMReusableContent name="undefined" />';
const doc = '<RMReusableContent slug="undefined" />';
const tree = mdast(doc);

expect(md(tree)).toMatch(doc);
Expand All @@ -12,7 +12,7 @@ describe('reusable content compiler', () => {
const reusableContent = {
defined: '# Whoa',
};
const doc = '<RMReusableContent name="defined" />';
const doc = '<RMReusableContent slug="defined" />';
const tree = mdast(doc, { reusableContent });

expect(tree.children[0].children[0].type).toBe('heading');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Object {
"type": "paragraph",
},
],
"name": "test",
"slug": "test",
"type": "reusable-content",
}
`;
8 changes: 4 additions & 4 deletions __tests__/transformers/reusable-content.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('reusable content transfomer', () => {
const md = `
Before
<RMReusableContent name="test" />
<RMReusableContent slug="test" />
After
`;
Expand All @@ -25,7 +25,7 @@ After
});

it('should insert an empty node if the reusable content block is not defined', () => {
const md = '<RMReusableContent name="not-defined" />';
const md = '<RMReusableContent slug="not-defined" />';
const tree = mdast(md);

expect(tree.children[0].type).toBe('reusable-content');
Expand All @@ -34,9 +34,9 @@ After

it('does not expand reusable content recursively', () => {
const reusableContent = {
test: '<RMReusableContent name="test" />',
test: '<RMReusableContent slug="test" />',
};
const md = '<RMReusableContent name="test" />';
const md = '<RMReusableContent slug="test" />';
const tree = mdast(md, { reusableContent });

expect(tree.children[0].children[0].type).toBe('reusable-content');
Expand Down
2 changes: 1 addition & 1 deletion processor/compile/reusable-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ export default function ReusableContentCompiler() {
const { Compiler } = this;
const { visitors } = Compiler.prototype;

visitors[type] = node => `<${tag} name="${node.name}" />`;
visitors[type] = node => `<${tag} slug="${node.slug}" />`;
}
10 changes: 5 additions & 5 deletions processor/transform/reusable-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ import { visit } from 'unist-util-visit';
export const type = 'reusable-content';
export const tag = 'RMReusableContent';

const regexp = new RegExp(`^\\s*<${tag} name="(?<name>.*)" />\\s*$`);
const regexp = new RegExp(`^\\s*<${tag} slug="(?<slug>.*)" />\\s*$`);

const reusableContentTransformer = function () {
const reusableContent = this.data('reusableContent');

return tree => {
visit(tree, 'html', (node, index, parent) => {
const result = regexp.exec(node.value);
if (!result || !result.groups.name) return;
if (!result || !result.groups.slug) return;

const { name } = result.groups;
const { slug } = result.groups;
const block = {
type,
name,
children: name in reusableContent ? reusableContent[name] : [],
slug,
children: slug in reusableContent ? reusableContent[slug] : [],
};

parent.children[index] = block;
Expand Down

0 comments on commit 48b7ecd

Please sign in to comment.