Skip to content

Commit

Permalink
fix: better u tag support (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
kptdobe committed Sep 15, 2022
1 parent 0a0aadc commit 6ffb3a3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
23 changes: 18 additions & 5 deletions src/importer/PageImporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,18 @@ export default class PageImporter {
},
u: (h, node) => {
if (node.children && node.children.length > 0) {
const children = node.children.map((child) => processor.stringify(child).trim());
const children = node.children.map((child) => {
try {
if (child.type === 'element' && child.tagName !== 'span') {
const n = h(child, child.tagName, child.children);
return processor.stringify(n).trim();
}
return processor.stringify(child).trim();
} catch (e) {
// cannot stringify the node, return html
return toHtml(child);
}
});
return h(node, 'html', `<u>${children.join()}</u>`);
}
return '';
Expand Down Expand Up @@ -194,11 +205,13 @@ export default class PageImporter {
].forEach((tag) => DOMUtils.reviewInlineElement(document, tag));

// u a tag combo is not handled properly by unified js and is discouraged anyway -> remove the u
document.querySelectorAll('u > a').forEach((a) => {
const p = a.parentNode;
p.before(a);
p.remove();
const us = [];
document.querySelectorAll('u > a, u > span > a').forEach((a) => {
const u = a.closest('u');
u.before(a);
us.push(u);
});
us.forEach((u) => u.remove());

const imgs = document.querySelectorAll('img');
imgs.forEach((img) => {
Expand Down
3 changes: 3 additions & 0 deletions test/importers/fixtures/u.spec.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ <h1>Underline combo sample</h1>
<p>
<u><a href="https:/www.sample.com/a">Unlined link</a></u> or <a href="https:/www.sample.com/b"><u>Linked underline</u></a> ?
</p>
<p><u><strong>Some underline and strong text</strong></u></p>
<p><u><a href="https://www.austinparks.org/" adhocenable="false">U and A are not friends</a>,&nbsp;<a href="http://www.bgcaustin.org/" adhocenable="false">Boys &amp; Girls Clubs of the Austin Area</a>,&nbsp;<a href="http://www.thefirstteeaustin.org/club/scripts/public/public.asp" adhocenable="false">The First Tee of Greater Austin</a>&nbsp;</u></p>
<p><u><span class="theme-color"><a href="https://www.austinparks.org/" adhocenable="false">U and A are not friends</a>,&nbsp;<a href="http://www.bgcaustin.org/" adhocenable="false">Boys &amp; Girls Clubs of the Austin Area</a>,&nbsp;<a href="http://www.thefirstteeaustin.org/club/scripts/public/public.asp" adhocenable="false">The First Tee of Greater Austin</a>&nbsp;</span></u></p>
</body>
</html>
8 changes: 7 additions & 1 deletion test/importers/fixtures/u.spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,10 @@ Some normal text with random <u>underline</u> or <u>span with underline</u> or <

- <u>li underline 3</u>

[Unlined link](https:/www.sample.com/a) or [<u>Linked underline</u>](https:/www.sample.com/b) ?
[Unlined link](https:/www.sample.com/a) or [<u>Linked underline</u>](https:/www.sample.com/b) ?

<u>**Some underline and strong text**</u>

[U and A are not friends](https://www.austinparks.org/)[Boys & Girls Clubs of the Austin Area](http://www.bgcaustin.org/)[The First Tee of Greater Austin](http://www.thefirstteeaustin.org/club/scripts/public/public.asp)

[U and A are not friends](https://www.austinparks.org/)[Boys & Girls Clubs of the Austin Area](http://www.bgcaustin.org/)[The First Tee of Greater Austin](http://www.thefirstteeaustin.org/club/scripts/public/public.asp)

0 comments on commit 6ffb3a3

Please sign in to comment.