Skip to content

Generate DOM for markdown links in help text#1979

Merged
brendankenny merged 7 commits intomasterfrom
markdownlinks
Apr 12, 2017
Merged

Generate DOM for markdown links in help text#1979
brendankenny merged 7 commits intomasterfrom
markdownlinks

Conversation

@ebidel
Copy link
Copy Markdown
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
Copy Markdown
Contributor

@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

}

/**
* @param {string} text
Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
Contributor

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>.

_convertMarkdownLinksToElement(text) {
const element = this._createElement('span');

// Split on markdown links (e.g. [some link](https://...)).
Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
Copy link
Copy Markdown
Contributor

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
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
Contributor

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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

if (linkText && linkHref) {
const a = this._createElement('a');
a.rel = 'noopener';
a.target = '_blank';
Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
Contributor

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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

// Split on markdown links (e.g. [some link](https://...)).
const parts = text.split(/(\[(.*?)\]\((https?:\/\/.*?)\))/g);

while (parts.length) {
Copy link
Copy Markdown
Contributor

@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
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
Contributor Author

ebidel commented Apr 11, 2017

PTAL

try {
a.href = (new URL(linkHref)).href;
element.appendChild(a);
} catch (e) {
Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
Contributor Author

ebidel commented Apr 11, 2017

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

@patrickhulce
Copy link
Copy Markdown
Collaborator

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

Copy link
Copy Markdown
Contributor

@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