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

fix: emphasis with links #1007

Merged
merged 1 commit into from
Oct 30, 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
4 changes: 2 additions & 2 deletions __tests__/migration/emphasis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ describe('migrating emphasis', () => {

it('migrates a complex case', () => {
const md =
'*the recommended initial action is to**initiate a[reversal operation (rollback)](https://docs.jupico.com/reference/ccrollback)**. *';
'*the recommended initial action is to**initiate a [reversal operation (rollback)](https://docs.jupico.com/reference/ccrollback) test**. *';

const mdx = rmdx.mdx(rmdx.mdastV6(md));
expect(mdx).toMatchInlineSnapshot(`
"*the recommended initial action is to**initiate a[reversal operation (rollback)](https://docs.jupico.com/reference/ccrollback)**.*
"*the recommended initial action is to**initiate a [reversal operation (rollback)](https://docs.jupico.com/reference/ccrollback) test**.*
"
`);
});
Expand Down
26 changes: 21 additions & 5 deletions processor/migration/emphasis.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Emphasis, Parent, Root, Strong, Text } from 'mdast';

import { visit } from 'unist-util-visit';
import { visit, EXIT } from 'unist-util-visit';

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

Expand All @@ -27,24 +27,40 @@ const trimEmphasis = (node: Emphasis | Strong, index: number, parent: Parent) =>
let trimmed = false;

visit(node, 'text', (child: Text) => {
const newValue = child.value.trim();
const newValue = child.value.trimStart();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we have to trim the start and end separately? Oh I see now! In cases where there are multiple nodes!


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

return EXIT;
});

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

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

return EXIT;
},
true,
);

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

const emphasisTransfomer = () => (tree: Root) => {
// @ts-expect-error: the current version of visit is before the package
// types/mdast was created
visit(tree, strongTest, trimEmphasis as $TSFixMe);
visit(tree, strongTest, trimEmphasis);

return tree;
};
Expand Down
Loading