Skip to content

Conversation

@fregante
Copy link
Member

@fregante fregante commented Jun 5, 2017

First commit was to fix #463: If a line contained the same URL twice, it would replace the first URL twice instead of touching the second one.

Then I extended the changes to reorganize the code and test more of it.

Closes #463
Closes #478

@fregante fregante closed this Jun 5, 2017
@sindresorhus sindresorhus reopened this Jun 5, 2017
@fregante fregante changed the title Correctly linkify repeating URLs on the same line Correctly linkify repeating URLs on the same line (WIP) Jun 6, 2017
@fregante
Copy link
Member Author

fregante commented Jun 6, 2017

@fregante fregante changed the title Correctly linkify repeating URLs on the same line (WIP) Correctly linkify repeating URLs on the same line Jun 6, 2017
@fregante fregante changed the title Correctly linkify repeating URLs on the same line Rework linkify-urls-in-code Jun 6, 2017
This was referenced Jun 6, 2017
@sindresorhus
Copy link
Member

@fregante fregante force-pushed the linkify-fix branch 2 times, most recently from d2ee07b to 7e7825d Compare June 7, 2017 17:20
@fregante
Copy link
Member Author

fregante commented Jun 7, 2017

@sindresorhus damn you, you could have done that 48 hours ago :D

@fregante fregante force-pushed the linkify-fix branch 2 times, most recently from 743867c to 835700a Compare June 7, 2017 17:43
@fregante
Copy link
Member Author

fregante commented Jun 7, 2017

I think linkify-urls has issues here: https://github.com/sindresorhus/caprine/pull/220/files

It only detects a single URL:

https://travis-ci.org/sindresorhus/caprine.svg?branch=master)](https://travis-ci.org/sindresorhus/caprine

url-regex may be too lax: kevva/url-regex#35

@sindresorhus
Copy link
Member

damn you, you could have done that 48 hours ago :D

I was actually going to do a PR adding those after your "build script" PR, but you're just too fast. (Not complaining 😛)

@fregante
Copy link
Member Author

What do we do about url-regex?

@sindresorhus
Copy link
Member

@bfred-it I've worked around it for now: sindresorhus/linkify-urls@aca71b8

@fregante
Copy link
Member Author

fregante commented Jun 13, 2017

Great! I'll fix this today. I also found another issue that has to do with applying this to HTML rather than its text nodes. e.g. http://a.com<span is wholly matched as a URL. I'll use an DOM tree walker and apply it only on textContent

fregante added 2 commits June 13, 2017 13:46
Before:
* Its code was spread between utils, content and its own file
* Matching was run three times (.test, .match, .replace)
* The linkifier was only half-DRYed

Now:
* The replacer can run on new elements, it won’t stop if it finds any
previously-linkified elements
* The replacer is tested on elements
@fregante
Copy link
Member Author

fregante commented Jun 13, 2017

It was appearing here but linkify-urls@1.0.2 fixed the issue.

screen shot 2017-06-13 at 15 27 23

@jgierer12 jgierer12 mentioned this pull request Jun 13, 2017
// Shortcut for html`text` instead of html(`text`)
if (html.raw) {
html = String.raw(...arguments);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like using tagged templates just for the sake of it. In this case it gives no added benefit over a normal function call, right?

};
walker[Symbol.iterator] = () => ({next});
return walker;
};
Copy link
Member

@sindresorhus sindresorhus Jun 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a useful npm package. Hint hint ;) Shouldn't block merging this PR though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about t but I'm not good at testing browser packages 😅

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it's annoying, but easier if JSDom supports the tree-walker API. See: https://github.com/sindresorhus/is-blob/blob/927899acea40ce7bd31e2b3c4352248eba3a0609/test.js

}

// Mark code block as touched
untouchedCode.forEach(el => el.classList.add(linkifiedURLClass));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a for-of loop for consistency and performance.

src/libs/util.js Outdated
// Export as functions because of the g flag
// https://stackoverflow.com/questions/1520800/why-regexp-with-global-flag-in-javascript-give-wrong-results
export const getURLRegex = () => /(http(s)?(:\/\/))(www\.)?[a-zA-Z0-9-_.]+(\.[a-zA-Z0-9]{2,})([-a-zA-Z0-9:%_+.~#?&//=]*)/g;
export const getIssueRegex = () => /([a-zA-Z0-9-_.]+\/[a-zA-Z0-9-_.]+)?#[0-9]+/g;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't these be removed now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose so. The rebase conflicts were confusing

return;
}
for (const textNode of getTextNodes(el)) {
if (textNode.textContent.length < 11) { // Shortest url: http://j.mp
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clever 🙌

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Realistically we can even increase the number to skip already-short URLs like that.

@fregante
Copy link
Member Author

fregante commented Jun 14, 2017

Good to go?

getURLRegex has been removed in 8253c7d

@sindresorhus sindresorhus merged commit baf5019 into master Jun 14, 2017
@sindresorhus sindresorhus deleted the linkify-fix branch June 14, 2017 10:59
@sindresorhus
Copy link
Member

Yup. It's perfect now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Weird escaping in issue titles with single-quote URL linkification fails inside a Travis badge

2 participants