Skip to content

Commit

Permalink
Fix hashtag plugin: add support for full width middle dot (#492)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuiseki authored Oct 25, 2024
1 parent cc5c5fa commit e4b161a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/linkify-plugin-hashtag/src/hashtag.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ const HashtagToken = createTokenClass('hashtag', { isLink: true });
/**
* @type {import('linkifyjs').Plugin}
*/
export default function hashtag({ scanner, parser }) {
export default function hashtag({ scanner, parser }) {
// Various tokens that may compose a hashtag
const { POUND, UNDERSCORE } = scanner.tokens;
const { POUND, UNDERSCORE, FULLWIDTHMIDDLEDOT } = scanner.tokens;
const { alpha, numeric, alphanumeric, emoji } = scanner.tokens.groups;

// Take or create a transition from start to the '#' sign (non-accepting)
Expand All @@ -21,11 +21,14 @@ const HashtagToken = createTokenClass('hashtag', { isLink: true });
Hash.ta(numeric, HashPrefix);
Hash.ta(alpha, Hashtag);
Hash.ta(emoji, Hashtag);
Hash.ta(FULLWIDTHMIDDLEDOT, Hashtag);
HashPrefix.ta(alpha, Hashtag);
HashPrefix.ta(emoji, Hashtag);
HashPrefix.ta(FULLWIDTHMIDDLEDOT, Hashtag);
HashPrefix.ta(numeric, HashPrefix);
HashPrefix.tt(UNDERSCORE, HashPrefix);
Hashtag.ta(alphanumeric, Hashtag);
Hashtag.ta(emoji, Hashtag);
Hashtag.tt(FULLWIDTHMIDDLEDOT, Hashtag);
Hashtag.tt(UNDERSCORE, Hashtag); // Trailing underscore is okay
}
1 change: 1 addition & 0 deletions packages/linkifyjs/src/scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export function init(customSchemes = []) {
tt(Start, '~', tk.TILDE);
tt(Start, '_', tk.UNDERSCORE);
tt(Start, '\\', tk.BACKSLASH);
tt(Start, '・', tk.FULLWIDTHMIDDLEDOT);

const Num = tr(Start, re.DIGIT, tk.NUM, { [fsm.numeric]: true });
tr(Num, re.DIGIT, Num);
Expand Down
1 change: 1 addition & 0 deletions packages/linkifyjs/src/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export const PLUS = 'PLUS'; // +
export const POUND = 'POUND'; // #
export const QUERY = 'QUERY'; // ?
export const QUOTE = 'QUOTE'; // "
export const FULLWIDTHMIDDLEDOT = 'FULLWIDTHMIDDLEDOT'; // ・

export const SEMI = 'SEMI'; // ;
export const SLASH = 'SLASH'; // /
Expand Down
8 changes: 8 additions & 0 deletions test/spec/linkify-plugin-hashtag.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ describe('linkify-plugin-hashtag', () => {
expect(linkify.test('#سلام', 'hashtag')).to.be.ok;
});

it('Works with Japanese characters', () => {
expect(linkify.test('#おはよう', 'hashtag')).to.be.ok;
});

it('Works with Japanese characters and full width middle dot', () => {
expect(linkify.test('#おは・よう', 'hashtag')).to.be.ok;
});

it('Works with emojis', () => {
expect(linkify.test('#🍭', 'hashtag')).to.be.ok;
});
Expand Down
6 changes: 6 additions & 0 deletions test/spec/linkifyjs/scanner.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const tests = [
['$', [t.DOLLAR], ['$']],
['=', [t.EQUALS], ['=']],
['-', [t.HYPHEN], ['-']],
['・', [t.FULLWIDTHMIDDLEDOT], ['・']],
['&?<>(', [t.AMPERSAND, t.QUERY, t.OPENANGLEBRACKET, t.CLOSEANGLEBRACKET, t.OPENPAREN], ['&', '?', '<', '>', '(']],
['([{}])', [t.OPENPAREN, t.OPENBRACKET, t.OPENBRACE, t.CLOSEBRACE, t.CLOSEBRACKET, t.CLOSEPAREN], ['(', '[', '{', '}', ']', ')']],
['!,;\'', [t.EXCLAMATION, t.COMMA, t.SEMI, t.APOSTROPHE], ['!', ',', ';', '\'']],
Expand Down Expand Up @@ -83,6 +84,11 @@ const tests = [
[t.POUND, t.UWORD, t.UNDERSCORE, t.UWORD, t.WS, t.POUND, t.UWORD, t.WS, t.POUND, t.UWORD],
['#', 'АБВ', '_', 'бв', ' ', '#', '한글', ' ', '#', 'سلام']
],
[
'#おは・よう',
[t.POUND, t.UWORD, t.FULLWIDTHMIDDLEDOT, t.UWORD],
['#', 'おは', '・', 'よう']
],
[
'テストexample.comテスト',
[t.UWORD, t.WORD, t.DOT, t.TLD, t.UWORD],
Expand Down

0 comments on commit e4b161a

Please sign in to comment.