Skip to content

Commit ff4aa28

Browse files
authored
feat: add regex to fix <br> (#1210)
[![PR App][icn]][demo] | Fix RM-XYZ :-------------------:|:----------: ## 🧰 Changes Add another regex in the migrator to replace unclosed <br> tags to closed <br/> tags ## 🧬 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
1 parent 9db6259 commit ff4aa28

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { migrate } from '../helpers';
2+
3+
describe('migrating html tags', () => {
4+
describe('br tags', () => {
5+
it('converts unclosed br tags to self-closing', () => {
6+
const md = `This is a line with a break<br>and another line.
7+
Multiple breaks<br><br>in sequence.
8+
Already closed<br />should remain unchanged.`;
9+
10+
const mdx = migrate(md);
11+
expect(mdx).toMatchInlineSnapshot(`
12+
"This is a line with a break<br />and another line.\\
13+
Multiple breaks<br /><br />in sequence.\\
14+
Already closed<br />should remain unchanged.
15+
"
16+
`);
17+
});
18+
19+
it('handles br tags with attributes', () => {
20+
const md = 'Line with styled break<br class="clear">and more text.';
21+
22+
const mdx = migrate(md);
23+
expect(mdx).toMatchInlineSnapshot(`
24+
"Line with styled break<br class="clear" />and more text.
25+
"
26+
`);
27+
});
28+
29+
it('does not change br tags inside code blocks', () => {
30+
const md = `Not a \`<br>\` tag.
31+
32+
\`\`\`
33+
Also not a \`<br>\` tag.
34+
\`\`\``;
35+
36+
const mdx = migrate(md);
37+
expect(mdx).toMatchInlineSnapshot(`
38+
"Not a \`<br>\` tag.
39+
40+
\`\`\`
41+
Also not a \`<br>\` tag.
42+
\`\`\`
43+
"
44+
`);
45+
});
46+
});
47+
});

lib/migrate.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import migrateCallouts from '../processor/transform/migrate-callouts';
2+
import migrateHtmlTags from '../processor/transform/migrate-html-tags';
23
import migrateLinkReferences from '../processor/transform/migrate-link-references';
34

45
import mdastV6 from './mdastV6';
@@ -8,7 +9,7 @@ const migrate = (doc: string, { rdmd }): string => {
89
const ast = mdastV6(doc, { rdmd });
910

1011
return (
11-
mdx(ast, { remarkTransformers: [migrateCallouts, migrateLinkReferences], file: doc })
12+
mdx(ast, { remarkTransformers: [migrateCallouts, migrateLinkReferences, migrateHtmlTags], file: doc })
1213
.replaceAll(/&#x20;/g, ' ')
1314
// @note: I'm not sure what's happening, but I think mdx is converting an
1415
// 'a' to '&#x61;' as a means of escaping it. I think this helps with
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import type { Root } from 'mdast';
2+
3+
import { visit } from 'unist-util-visit';
4+
5+
// Add more visits to migrate other HTML tags here
6+
const migrateHtmlTags = () => (tree: Root) => {
7+
// A common issue is that <br> tags are not properly closed
8+
visit(tree, 'html', htmlNode => {
9+
htmlNode.value = htmlNode.value.replaceAll(/<br(?!\s*\/>)([^>]*?)>/g, '<br$1 />');
10+
});
11+
};
12+
13+
export default migrateHtmlTags;

0 commit comments

Comments
 (0)