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

_getJSONLD doesn't handle context objects #934

Closed
danielnixon opened this issue Dec 27, 2024 · 2 comments · Fixed by #940
Closed

_getJSONLD doesn't handle context objects #934

danielnixon opened this issue Dec 27, 2024 · 2 comments · Fixed by #940
Labels
metadata Issues with the metadata generated by readability

Comments

@danielnixon
Copy link
Contributor

For example this json-ld from https://www.nbcnews.com/news/world/south-korea-martial-law-president-yoon-suk-yeol-impeachment-vote-rcna183138 fails with Reader: (Readability) [email protected] is not a function:

{
  '@context': {
    '@vocab': 'http://schema.org',
    articleId: { '@id': 'Text', '@type': '@id' },
    topics: { '@id': 'Text', '@type': '@id' }
  },
  '@type': 'NewsArticle',
  headline: 'South Korean president’s impeachment fails after ruling party lawmakers walk out'
  ...
}

The problematic code is this, which assumes parsed["@context"] will always be a string, never an object:

          if (
            !parsed["@context"] ||
            !parsed["@context"].match(/^https?\:\/\/schema\.org$/)
          ) {
            return;
          }

The quick fix to at least avoid the error would be:

          if (
            !parsed["@context"] ||
+           !(typeof parsed["@context"] === "string") ||
            !parsed["@context"].match(/^https?\:\/\/schema\.org$/)
          ) {
            return;
          }

To support objects in addition to strings:

           var content = jsonLdElement.textContent.replace(/^\s*<!\[CDATA\[|\]\]>\s*$/g, "");
           var parsed = JSON.parse(content);
-          if (
-            !parsed["@context"] ||
-            !parsed["@context"].match(/^https?\:\/\/schema\.org$/)
-          ) {
+
+          const schemaDotOrgRegex = /^https?\:\/\/schema\.org$/;
+          const matches =
+            (typeof parsed["@context"] === "string" && parsed["@context"].match(schemaDotOrgRegex)) ||
+            (typeof parsed["@context"] === "object" && typeof parsed["@context"]["@vocab"] == "string" && parsed["@context"]["@vocab"].match(schemaDotOrgRegex))
+
+          if (!matches) {
             return;
           }
@gijsk gijsk added the metadata Issues with the metadata generated by readability label Dec 31, 2024
@gijsk
Copy link
Contributor

gijsk commented Dec 31, 2024

Here too, I'd love to see this in the form of a PR? :-)

danielnixon added a commit to danielnixon/readability that referenced this issue Jan 1, 2025
@danielnixon
Copy link
Contributor Author

Thanks @gijsk, here's a PR: #940

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
metadata Issues with the metadata generated by readability
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants