-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generate DOM for markdown links in help text #1979
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cooool. nice work
@@ -181,6 +182,36 @@ class ReportRenderer { | |||
} | |||
|
|||
/** | |||
* @param {string} text | |||
* @return {!Element} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@return {!HTMLSpanElement}
. A function description here would be good too :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the type of element important We're not specifying subclass type for the other methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the type of element important We're not specifying subclass type for the other methods.
not really, though it doesn't hurt. I'm more thinking for documentation purposes it's nice to have the information that your linkified text is coming back in a <span>
.
const element = this._createElement('span'); | ||
|
||
// Split on markdown links (e.g. [some link](https://...)). | ||
const parts = text.split(/(\[(.*?)\]\((https?:\/\/.*?)\))/g); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any reason to capture fullMarkdownLink
if it's going to be unused?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nope done.
while (parts.length) { | ||
// Remove the same number of elements as there are capture groups. | ||
// eslint-disable-next-line no-unused-vars | ||
const [preambleText, fullMarkdownLink, linkText, linkHref] = parts.splice(0, 4); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you want to keep the larger capture group, you can also rename fullMarkdownLink
to _
to indicate that it's unused and drop the // eslint-disable-next-line no-unused-vars
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ooh, can also do const [preambleText, , linkText, linkHref]
, though don't know how our eslint would react
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i just removed the extra group. no longer needed.
const renderer = new ReportRenderer(document); | ||
|
||
describe('createElement', () => { | ||
it('creates a simple element using default values', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's new and what's not is very confusing when you move things around :P
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i like to keep you on your toes.
}); | ||
|
||
describe('_convertMarkdownLinksToElement', () => { | ||
it('converts links', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few more corner cases would be nice. With/without preamble text and text afterwards, etc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
const a = this._createElement('a'); | ||
a.rel = 'noopener'; | ||
a.target = '_blank'; | ||
a.href = linkHref; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we want to verify the url? Maybe someday it would be good if we start using this in more places, but today it would start checking our help text URLs, which would be nice
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what do you mean by verify?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what do you mean by verify?
sorry, I meant like check that it's a valid url. a.href = new URL(linkHref).href
or whatever
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
const parts = text.split(/(\[(.*?)\]\((https?:\/\/.*?)\))/g); | ||
|
||
while (parts.length) { | ||
// Remove the same number of elements as there are capture groups. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I always forget that splice
both creates a new array and modifies the original, so maybe change the comment to something like // Pop off the same number of elements as there are capture groups.
to make it clear?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you could also avoid the splice
by doing something like
let parts = text.split(/(\[(.*?)\]\((https?:\/\/.*?)\))/g);
while (parts.length) {
const [preambleText, fullMarkdownLink, linkText, linkHref, ...rest] = parts;
// ...
parts = rest;
}
more explicit, though the extra assignment is kind of annoying
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated the comment.
PTAL |
a.href = (new URL(linkHref)).href; | ||
element.appendChild(a); | ||
} catch (e) { | ||
element.appendChild(this._document.createTextNode(linkText)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we fail silently here? Seems like a big enough mistake that it should complain loudly and end up in _renderException
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's better to fail silently here. If a link is busted for some reason, it shouldn't block the whole report from being generated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but why is the link busted? Right now, at least, they're all coming from helpText. An error in one of those should be fixed immediately
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
FWIW, b/c of the regex, the only time we'd get an invalid URL is if someone wrote https://
. or used something crazy in help text like [boom](🍣🍺)
.
This ready. Let's get it landed so other PRs can proceed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, LGTM
R: all
Just support in help text / descriptions.
Will add a test once #1963 lands.