diff --git a/core/components/__tests__/link.test.js b/core/components/__tests__/link.test.js index c9dfe5c64..68724e9ea 100644 --- a/core/components/__tests__/link.test.js +++ b/core/components/__tests__/link.test.js @@ -646,8 +646,10 @@ describe('queryLinkAttrs', () => { describe('auto link regexp', () => { test.each([ ['http://foo.com', true], + ['http:///foo.com', false], ['foo.com', true], ['1http://foo.com', false], + ['abc def.com', true], ])('%# auto link regexp', async (input, expected) => { const match = URL_REGEX.exec(input + ' '); expect({ @@ -668,7 +670,17 @@ describe('Input rule', () => { , - hello [] + hello.com [] + + , + ], + [ + + hello https://123.com[] + , + + + hello https://123.com [] , ], diff --git a/core/components/link.js b/core/components/link.js index 87e06f3f8..1b13e3435 100644 --- a/core/components/link.js +++ b/core/components/link.js @@ -116,17 +116,26 @@ function pluginsFactory() { // TODO: Get a full tld list from IANA, or make it an option. // scheme :: name :: tld export const URL_REGEX = - /^(?:(http|https|ftp):\/\/)?(?:[^\s.:].)+(?:com|net|io)\s$/; + /(^|\s)(((http|https|ftp):\/\/)?(?:[^\s.:\/]+\.)+(?:com|net|io))\s$/; function autoLinkInputRule(type) { return new InputRule(URL_REGEX, (state, match, start, end) => { if (!match[0]) { return null; } - debugger; - const text = match[0]; - console.log(text); - // tr.addMark(markStart, markEnd, markType.create({href: 'http://icanhazip.com'})); + const [_, leadingSpace, text, scheme] = match; + // If no scheme, use default scheme http:// + const href = scheme ? text : `http://${text}`; + const tr = state.tr; + tr.addMark( + // Ignore the leading space, if any + leadingSpace.length > 0 ? start + 1 : start, + end, + type.create({ href: href }), + ); + // Append the space after the link + tr.insertText(' ', end); + return tr; }); }