Skip to content
Merged
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
16 changes: 10 additions & 6 deletions src/components/RenderHTML.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ import ULRenderer from './HTMLEngineProvider/HTMLRenderers/ULRenderer';

type LinkPressHandler = NonNullable<RenderersProps['a']>['onPress'];

// Matches &amp;#91; (→ "[") and &amp;#93; (→ "]"). Index 7 is the distinguishing digit ('1' vs '3').
const RE_BRACKET_ESCAPE = /&amp;#9[13];/g;
// Matches consecutive duplicate <emoji> or </emoji> tags, keeping only the outermost one.
const RE_EMOJI_OPEN_OR_CLOSE = /(<emoji[^>]*>)(?:<emoji[^>]*>)+|(<\/emoji[^>]*>)(?:<\/emoji[^>]*>)+/g;
// Strips orphaned <br/> tags inside <ul> that would render as extra empty bullets.
const RE_BR_CLEANUP = /<br\s*\/?>\s*(<\/ul>)|(<\/li>)\s*<br\s*\/?>\s*(?=<(?:li|\/ul)>)/gi;

type RenderHTMLProps = {
/** HTML string to render */
html: string;
Expand All @@ -36,14 +43,11 @@ function RenderHTML({html: htmlParam, onLinkPress, isSelectable}: RenderHTMLProp
return (
Parser.replace(htmlParam, {shouldEscapeText: false, filterRules: ['emoji']})
// Escape brackets when pasting a link, since unescaped [] can break Markdown link syntax
.replaceAll('&amp;#91;', '[')
.replaceAll('&amp;#93;', ']')
.replaceAll(RE_BRACKET_ESCAPE, (m) => (m.at(7) === '1' ? '[' : ']'))
// Remove double <emoji> tag if exists and keep the outermost tag (always the original tag).
.replaceAll(/(<emoji[^>]*>)(?:<emoji[^>]*>)+/g, '$1')
.replaceAll(/(<\/emoji[^>]*>)(?:<\/emoji[^>]*>)+/g, '$1')
.replaceAll(RE_EMOJI_OPEN_OR_CLOSE, '$1$2')
// Strip orphaned <br/> tags inside <ul> that would render as extra empty bullets
.replaceAll(/<br\s*\/?>\s*(<\/ul>)/gi, '$1')
.replaceAll(/(<\/li>)\s*<br\s*\/?>\s*(?=<(?:li|\/ul)>)/gi, '$1')
.replaceAll(RE_BR_CLEANUP, '$1$2')

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve cleanup for consecutive list breaks

When a list contains consecutive orphaned breaks before the closing </ul> (for example from pasted/blank-line list HTML like </li><br><br></ul>), the merged single-pass regex only removes the last <br> and leaves </li><br></ul>. The previous two-pass logic first removed the <br> before </ul> and then removed the newly exposed <br> after </li>, so this reintroduces the extra empty bullet the cleanup is meant to prevent.

Useful? React with 👍 / 👎.

);
}, [htmlParam]);

Expand Down
Loading