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

Extract author name from itemprop='name'. #943

Merged
merged 3 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion Readability.js
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,10 @@ Readability.prototype = {
* (and its kids) are going away, and we want the next node over.
*
* Calling this in a loop will traverse the DOM depth-first.
*
* @param {Element} node
* @param {boolean} ignoreSelfAndKids
* @return {Element}
*/
_getNextNode(node, ignoreSelfAndKids) {
// First check for kids if those aren't being ignored
Expand Down Expand Up @@ -1079,7 +1083,20 @@ Readability.prototype = {
!this._metadata.byline &&
this._isValidByline(node, matchString)
) {
this._articleByline = node.textContent.trim();
// Find child node matching [itemprop="name"] and use that if it exists for a more accurate author name byline
var endOfSearchMarkerNode = this._getNextNode(node, true);
var next = this._getNextNode(node);
var itemPropNameNode = null;
while (next && next != endOfSearchMarkerNode) {
var itemprop = next.getAttribute("itemprop");
if (itemprop && itemprop.includes("name")) {
itemPropNameNode = next;
break;
} else {
next = this._getNextNode(next);
}
}
this._articleByline = (itemPropNameNode ?? node).textContent.trim();
node = this._removeAndGetNext(node);
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion test/test-pages/001/expected-metadata.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"title": "Get your Frontend JavaScript Code Covered | Code",
"byline": "Nicolas Perriault",
"byline": "Nicolas Perriault",
"dir": null,
"lang": "en",
"excerpt": "Nicolas Perriault's homepage.",
Expand Down
2 changes: 1 addition & 1 deletion test/test-pages/ars-1/expected-metadata.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"title": "Just-released Minecraft exploit makes it easy to crash game servers",
"byline": "Dan Goodin - Apr 16, 2015 8:02 pm UTC",
"byline": "Dan Goodin",
"dir": null,
"lang": "en-us",
"excerpt": "Two-year-old bug exposes thousands of servers to crippling attack.",
Expand Down
2 changes: 1 addition & 1 deletion test/test-pages/msn/expected-metadata.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"title": "Nintendo's first iPhone game will launch in December for $10",
"byline": "Alex Perry\n \n 1 day ago",
"byline": "Alex Perry",
"dir": "ltr",
"lang": "en-US",
"excerpt": "Nintendo and Apple shocked the world earlier this year by announcing \"Super Mario Run,\" the legendary gaming company's first foray into mobile gaming. ",
Expand Down
2 changes: 1 addition & 1 deletion test/test-pages/nytimes-3/expected-metadata.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"title": "Manhole Fires and Burst Pipes: How Winter Wreaks Havoc on What’s Underneath N.Y.C.",
"byline": "By Corey Kilgannon",
"byline": "Corey Kilgannon",
"dir": null,
"lang": "en",
"excerpt": "New York’s aging below-street infrastructure is tough to maintain, and the corrosive rock salt and “freeze-thaw” cycles of winter make it even worse.",
Expand Down
2 changes: 1 addition & 1 deletion test/test-pages/nytimes-4/expected-metadata.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"title": "As Debt Rises, the Government Will Soon Spend More on Interest Than on the Military",
"byline": "By Nelson D. Schwartz",
"byline": "Nelson D. Schwartz",
"dir": null,
"lang": "en",
"excerpt": "Tax cuts, spending increases and higher interest rates could make it harder to respond to future recessions and deal with other needs.",
Expand Down