Skip to content

Commit

Permalink
Handle article:author meta tag. Fixes mozilla#938
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnixon committed Jan 1, 2025
1 parent 65578ae commit 5c39ec5
Show file tree
Hide file tree
Showing 4 changed files with 2,538 additions and 2 deletions.
25 changes: 23 additions & 2 deletions Readability.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,20 @@ Readability.prototype = {
}
},

/**
* Tests whether a string is a URL or not.
*
* @param {string} str The string to test
* @return {boolean} true if str is a URL, false if not
*/
_isUrl(str) {
try {
new URL(str);
return true;
} catch {
return false;
}
},
/**
* Converts each <a> and <img> uri in the given element to an absolute URI,
* ignoring #ref URIs.
Expand Down Expand Up @@ -1726,7 +1740,7 @@ Readability.prototype = {

// property is a space-separated list of values
var propertyPattern =
/\s*(article|dc|dcterm|og|twitter)\s*:\s*(author|creator|description|published_time|title|site_name)\s*/gi;
/\s*(article|dc|dcterm|og|twitter)\s*:\s*(author|article:author|creator|description|published_time|title|site_name)\s*/gi;

// name is a single value
var namePattern =
Expand Down Expand Up @@ -1780,13 +1794,20 @@ Readability.prototype = {
metadata.title = this._getArticleTitle();
}

const articleAuthor =
typeof values["article:author"] === "string" &&
!this._isUrl(values["article:author"])
? values["article:author"]
: undefined;

// get author
metadata.byline =
jsonld.byline ||
values["dc:creator"] ||
values["dcterm:creator"] ||
values.author ||
values["parsely-author"];
values["parsely-author"] ||
articleAuthor;

// get description
metadata.excerpt =
Expand Down
10 changes: 10 additions & 0 deletions test/test-pages/article-author-tag/expected-metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"title": "If You Can Picture A Tarot Card, It's Because of These 3 People",
"byline": "Laura June Topolsky",
"dir": null,
"lang": "en",
"excerpt": "Picture a deck of tarot cards. What do you see? Maybe the Magician in his rich red robes, right arm raised high above him. Or the skeleton on horseback for...",
"siteName": "Atlas Obscura",
"publishedTime": "2015-07-10 09:53:00 -0400",
"readerable": true
}
Loading

0 comments on commit 5c39ec5

Please sign in to comment.