Skip to content

Commit

Permalink
Merge branch 'next'
Browse files Browse the repository at this point in the history
  • Loading branch information
kellyjosephprice committed Sep 10, 2024
2 parents 873beb0 + fb4fb7d commit 696a69d
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 3 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ A module ([`RMDXModule`](#rmdxmodule)) of renderable components

Compiles an ast to mdx.

###### Parameters

Extends [`remark-stringify` options](https://github.com/remarkjs/remark/tree/main/packages/remark-stringify#options).

###### Returns

An mdx string.

#### `mdast`

Parses mdx to an mdast.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { mdx } from '../../index';
import React from 'react';
import fs from 'node:fs';
import { vi } from 'vitest';
import { render, screen, prettyDOM, configure } from '@testing-library/react';

import { mdx, compile, run } from '../../index';
import * as rdmd from '@readme/markdown-legacy';

describe('compatability with RDMD', () => {
Expand Down Expand Up @@ -199,4 +203,68 @@ This is an image: <img src="http://example.com/#\\>" >

expect(mdx(rdmd.mdast(md))).toBe(`Contact me at {user.email}\n`);
});

describe('<HTMLBlock> wrapping', () => {
// configure({ defaultIgnore: undefined });

const rawStyle = `<style data-testid="style-tag">
p {
color: red;
}
</style>
`;
const rawScript = `<script data-testid="script-tag">
console.log('hello raw')
</script>
`;
const magicScript = `[block:html]
{
"html": "<script data-testid='script-block'>console.log('hello magic')</script>"
}
[/block]
`;

it('should wrap raw <style> tags in an <HTMLBlock>', async () => {
const legacyAST = rdmd.mdast(rawStyle);
const converted = mdx(legacyAST);
const compiled = compile(converted);
const Component = (await run(compiled)).default;
render(
<div className="markdown-body">
<Component />
</div>,
);
expect(screen.getByTestId('style-tag').tagName).toMatch('STYLE');
});

it('should wrap raw <script> tags in an <HTMLBlock>', async () => {
const legacyAST = rdmd.mdast(rawScript);
const converted = mdx(legacyAST);
const compiled = compile(converted);
const Component = (await run(compiled)).default;
render(
<div className="markdown-body">
<Component />
</div>,
);
expect(screen.queryByTestId('script-tag')).toBe(null);
});

it('should never execute <script> tags', async () => {
/**
* @note compatability mode has been deprecated for RMDX
*/
const spy = vi.spyOn(console, 'log');
const legacyAST = rdmd.mdast(magicScript);
const converted = mdx(legacyAST);
const compiled = compile(converted);
const Component = await run(compiled);
render(
<div className="markdown-body">
<Component.default />
</div>,
);
expect(spy).toHaveBeenCalledTimes(0);
});
});
});
4 changes: 2 additions & 2 deletions lib/mdx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import remarkStringify from 'remark-stringify';
import compilers from '../processor/compile';
import { divTransformer, readmeToMdx, tablesToJsx } from '../processor/transform';

export const mdx = (tree: any, { hast = false } = {}) => {
export const mdx = (tree: any, { hast = false, ...opts } = {}) => {
const processor = unified()
.use(hast ? rehypeRemark : undefined)
.use(remarkMdx)
Expand All @@ -16,7 +16,7 @@ export const mdx = (tree: any, { hast = false } = {}) => {
.use(readmeToMdx)
.use(tablesToJsx)
.use(compilers)
.use(remarkStringify);
.use(remarkStringify, opts);

return processor.stringify(processor.runSync(tree));
};
Expand Down
15 changes: 15 additions & 0 deletions processor/transform/readme-to-mdx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { MdxJsxAttribute } from 'mdast-util-mdx-jsx';

import { visit } from 'unist-util-visit';
import { toAttributes } from '../utils';
import { type HTMLBlock } from 'types';

const imageAttrs = ['align', 'alt', 'caption', 'border', 'src', 'title', 'width', 'lazy', 'className'];

Expand Down Expand Up @@ -75,6 +76,20 @@ const readmeToMdx = (): Transform => tree => {
}
});

visit(tree, 'html', (node, index, parent) => {
const html = node.value;
const isScriptOrStyleTag = [!!html.match(/^<(?:style|script)/i), !!html.match(/<\/(?:style|script)>$/i)];
if (!isScriptOrStyleTag.includes(true)) return;
parent.children.splice(index, 1, {
type: 'html-block',
children: [{ type: 'text', value: html }],
data: {
hName: 'html-block',
hProperties: { html },
},
} as HTMLBlock);
});

return tree;
};

Expand Down

0 comments on commit 696a69d

Please sign in to comment.