You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by zr...@apache.org on 2020/11/03 13:34:11 UTC

[camel-website] 02/02: chore: add rule to check JSON-LD structured data

This is an automated email from the ASF dual-hosted git repository.

zregvart pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-website.git

commit c2b5b1e287f433b2e261e65ced7073c6ebd3fc77
Author: Zoran Regvart <zr...@apache.org>
AuthorDate: Tue Nov 3 13:49:19 2020 +0100

    chore: add rule to check JSON-LD structured data
---
 .htmlvalidate.json |  3 ++-
 rules.js           | 40 +++++++++++++++++++++++++++++++++++++++-
 2 files changed, 41 insertions(+), 2 deletions(-)

diff --git a/.htmlvalidate.json b/.htmlvalidate.json
index 1c69ddf..64899fb 100644
--- a/.htmlvalidate.json
+++ b/.htmlvalidate.json
@@ -12,6 +12,7 @@
     "element-required-attributes": "off",
     "prefer-tbody": "off",
     "long-title": "off",
-    "camel/title": "error"
+    "camel/title": "error",
+    "camel/structured-data": "error"
   }
 }
diff --git a/rules.js b/rules.js
index 9271c3f..2bf21ec 100644
--- a/rules.js
+++ b/rules.js
@@ -49,9 +49,47 @@ class RelativeLinks extends Rule {
   }
 }
 
+class StructuredData extends Rule {
+  documentation(context) {
+    return {
+      description: "Validates JSON-LD according to schema.org"
+    };
+  }
+
+  setup() {
+    let start = -1;
+
+    this.on('tag:open', event => {
+      if (event.target.nodeName === 'script') {
+        start = event.target.location.offset;
+      }
+    });
+
+    this.on('tag:close', async event => {
+      const tag = event.previous;
+      if (event.target.nodeName === 'script' && tag.hasAttribute('type') && tag.getAttribute('type').value === 'application/ld+json') {
+        const startIdx = data.indexOf('>', start) + 1;
+        const endIdx = event.target.location.offset - 1; // omit the opening tag angled bracket
+        const content = data.substring(startIdx, endIdx);
+        try {
+          JSON.parse(content);
+        } catch (err) {
+          this.report(tag, `Unable to parse JSON-LD as JSON: ${err}`);
+        }
+      }
+    });
+  }
+}
+
+let data;
+
 module.exports = {
+  setup(source) {
+    data = source.data;
+  },
   rules: {
     "camel/title": HtmlTitle,
-    "camel/relative-links": RelativeLinks
+    "camel/relative-links": RelativeLinks,
+    "camel/structured-data": StructuredData
   }
 };