Skip to content

Commit

Permalink
feat: fix incompatable emphasis (#988)
Browse files Browse the repository at this point in the history
[![PR App][icn]][demo] | Ref CX-1135
:-------------------:|:----------:

## 🧰 Changes

Attempts to migrate more buggy emphasis syntax.

## 🧬 QA & Testing

- [Broken on production][prod].
- [Working in this PR app][demo].


[demo]: https://markdown-pr-PR_NUMBER.herokuapp.com
[prod]: https://SUBDOMAIN.readme.io
[icn]:
https://user-images.githubusercontent.com/886627/160426047-1bee9488-305a-4145-bb2b-09d8b757d38a.svg
  • Loading branch information
kellyjosephprice authored Oct 7, 2024
1 parent fca4fef commit 811fad4
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 16 deletions.
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
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

0 comments on commit 811fad4

Please sign in to comment.