Skip to content

Commit 606feca

Browse files
authored
Allow parsing magnet links (#503)
Specically any optionalSlashSlash scheme followed by a question mark, e.g., magnet:?data...
1 parent 9183db6 commit 606feca

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

packages/linkifyjs/src/parser.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export function init({ groups }) {
5454
tk.COMMA,
5555
tk.DOT,
5656
tk.EXCLAMATION,
57+
tk.PERCENT,
5758
tk.QUERY,
5859
tk.QUOTE,
5960
tk.SEMI,
@@ -211,6 +212,7 @@ export function init({ groups }) {
211212
// Force URL with scheme prefix followed by anything sane
212213
ta(SchemeColon, groups.domain, Url);
213214
tt(SchemeColon, tk.SLASH, Url);
215+
tt(SchemeColon, tk.QUERY, Url);
214216
ta(UriPrefix, groups.domain, Url);
215217
ta(UriPrefix, qsAccepting, Url);
216218
tt(UriPrefix, tk.SLASH, Url);

test/spec/linkifyjs.test.mjs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ describe('linkifyjs', () => {
3737

3838
describe('registerCustomProtocol', () => {
3939
beforeEach(() => {
40-
linkify.registerCustomProtocol('instagram', true);
4140
linkify.registerCustomProtocol('view-source');
41+
linkify.registerCustomProtocol('instagram', true);
42+
linkify.registerCustomProtocol('magnet', true);
4243
});
4344

4445
it('Detects basic protocol', () => {
@@ -49,6 +50,12 @@ describe('linkifyjs', () => {
4950
expect(linkify.test('instagram://user/nfrasser', 'url')).to.be.ok;
5051
});
5152

53+
it('Detects magnet protocol', () => {
54+
const magnetLink =
55+
'magnet:?xt=urn:btih:5a7f5e0f3ce439e2f1a83e718a8405ec8809110b&dn=ernfkjenrkfk%5FSQ80%5FV%5Fv1.0.0%5Ferfnkerkf%5Ferfnkerkfefrfvegrteggt.net.rar';
56+
expect(linkify.test(magnetLink, 'url')).to.be.ok;
57+
});
58+
5259
it('Detects compound protocol', () => {
5360
expect(linkify.test('view-source://http://github.com/', 'url')).to.be.ok;
5461
});

0 commit comments

Comments
 (0)