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

feat: fix incompatable emphasis #988

Merged
merged 2 commits into from
Oct 7, 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
11 changes: 9 additions & 2 deletions __tests__/compilers/compatability.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,6 @@ This is an image: <img src="http://example.com/#\\>" >
});

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

const rawStyle = `<style data-testid="style-tag">
p {
color: red;
Expand Down Expand Up @@ -332,6 +330,15 @@ This is an image: <img src="http://example.com/#\\>" >
`);
});

it('moves whitespace surrounding phrasing content (emphasis, strong, etc) to the appropriate place', () => {
const md = `**bold **and also_ italic_ and*** bold italic***aaaaaah`;

const rmdx = mdx(rdmd.mdast(md));
expect(rmdx).toMatchInlineSnapshot(`
"**bold** and also *italic* and ***bold italic***aaaaaah
"
`);
});

it('correctly parses and transforms image magic block with legacy data', () => {
const md = `
Expand Down
54 changes: 40 additions & 14 deletions processor/transform/compatability.ts
rafegoldberg marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,25 +1,51 @@
import { Emphasis, Image, Strong, Node, Parent } from 'mdast';
import { EXIT, SKIP, visit } from 'unist-util-visit';
import { Transform } from 'mdast-util-from-markdown';
import { phrasing } from 'mdast-util-phrasing';
import { visit } from 'unist-util-visit';

const strongTest = (node: Node): node is Strong | Emphasis => ['emphasis', 'strong'].includes(node.type);

const compatibilityTransfomer = (): Transform => tree => {
const trimEmphasis = (node: Emphasis | Strong) => {
visit(node, 'text', child => {
child.value = child.value.trim();
return EXIT;
});

return node;
};

visit(tree, strongTest, node => {
trimEmphasis(node);
return SKIP;
const addSpaceBefore = (index: number, parent: Parent) => {
if (!(index > 0 && parent.children[index - 1])) return;

const prev = parent.children[index - 1];
if (!('value' in prev) || prev.value.endsWith(' ') || prev.type === 'escape') return;

parent.children.splice(index, 0, { type: 'text', value: ' ' });
};

const addSpaceAfter = (index: number, parent: Parent) => {
if (!(index < parent.children.length - 1 && parent.children[index + 1])) return;

const nextChild = parent.children[index + 1];
if (!('value' in nextChild) || nextChild.value.startsWith(' ')) return;

parent.children.splice(index + 1, 0, { type: 'text', value: ' ' });
};

const trimEmphasis = (node: Emphasis | Strong, index: number, parent: Parent) => {
let trimmed = false;

visit(node, 'text', child => {
const newValue = child.value.trim();

if (newValue !== child.value) {
trimmed = true;
child.value = newValue;
}
});

if (trimmed) {
addSpaceBefore(index, parent);
addSpaceAfter(index, parent);
}

return node;
};

const compatibilityTransfomer = (): Transform => tree => {
visit(tree, strongTest, trimEmphasis);

Check failure on line 47 in processor/transform/compatability.ts

View workflow job for this annotation

GitHub Actions / Bundle Watch

No overload matches this call.

visit(tree, 'image', (node: Image, index: number, parent: Parent) => {
if (phrasing(parent) || !parent.children.every(child => child.type === 'image' || !phrasing(child))) return;

Expand Down
Loading