Skip to content

Commit 729dca3

Browse files
committed
fix linter issues
1 parent 740ddd3 commit 729dca3

File tree

7 files changed

+64
-16
lines changed

7 files changed

+64
-16
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
module.exports = {
55
"parserOptions": {
6-
"ecmaVersion": 6,
6+
"ecmaVersion": 2017,
77
},
88
"env": {
99
"es6": true,

Readability.js

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ Readability.prototype = {
495495
// could assume it's the full title.
496496
var headings = this._concatNodeLists(
497497
doc.getElementsByTagName("h1"),
498-
doc.getElementsByTagName("h2")
498+
doc.getElementsByTagName("h2"),
499499
);
500500
var trimmedTitle = curTitle.trim();
501501
var match = this._someNode(headings, function(heading) {
@@ -1401,7 +1401,7 @@ Readability.prototype = {
14011401
if (!parsed["@type"] && Array.isArray(parsed["@graph"])) {
14021402
parsed = parsed["@graph"].find(function(it) {
14031403
return (it["@type"] || "").match(
1404-
this.REGEXPS.jsonLdArticleTypes
1404+
this.REGEXPS.jsonLdArticleTypes,
14051405
);
14061406
});
14071407
}
@@ -1470,6 +1470,36 @@ Readability.prototype = {
14701470
return metadata ? metadata : {};
14711471
},
14721472

1473+
/**
1474+
* Swaps the "Surname, GivenName" formatted bylines to "GivenName Surname".
1475+
*
1476+
* @param {string|string[]} name
1477+
* @returns Name or names in "GivenName Surname" format
1478+
*/
1479+
_normalizeByline: function(name) {
1480+
var result = name;
1481+
1482+
if (Array.isArray(name)) {
1483+
return name.map((n) => this._normalizeByline(n));
1484+
}
1485+
1486+
// handle Surname, GivenName formatting
1487+
if (name.includes(",")) {
1488+
const parts = name.split(",").map(part => part.trim());
1489+
if (parts.length == 2) {
1490+
result = `${parts[1]} ${parts[0]}`;
1491+
}
1492+
if (parts.length > 2) {
1493+
result = `${parts[1]} ${parts[0]} ${parts.slice(2).join(" ")}`;
1494+
}
1495+
}
1496+
1497+
// remove things like "By:"
1498+
result = result.replace(/\w+:/, "");
1499+
1500+
return this._unescapeHtmlEntities(result);
1501+
},
1502+
14731503
/**
14741504
* Attempts to get excerpt and byline metadata for the article.
14751505
*
@@ -1499,6 +1529,7 @@ Readability.prototype = {
14991529
}
15001530
var matches = null;
15011531
var name = null;
1532+
var result = null;
15021533

15031534
if (elementProperty) {
15041535
matches = elementProperty.match(propertyPattern);
@@ -1507,7 +1538,7 @@ Readability.prototype = {
15071538
// so we can match below.
15081539
name = matches[0].toLowerCase().replace(/\s/g, "");
15091540
// multiple authors
1510-
values[name] = content.trim();
1541+
result = content.trim();
15111542
}
15121543
}
15131544
if (!matches && elementName && namePattern.test(elementName)) {
@@ -1516,8 +1547,23 @@ Readability.prototype = {
15161547
// Convert to lowercase, remove any whitespace, and convert dots
15171548
// to colons so we can match below.
15181549
name = name.toLowerCase().replace(/\s/g, "").replace(/\./g, ":");
1519-
values[name] = content.trim();
1550+
result = content.trim();
1551+
}
1552+
}
1553+
1554+
// handle properties which might have multiple distinct values, eg: citation_author
1555+
if (result) {
1556+
if (values[name]) {
1557+
if (Array.isArray(values[name]) && typeof result == "string") {
1558+
values[name].push(result);
1559+
}
1560+
if (typeof values[name] == "string") {
1561+
values[name] = [values[name], result];
1562+
}
1563+
} else {
1564+
values[name] = result;
15201565
}
1566+
this.log(`found metadata: ${name}=${values[name]}`);
15211567
}
15221568
});
15231569

@@ -1569,7 +1615,7 @@ Readability.prototype = {
15691615
// in many sites the meta value is escaped with HTML entities,
15701616
// so here we need to unescape it
15711617
metadata.title = this._unescapeHtmlEntities(metadata.title);
1572-
metadata.byline = this._unescapeHtmlEntities(metadata.byline);
1618+
metadata.byline = this._normalizeByline(metadata.byline);
15731619
metadata.excerpt = this._unescapeHtmlEntities(metadata.excerpt);
15741620
metadata.siteName = this._unescapeHtmlEntities(metadata.siteName);
15751621
metadata.publishedTime = this._unescapeHtmlEntities(metadata.publishedTime);

test/generate-testcase.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ function getWithRedirects(url, cb) {
6262
console.log("HEADERS:", JSON.stringify(response.headers));
6363
}
6464

65-
if(response.statusCode > 300 && response.statusCode <= 303) {
66-
if (debug) console.log("following redirect", response.headers.location);
65+
if (response.statusCode > 300 && response.statusCode <= 303) {
66+
if (debug)
67+
console.log("following redirect", response.headers.location);
6768
await getWithRedirects(response.headers.location, cb);
6869
}
6970

@@ -73,7 +74,8 @@ function getWithRedirects(url, cb) {
7374
response.on("data", (chunk) => rv += chunk);
7475

7576
response.on("end", () => {
76-
if (debug) console.log("End received");
77+
if (debug)
78+
console.log("End received");
7779
cb(rv);
7880
});
7981
});
@@ -192,4 +194,4 @@ if (process.argv[2] === "all") {
192194
});
193195
} else {
194196
generateTestcase(process.argv[2]);
195-
}
197+
}

test/test-pages/nature/expected-metadata.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
{
22
"title": "Worldwide divergence of values",
3-
"byline": "Medvedev, Danila",
3+
"byline": [
4+
"Joshua Conrad Jackson",
5+
"Danila Medvedev"
6+
],
47
"dir": null,
58
"lang": "en",
69
"excerpt": "Social scientists have long debated the nature of cultural change in a modernizing and globalizing world. Some scholars predicted that national cultures would converge by adopting social values typical of Western democracies. Others predicted that cultural differences in values would persist or even increase over time. We test these competing predictions by analyzing survey data from 1981 to 2022 (n = 406,185) from 76 national cultures. We find evidence of global value divergence. Values emphasizing tolerance and self-expression have diverged most sharply, especially between high-income Western countries and the rest of the world. We also find that countries with similar per-capita GDP levels have held similar values over the last 40 years. Over time, however, geographic proximity has emerged as an increasingly strong correlate of value similarity, indicating that values have diverged globally but converged regionally. The authors test whether social values have become converged or diverged across national cultures over the last 40 years using a 76-country analysis of the World Values Survey. They show that values have diverged, especially between high-income Western countries and the rest of the world.",

test/test-pages/nature/expected.html

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -651,9 +651,6 @@ <h2 id="additional-information"> Additional information </h2>
651651
<b>Publisher’s note</b> Springer Nature remains neutral with regard to jurisdictional claims in published maps and institutional affiliations.
652652
</p>
653653
</div>
654-
<div id="Sec25-section" data-title="Supplementary information">
655-
<h2 id="Sec25"> Supplementary information </h2>
656-
</div>
657654
<div id="rightslink-section" data-title="Rights and permissions">
658655
<h2 id="rightslink"> Rights and permissions </h2>
659656
<div id="rightslink-content">

test/test-pages/ourworldindata/expected-metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"title": "Why do we need to know about progress if we are concerned about the world's largest problems?",
3-
"byline": "By: Max Roser",
3+
"byline": "Max Roser",
44
"dir": null,
55
"excerpt": "Why have we made it our mission to publish “research and data to make progress against the world’s largest problems”?",
66
"siteName": "Our World in Data",

test/test-readability.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ describe("Readability API", function() {
289289
it("should use custom video regex sent as option", function() {
290290
var dom = new JSDOM(
291291
"<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc mollis leo lacus, vitae semper nisl ullamcorper ut.</p>" +
292-
"<iframe src=\"https://mycustomdomain.com/some-embeds\"></iframe>"
292+
"<iframe src=\"https://mycustomdomain.com/some-embeds\"></iframe>",
293293
);
294294
var expected_xhtml = "<div id=\"readability-page-1\" class=\"page\">" +
295295
"<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc mollis leo lacus, vitae semper nisl ullamcorper ut.</p>" +

0 commit comments

Comments
 (0)