-
Notifications
You must be signed in to change notification settings - Fork 48
Move structured data testing into HTML validate plugin framework #155
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
Changes from all commits
c37996a
14ee08a
2ae8c43
3c0840d
bb8ee88
2a829a5
45c0d8d
6b5765a
4a1d502
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,176 @@ | ||||||
| import { Rule } from "html-validate"; | ||||||
| import { execSync } from "child_process"; | ||||||
| import fs from "fs"; | ||||||
| import path from "path"; | ||||||
|
|
||||||
| export default class StructuredDataRule extends Rule { | ||||||
| documentation() { | ||||||
| return { | ||||||
| description: "Validate JSON-LD structured data using structured-data-testing-tool", | ||||||
| url: "https://github.com/fulldecent/github-pages-template/#structured-data", | ||||||
| }; | ||||||
| } | ||||||
|
|
||||||
| setup() { | ||||||
| this.on("tag:ready", this.tagReady.bind(this)); | ||||||
| } | ||||||
|
|
||||||
| tagReady({ target }) { | ||||||
| if (target.tagName === "script") { | ||||||
| const type = target.getAttribute("type")?.value; | ||||||
|
|
||||||
| // Only process script tags with type="application/ld+json" | ||||||
| if (type === "application/ld+json") { | ||||||
| this.validateJsonLd(target); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| validateJsonLd(scriptElement) { | ||||||
| // Try to read the file content directly and extract the script | ||||||
| if (scriptElement.location && scriptElement.location.filename) { | ||||||
| try { | ||||||
| const fileContent = fs.readFileSync(scriptElement.location.filename, "utf8"); | ||||||
| const lines = fileContent.split("\n"); | ||||||
|
|
||||||
| // Find script tag boundaries | ||||||
| let startLine = -1; | ||||||
| let endLine = -1; | ||||||
| let inScript = false; | ||||||
|
|
||||||
| for (let i = 0; i < lines.length; i++) { | ||||||
| const line = lines[i]; | ||||||
| if (line.includes('<script type="application/ld+json">')) { | ||||||
| startLine = i + 1; // Start after the opening tag | ||||||
| inScript = true; | ||||||
| } else if (inScript && line.includes("</script>")) { | ||||||
| endLine = i; | ||||||
| break; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| if (startLine >= 0 && endLine >= 0) { | ||||||
| const scriptContent = lines.slice(startLine, endLine).join("\n").trim(); | ||||||
|
|
||||||
| if (scriptContent) { | ||||||
| this.testStructuredData(scriptContent, scriptElement); | ||||||
| return; | ||||||
| } | ||||||
| } | ||||||
| } catch (error) { | ||||||
| this.report({ | ||||||
| node: scriptElement, | ||||||
| message: `Error reading file for structured data validation: ${error.message}`, | ||||||
| }); | ||||||
| return; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| this.report({ | ||||||
| node: scriptElement, | ||||||
| message: "JSON-LD script tag is empty or cannot be read", | ||||||
| }); | ||||||
| } | ||||||
|
|
||||||
| testStructuredData(content, scriptElement) { | ||||||
| // Create a temporary HTML file with just this JSON-LD script | ||||||
| const tempDir = "/tmp"; | ||||||
|
||||||
| const tempFileName = `structured-data-${Date.now()}-${Math.random().toString(36).substr(2, 9)}.html`; | ||||||
|
||||||
| const tempFileName = `structured-data-${Date.now()}-${Math.random().toString(36).substr(2, 9)}.html`; | |
| const tempFileName = `structured-data-${Date.now()}-${Math.random().toString(36).substring(2, 11)}.html`; |
Copilot
AI
Sep 16, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tempFilePath variable is not properly escaped for shell execution, which could lead to command injection if the path contains special characters. Use proper argument escaping or pass arguments as an array to avoid shell injection vulnerabilities.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good, if this is the actual result