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 emoticon regex #36

Closed
wants to merge 3 commits into from
Closed
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
13 changes: 8 additions & 5 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { Plugin } from 'unified';
import type { Root, Nodes, Text } from 'mdast';

const RE_EMOJI = /:\+1:|:-1:|:[\w-]+:/g;
const RE_SHORT = /[$@|*'",;.=:\-)([\]\\/<>038BOopPsSdDxXzZ]{2,5}/g;
const RE_SHORT = /(^|\s)[@$|*'",;.=:\-)([\]\\/<>038BOopPsSdDxXzZ]{2,5}/g;
const RE_PUNCT = /(?:_|-(?!1))/g;

/**
Expand Down Expand Up @@ -67,14 +67,17 @@ const plugin: Plugin<[(RemarkEmojiOptions | null | undefined)?], Root> = options
function replaceEmoticon(match: string): string | false | Text {
// find emoji by shortcode - full match or with-out last char as it could be from text e.g. :-),
const iconFull = emoticon.find(e => e.emoticons.includes(match)); // full match
const iconPart = emoticon.find(e => e.emoticons.includes(match.slice(0, -1))); // second search pattern
const icon = iconFull || iconPart;
const iconPartStart = emoticon.find(e => e.emoticons.includes(match.slice(0, -1))); // second search pattern
const iconPartEnd = emoticon.find(e => e.emoticons.includes(match.slice(1)));
const iconPart = emoticon.find(e => e.emoticons.includes(match.slice(1, -1)));
const icon = iconFull || iconPartStart || iconPartEnd || iconPart;
if (!icon) {
return false;
}
const trimmedChar = !iconFull && iconPart ? match.slice(-1) : '';
const trimmedChar = !(iconFull || iconPartEnd) && (iconPart || iconPartStart) ? match.slice(-1) : '';
const startTrimmedChar = !(iconFull || iconPartStart) && (iconPart || iconPartEnd) ? match.slice(0, 1) : '';
const addPad = pad ? ' ' : '';
const replaced = icon.emoji + addPad + trimmedChar;
const replaced = startTrimmedChar + icon.emoji + addPad + trimmedChar;
if (accessible) {
return aria(replaced, icon.name + ' emoticon');
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "remark-emoji",
"version": "5.0.1",
"version": "5.0.2",
"type": "module",
"engines": {
"node": ">=18"
Expand Down
3 changes: 3 additions & 0 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ describe('remark-emoji', function () {

it('handles emoji shortcodes (emoticon)', async function () {
const tests: Record<string, string> = {
'with space :o, and comma': 'with space 😮, and comma\n',
'WARN:Danger': 'WARN:Danger\n',
'https://github.com': 'https://github.com\n',
':p': '😛\n',
':-)': '😃\n',
'With-in some text :-p, also with some :o spaces :-)!':
Expand Down