Skip to content
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

Merged
merged 7 commits into from
Apr 12, 2017
Merged

Conversation

ebidel
Copy link
Contributor

@ebidel ebidel commented Apr 7, 2017

R: all

Just support in help text / descriptions.

screen shot 2017-04-07 at 1 57 59 pm

Will add a test once #1963 lands.

@ebidel ebidel added the v2report label Apr 7, 2017
Copy link
Member

@brendankenny brendankenny left a 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}
Copy link
Member

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 :)

Copy link
Contributor Author

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.

Copy link
Member

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);
Copy link
Member

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?

Copy link
Contributor Author

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);
Copy link
Member

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

Copy link
Member

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

Copy link
Contributor Author

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', () => {
Copy link
Member

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

Copy link
Contributor Author

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', () => {
Copy link
Member

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

Copy link
Contributor Author

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;
Copy link
Member

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

Copy link
Contributor Author

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?

Copy link
Member

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

Copy link
Contributor Author

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.
Copy link
Member

@brendankenny brendankenny Apr 10, 2017

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?

Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated the comment.

@ebidel
Copy link
Contributor Author

ebidel commented Apr 11, 2017

PTAL

a.href = (new URL(linkHref)).href;
element.appendChild(a);
} catch (e) {
element.appendChild(this._document.createTextNode(linkText));
Copy link
Member

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

Copy link
Contributor Author

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.

Copy link
Member

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

Copy link
Contributor Author

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](🍣🍺).

@ebidel
Copy link
Contributor Author

ebidel commented Apr 11, 2017

This ready. Let's get it landed so other PRs can proceed.

@patrickhulce
Copy link
Collaborator

let's merge this so we can rebase #1987 and #1992 💥

Copy link
Member

@brendankenny brendankenny left a comment

Choose a reason for hiding this comment

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

Yeah, LGTM

@brendankenny brendankenny merged commit 87bd052 into master Apr 12, 2017
@brendankenny brendankenny deleted the markdownlinks branch April 12, 2017 19:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants