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

Suggestion: follow links in json-ld to find author name #932

Open
danielnixon opened this issue Dec 26, 2024 · 1 comment
Open

Suggestion: follow links in json-ld to find author name #932

danielnixon opened this issue Dec 26, 2024 · 1 comment

Comments

@danielnixon
Copy link
Contributor

Consider the json-ld from this article: https://harpers.org/archive/2024/12/the-painted-protest-dean-kissick-contemporary-art//

It looks something like this (truncated to just the bits relevant to the author name):

{
  "@context": "https://schema.org",
  "@graph": [
    {
      "@type": "Article",
      ...
      ...
      ...
      "author": [
        {
          "@id": "https://harpers.org/#/schema/person/69e72b14e23945c24e35bf38c40c0fd6"
        }
      ],
      ...
      ...
      ...
    },
    ...
    ...
    ...
    {
      "@type": "Person",
      "@id": "https://harpers.org/#/schema/person/69e72b14e23945c24e35bf38c40c0fd6",
      "name": "Dean Kissick",
      ...
      ...
      ...
    }
  ]
}

Readability will currently stop following the graph once it gets to the author element that doesn't have a name. If instead we followed the author ID to the person element, we'd find the author name there.

Because Readability doesn't see an author in the json-ld, it falls back on the rel="author" link present in the document body, which unfortunately contains "by Dean Kissick," with the extraneous text "by" and the trailing comma.

@danielnixon
Copy link
Contributor Author

Quick and dirty patch for argument's sake:

             return;
           }
 
-          if (!parsed["@type"] && Array.isArray(parsed["@graph"])) {
-            parsed = parsed["@graph"].find(function(it) {
+          var graphArray = parsed["@graph"];
+          if (!parsed["@type"] && Array.isArray(graphArray)) {
+            parsed = graphArray.find(function(it) {
               return (it["@type"] || "").match(
                 this.REGEXPS.jsonLdArticleTypes
               );
@@ -1436,6 +1437,15 @@ Readability.prototype = {
                   return author.name.trim();
                 })
                 .join(", ");
+            } else if (typeof parsed.author["@id"] === "string") {
+              var authorId = parsed.author["@id"];
+              var authorPerson = graphArray.find(function(it) {
+                return (it["@id"] || "").match(authorId);
+              });
+              if (authorPerson && typeof authorPerson.name === "string") {
+                metadata.byline = authorPerson.name.trim();
+              }
             }
           }
           if (typeof parsed.description === "string") {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant