Skip to content

Commit

Permalink
Allow parsing magnet links (#503)
Browse files Browse the repository at this point in the history
Specically any optionalSlashSlash scheme followed by a question mark,
e.g., magnet:?data...
  • Loading branch information
nfrasser authored Dec 4, 2024
1 parent 9183db6 commit 606feca
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/linkifyjs/src/parser.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export function init({ groups }) {
tk.COMMA,
tk.DOT,
tk.EXCLAMATION,
tk.PERCENT,
tk.QUERY,
tk.QUOTE,
tk.SEMI,
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 8 additions & 1 deletion test/spec/linkifyjs.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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;
});
Expand Down

0 comments on commit 606feca

Please sign in to comment.