Skip to content

Commit

Permalink
Add link to opportunities (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
JuanMaRuiz authored Sep 3, 2020
1 parent 129568b commit f59a309
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 4 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ Default: `70`

Threshold score to pass the PageSpeed test. Useful for setting a performance budget.

##### links

Type: `boolean`<br>
Default: `false`

If passed adds links with more info about opportunities. Useful for checking documentation about opportunities.

### psi.output(url, [options])

Output the formatted report to the terminal.
Expand Down
1 change: 1 addition & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const cli = meow(`
--format Output format: cli|json|tap
--locale Locale results should be generated in
--threshold Threshold score to pass the PageSpeed test
--links Adds link with more info about opportunities
Example
$ psi https://addyosmani.com --strategy=mobile
Expand Down
23 changes: 19 additions & 4 deletions lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
const sortOn = require('sort-on');
const humanizeUrl = require('humanize-url');
const prettyMs = require('pretty-ms');
const terminalLink = require('terminal-link');
const {getThreshold, getReporter} = require('./../lib/options-handler');
const {getLink} = require('./../lib/string-utils');

function overview(url, strategy, scores) {
return [
Expand Down Expand Up @@ -53,7 +55,19 @@ const labData = lighthouseResult => {
return sortOn(ret, 'label');
};

const opportunities = lighthouseResult => {
const getTitle = (title, description) => {
let output = title;
if (description) {
const link = getLink(description);
if (link) {
output = terminalLink(title, getLink(description));
}
}

return output;
};

const opportunities = (lighthouseResult, links) => {
const {audits, categories} = lighthouseResult;
const rules = getRules(categories.performance.auditRefs, 'load-opportunities');

Expand All @@ -63,9 +77,10 @@ const opportunities = lighthouseResult => {
});

const ret = opportunityRules.map(rule => {
const {title, details} = audits[rule.id];
const {title, details, description} = audits[rule.id];

return {
label: title,
label: links ? getTitle(title, description) : title,
value: prettyMs(details.overallSavingsMs)
};
});
Expand All @@ -85,7 +100,7 @@ module.exports = (parameters, response) => {
overview(humanizeUrl(id), parameters.strategy, lighthouseResult),
fieldData(loadingExperience.metrics),
labData(lighthouseResult),
opportunities(lighthouseResult),
opportunities(lighthouseResult, parameters.links),
threshold
));

Expand Down
9 changes: 9 additions & 0 deletions lib/string-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const getLink = str => {
const containsLink = str.match(/\(https?:(.*)\)/);

return containsLink ? containsLink[0].replace('(', '').replace(')', '') : '';
};

module.exports = {
getLink
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"prepend-http": "^3.0.1",
"pretty-ms": "^6.0.1",
"sort-on": "^4.1.0",
"terminal-link": "^2.1.1",
"update-notifier": "^4.1.0"
},
"devDependencies": {
Expand Down
18 changes: 18 additions & 0 deletions test/string-utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* eslint-env mocha */
const {expect} = require('chai');
const {getLink} = require('../lib/string-utils');

describe('stringUtils module', () => {
describe('getLink method', () => {
it('should return the url from a string', () => {
const str = `Consider lazy - loading offscreen and hidden images after all critical resources
have finished loading to lower time to interactive. [Learn more](https://web.dev/offscreen-images).`;
expect(getLink(str)).to.eql('https://web.dev/offscreen-images');
});
it('should return an empty string when it does not contain an url', () => {
const str = `Consider lazy - loading offscreen and hidden images after all critical resources
have finished loading to lower time to interactive.`;
expect(getLink(str)).to.eql('');
});
});
});

0 comments on commit f59a309

Please sign in to comment.