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

chore(rdmd to rmdx): wrap scripts + styles in an <HTMLBlock> #954

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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,70 @@

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

describe('<HTMLBlock> wrapping', () => {
configure({ defaultIgnore: undefined });
const inspect = (node?: HTMLElement, min = false) => {
const filterNode = (node: Node) => true;
console.log(prettyDOM(node, undefined, { filterNode, min }));
};
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 <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 not render raw <script> tags', 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 execute <script> tags in compatibility mode', async () => {
const spy = vi.spyOn(console, 'log');
const legacyAST = rdmd.mdast(magicScript);
const converted = mdx(legacyAST);
const compiled = compile(converted);
const Component = await run(compiled, {
// compatibilityMode: true // not sure how/where to pass these global options anymore!
});
render(
<div className="markdown-body">
<Component.default />
</div>,
);
expect(spy).toHaveBeenCalledWith('hello magic');

Check failure on line 269 in __tests__/compilers/compatability.test.tsx

View workflow job for this annotation

GitHub Actions / Test (lts/-1, 16)

__tests__/compilers/compatability.test.tsx > compatability with RDMD > <HTMLBlock> wrapping > should execute <script> tags in compatibility mode

AssertionError: expected "log" to be called with arguments: [ 'hello magic' ] Received: Number of calls: 0 ❯ __tests__/compilers/compatability.test.tsx:269:19

Check failure on line 269 in __tests__/compilers/compatability.test.tsx

View workflow job for this annotation

GitHub Actions / Test (lts/-1, 17)

__tests__/compilers/compatability.test.tsx > compatability with RDMD > <HTMLBlock> wrapping > should execute <script> tags in compatibility mode

AssertionError: expected "log" to be called with arguments: [ 'hello magic' ] Received: Number of calls: 0 ❯ __tests__/compilers/compatability.test.tsx:269:19

Check failure on line 269 in __tests__/compilers/compatability.test.tsx

View workflow job for this annotation

GitHub Actions / Test (lts/-1, 18)

__tests__/compilers/compatability.test.tsx > compatability with RDMD > <HTMLBlock> wrapping > should execute <script> tags in compatibility mode

AssertionError: expected "log" to be called with arguments: [ 'hello magic' ] Received: Number of calls: 0 ❯ __tests__/compilers/compatability.test.tsx:269:19
});
});
});
18 changes: 18 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 { 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,23 @@
}
});

visit(tree, 'html', (node, index, parent) => {
const html = node.value;
const isScriptOrStyleTag = [!!html.match(/^<(?:style|script).*?>/), !!html.match(/<\/(?:style|script)>$/)];
Fixed Show fixed Hide fixed
if (!isScriptOrStyleTag.includes(true)) return;
parent.children.splice(index, 1, {
type: 'html-block',
children: [{ type: 'text', value: html }],
data: {
hName: 'html-block',
hProperties: {
html,
// runScripts: options.compatibilityMode, // not sure how to access the global options anymore!
},
},
} as HTMLBlock);
});

return tree;
};

Expand Down
Loading