Skip to content

Commit

Permalink
Generate DOM for markdown links in help text
Browse files Browse the repository at this point in the history
  • Loading branch information
ebidel committed Apr 7, 2017
1 parent 75d2768 commit c977a79
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions lighthouse-core/report/v2/report-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ window.ReportRenderer = class ReportRenderer {

const column = element.appendChild(this._createElement('div', 'lighthouse-score__text'));
column.appendChild(this._createElement('div', 'lighthouse-score__title')).textContent = title;
column.appendChild(
this._createElement('div', 'lighthouse-score__description')).textContent = description;
column.appendChild(this._createElement('div', 'lighthouse-score__description'))
.appendChild(this._convertMarkdownLinksToElement(description));

return element;
}
Expand All @@ -130,6 +130,36 @@ window.ReportRenderer = class ReportRenderer {
}
}

/**
* @param {string} text
* @return {!Element}
*/
_convertMarkdownLinksToElement(text) {
const element = this._createElement('span');

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

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);
element.appendChild(document.createTextNode(preambleText));

// Append link if there are any.
if (linkText && linkHref) {
const a = this._createElement('a');
a.rel = 'noopener';
a.target = '_blank';
a.href = linkHref;
a.textContent = linkText;
element.appendChild(a);
}
}

return element;
}

/**
* @param {!DetailsJSON} text
* @return {!Element}
Expand Down

0 comments on commit c977a79

Please sign in to comment.