Skip to content

Commit

Permalink
auto link
Browse files Browse the repository at this point in the history
  • Loading branch information
lucywang000 committed Jun 7, 2021
1 parent 098863f commit 9610b8c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
14 changes: 13 additions & 1 deletion core/components/__tests__/link.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -668,7 +670,17 @@ describe('Input rule', () => {
</doc>,
<doc>
<para>
<link href="http://hello.com">hello</link> []
<link href="http://hello.com">hello.com</link> []
</para>
</doc>,
],
[
<doc>
<para>hello https://123.com[]</para>
</doc>,
<doc>
<para>
hello <link href="https://123.com">https://123.com</link> []
</para>
</doc>,
],
Expand Down
19 changes: 14 additions & 5 deletions core/components/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
}

Expand Down

0 comments on commit 9610b8c

Please sign in to comment.