Skip to content

fix: skip emoji conversion inside code blocks#2373

Open
ffedex wants to merge 1 commit intonpmx-dev:mainfrom
ffedex:fix/emoji-in-code-blocks
Open

fix: skip emoji conversion inside code blocks#2373
ffedex wants to merge 1 commit intonpmx-dev:mainfrom
ffedex:fix/emoji-in-code-blocks

Conversation

@ffedex
Copy link
Copy Markdown

@ffedex ffedex commented Apr 3, 2026

🔗 Linked issue

Fixes #2364

🧭 Context

Emoji shortcodes like :1234: were being converted even inside code blocks in package READMEs.

📚 Description

convertToEmoji was running against the full HTML string, so :1234: in IPv6 addresses inside <code> and <pre> tags got turned into 🔢. Updated the function to match code/pre blocks first and skip them, only converting shortcodes in the rest of the content. Removed the now-unused emojisKeysRegex variable.

@vercel
Copy link
Copy Markdown

vercel bot commented Apr 3, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
npmx.dev Ready Ready Preview, Comment Apr 3, 2026 8:05pm
2 Skipped Deployments
Project Deployment Actions Updated (UTC)
docs.npmx.dev Ignored Ignored Preview Apr 3, 2026 8:05pm
npmx-lunaria Ignored Ignored Apr 3, 2026 8:05pm

Request Review

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 3, 2026

Hello! Thank you for opening your first PR to npmx, @ffedex! 🚀

Here’s what will happen next:

  1. Our GitHub bots will run to check your changes.
    If they spot any issues you will see some error messages on this PR.
    Don’t hesitate to ask any questions if you’re not sure what these mean!

  2. In a few minutes, you’ll be able to see a preview of your changes on Vercel

  3. One or more of our maintainers will take a look and may ask you to make changes.
    We try to be responsive, but don’t worry if this takes a few days.

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 3, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ All tests successful. No failed tests found.

📢 Thoughts on this report? Let us know!

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 3, 2026

📝 Walkthrough

Walkthrough

The convertToEmoji function has been refactored to accept HTML markup instead of plain text. The function now employs a case-insensitive regex pattern to identify and process both code blocks (<code> and <pre> tags) and emoji shortcodes (:shortcode: format). When a match is found, code blocks are preserved unchanged, whilst shortcodes are replaced with their corresponding emoji values from the emojis mapping. The previous implementation using emojisKeysRegex construction and callback logic has been removed, reducing overall implementation complexity.

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description check ✅ Passed The pull request description clearly relates to the changeset, explaining the problem (emoji conversion in code blocks), the solution (matching and skipping code/pre blocks), and cleanup actions.
Linked Issues check ✅ Passed The changes directly address issue #2364 by preventing emoji shortcode conversion within code and pre blocks, which resolves the IPv6 address conversion issue described.
Out of Scope Changes check ✅ Passed All changes are scoped to the emoji conversion function and directly address the linked issue; no out-of-scope modifications are present.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: ba0b124c-67e4-47fa-853d-cdd5c293abfc

📥 Commits

Reviewing files that changed from the base of the PR and between 1d1f450 and ad42fb6.

📒 Files selected for processing (1)
  • shared/utils/emoji.ts

Comment on lines +1914 to +1915
const key = shortcode.slice(1, -1) as keyof typeof emojis
return emojis[key] || shortcode
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion | 🟠 Major

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Verify TypeScript strictness settings and locate assertion-based lookup in this utility.
fd -i 'tsconfig*.json' --exec sh -c 'echo "=== {} ==="; cat "{}"' \;
echo
rg -n --type=ts -C2 'as keyof typeof emojis|const emojis\s*:\s*Record<string,\s*string>|emojis\[key\]' shared/utils/emoji.ts

Repository: npmx-dev/npmx.dev

Length of output: 1366


🏁 Script executed:

# Check file size first
wc -l shared/utils/emoji.ts

# Read the area around the disputed lines and the emojis definition
head -n 100 shared/utils/emoji.ts | tail -n 50

# Also check the beginning to find the emojis object definition
head -n 50 shared/utils/emoji.ts

Repository: npmx-dev/npmx.dev

Length of output: 2543


🏁 Script executed:

# Read the area around the disputed lines
sed -n '1900,1920p' shared/utils/emoji.ts

# Also check the end of the file to see if there's an export
tail -n 30 shared/utils/emoji.ts

# Check for how many entries are in the emojis object
grep -n "^const emojis\|^}" shared/utils/emoji.ts | head -n 5

Repository: npmx-dev/npmx.dev

Length of output: 1512


Remove assertion-based key narrowing in emoji lookup to improve type safety.

Line 1914 uses as keyof typeof emojis, which unsafely asserts the key is valid. Instead, annotate the emojis object as Record<string, string> to allow string-indexed lookup without assertion. This approach is more permissive in the right way—string keys are valid on string-indexed maps—and aligns with strict TypeScript requirements.

Suggested refactor
-const emojis = {
+const emojis: Record<string, string> = {
   '100': '💯',
   '1234': '🔢',
   ...
 }
@@
-        const key = shortcode.slice(1, -1) as keyof typeof emojis
-        return emojis[key] || shortcode
+        const key = shortcode.slice(1, -1)
+        return emojis[key] ?? shortcode

const key = match.slice(1, -1) as keyof typeof emojis
return emojis[key] || match
})
export function convertToEmoji(html: string): string {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this previously took in text, but now html - is this definitely the case everywhere? and if not I guess it doesn't matter?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

:1234: is converted to emoji when found w/in an IPv6 address

2 participants