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

Feat #903: Added support for emoji tags #1125

Merged
merged 2 commits into from
Jan 3, 2023
Merged
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
4 changes: 2 additions & 2 deletions packages/foam-vscode/src/core/utils/hashtags.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isSome } from './core';
const HASHTAG_REGEX = /(?<=^|\s)#([0-9]*[\p{L}/_-][\p{L}\p{N}/_-]*)/gmu;
const WORD_REGEX = /(?<=^|\s)([0-9]*[\p{L}/_-][\p{L}\p{N}/_-]*)/gmu;
const HASHTAG_REGEX = /(?<=^|\s)#([0-9]*[\p{L}\p{Emoji_Presentation}/_-][\p{L}\p{Emoji_Presentation}\p{N}/_-]*)/gmu;
const WORD_REGEX = /(?<=^|\s)([0-9]*[\p{L}\p{Emoji_Presentation}/_-][\p{L}\p{Emoji_Presentation}\p{N}/_-]*)/gmu;

export const extractHashtags = (
text: string
Expand Down
27 changes: 26 additions & 1 deletion packages/foam-vscode/src/core/utils/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,41 +7,48 @@ describe('hashtag extraction', () => {
it('returns empty list if no tags are present', () => {
expect(extractHashtags('hello world')).toEqual([]);
});

it('works with simple strings', () => {
expect(
extractHashtags('hello #world on #this planet').map(t => t.label)
).toEqual(['world', 'this']);
});

it('detects the offset of the tag', () => {
expect(extractHashtags('#hello')).toEqual([{ label: 'hello', offset: 0 }]);
expect(extractHashtags(' #hello')).toEqual([{ label: 'hello', offset: 1 }]);
expect(extractHashtags('to #hello')).toEqual([
{ label: 'hello', offset: 3 },
]);
});

it('works with tags at beginning or end of text', () => {
expect(
extractHashtags('#hello world on this #planet').map(t => t.label)
).toEqual(['hello', 'planet']);
});

it('supports _ and -', () => {
expect(
extractHashtags('#hello-world on #this_planet').map(t => t.label)
).toEqual(['hello-world', 'this_planet']);
});

it('supports nested tags', () => {
expect(
extractHashtags('#parent/child on #planet').map(t => t.label)
).toEqual(['parent/child', 'planet']);
});

it('ignores tags that only have numbers in text', () => {
expect(
extractHashtags('this #123 tag should be ignore, but not #123four').map(
t => t.label
)
).toEqual(['123four']);
});
it('supports unicode letters like Chinese charaters', () => {

it('supports unicode letters like Chinese characters', () => {
expect(
extractHashtags(`
this #tag_with_unicode_letters_汉字, pure Chinese tag like #纯中文标签 and
Expand All @@ -55,6 +62,24 @@ describe('hashtag extraction', () => {
]);
});

it('supports emoji tags', () => {
expect(
extractHashtags(`this is a pure emoji #⭐, #⭐⭐, #👍👍🏽👍🏿 some mixed emoji #π🥧, #✅todo
#urgent❗ or #❗❗urgent, and some nested emoji #📥/🟥 or #📥/🟢
`).map(t => t.label)
).toEqual([
'⭐',
'⭐⭐',
'👍👍🏽👍🏿',
'π🥧',
'✅todo',
'urgent❗',
'❗❗urgent',
'📥/🟥',
'📥/🟢',
]);
});

it('ignores hashes in plain text urls and links', () => {
expect(
extractHashtags(`
Expand Down