From 04d93202f55d9b08dc0ec236562e3c8495587749 Mon Sep 17 00:00:00 2001 From: Nick Frasser <1693461+nfrasser@users.noreply.github.com> Date: Wed, 4 Dec 2024 01:06:38 -0500 Subject: [PATCH] Allow parsing magnet links Specically any optionalSlashSlash scheme followed by a question mark, e.g., magnet:?data... --- packages/linkifyjs/src/parser.mjs | 2 ++ test/spec/linkifyjs.test.mjs | 9 ++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/linkifyjs/src/parser.mjs b/packages/linkifyjs/src/parser.mjs index 5bbded5..36dbf61 100644 --- a/packages/linkifyjs/src/parser.mjs +++ b/packages/linkifyjs/src/parser.mjs @@ -54,6 +54,7 @@ export function init({ groups }) { tk.COMMA, tk.DOT, tk.EXCLAMATION, + tk.PERCENT, tk.QUERY, tk.QUOTE, tk.SEMI, @@ -211,6 +212,7 @@ export function init({ groups }) { // Force URL with scheme prefix followed by anything sane ta(SchemeColon, groups.domain, Url); tt(SchemeColon, tk.SLASH, Url); + tt(SchemeColon, tk.QUERY, Url); ta(UriPrefix, groups.domain, Url); ta(UriPrefix, qsAccepting, Url); tt(UriPrefix, tk.SLASH, Url); diff --git a/test/spec/linkifyjs.test.mjs b/test/spec/linkifyjs.test.mjs index ffad928..913d2d1 100644 --- a/test/spec/linkifyjs.test.mjs +++ b/test/spec/linkifyjs.test.mjs @@ -37,8 +37,9 @@ describe('linkifyjs', () => { describe('registerCustomProtocol', () => { beforeEach(() => { - linkify.registerCustomProtocol('instagram', true); linkify.registerCustomProtocol('view-source'); + linkify.registerCustomProtocol('instagram', true); + linkify.registerCustomProtocol('magnet', true); }); it('Detects basic protocol', () => { @@ -49,6 +50,12 @@ describe('linkifyjs', () => { expect(linkify.test('instagram://user/nfrasser', 'url')).to.be.ok; }); + it('Detects magnet protocol', () => { + const magnetLink = + 'magnet:?xt=urn:btih:5a7f5e0f3ce439e2f1a83e718a8405ec8809110b&dn=ernfkjenrkfk%5FSQ80%5FV%5Fv1.0.0%5Ferfnkerkf%5Ferfnkerkfefrfvegrteggt.net.rar'; + expect(linkify.test(magnetLink, 'url')).to.be.ok; + }); + it('Detects compound protocol', () => { expect(linkify.test('view-source://http://github.com/', 'url')).to.be.ok; });