Skip to content

fix(rules): read @graph ItemList in content/article-toc (#339) - #609

Merged
nc9 merged 2 commits into
mainfrom
fix/article-toc-graph-339
Oct 9, 2026
Merged

nc9 merged 2 commits into
mainfrom
fix/article-toc-graph-339

Conversation

@nc9

@nc9 nc9 commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Closes #339

content/article-toc parsed each JSON-LD script with a bare JSON.parse and looked only at its top level, so an ItemList inside a Yoast / Rank Math @graph wrapper was never seen. It now uses flattenJsonLdNodes per script, as the schema rules do (#317). Unparseable JSON is still skipped.

Tests: packages/rules/tests/article-toc.test.ts. The graph case fails before the fix; the flat-vs-graph parity and unparseable-input cases pass both before and after. Repo-wide grep finds no remaining Array.isArray(data) ? data : [data] in packages/rules/src.

No rule doc page exists for this rule, so no docs change. Agent skill change needed: no.

@nc9
nc9 marked this pull request as ready for review October 9, 2026 12:15
@claude

claude Bot commented Oct 9, 2026

Copy link
Copy Markdown

Reviewed the diff and flattenJsonLdNodes in packages/utils. I found no blocking issues. The change is correct, and the helper is the right one to use here.

  • Unparseable JSON still returns [], so the old try/catch is no longer needed.

  • Nesting depth is capped, so a hostile page can't blow the stack.

  • The @squirrelscan/utils import matches how other rules import from it.

  • The tests cover the graph case, flat-vs-graph parity, a graph with no ItemList, and bad JSON.

  • (nit) article-toc.ts: the as ItemListSchema[] cast on the helper's Record<string, unknown>[] return is unchecked. This was already true before the change. item.url?.includes would throw if a page sets url to a non-string, for example "url": 5. A typeof === "string" guard would make the rule robust against untrusted JSON.

  • (nit) schema["@type"] === "ItemList" misses "@type": ["ItemList"]. This is also an existing limitation and could be a follow-up.

  • (nit) The new line with flattenJsonLdNodes(...) as ItemListSchema[] is over 80 columns, so run the formatter on it.

Overall this looks good to merge.

@nc9
nc9 force-pushed the fix/article-toc-graph-339 branch from 79bba99 to f2c3b1f Compare October 9, 2026 14:23
@claude

claude Bot commented Oct 9, 2026

Copy link
Copy Markdown

Reviewed the diff. I found no blocking issues.

  • The change swaps the hand-rolled JSON.parse in checkTocSchema for the shared flattenJsonLdNodes. That helper already handles @graph nesting, top-level arrays, unparseable input and a depth cap, so the stack-overflow risk on hostile nesting is covered.

  • Calling it per script keeps the behaviour of the old code, which also parsed each script on its own.

  • The new tests cover an @graph-nested ItemList, parity with the flat markup, a graph with no ItemList, and malformed JSON.

  • I did not run the tests or CI.

  • (nit) packages/rules/src/content/article-toc.ts:123 The as ItemListSchema[] cast is a little loose, since the nodes are Record<string, unknown>. It was equally loose before, so this is optional.

  • (nit) packages/rules/src/content/article-toc.ts:125 @type can be an array such as ["ItemList", ...] in real markup. The strict === "ItemList" check would miss it. That predates this PR, so it could be a follow-up.

  • (nit) packages/rules/src/content/article-toc.ts:7 Other rules (for example eeat/author-byline.ts) put the @squirrelscan/utils import alongside the other package imports. Check this one follows the same grouping.

🤖 Generated with Claude Code

@nc9
nc9 force-pushed the fix/article-toc-graph-339 branch from f2c3b1f to 776959a Compare October 9, 2026 15:42
@claude

claude Bot commented Oct 9, 2026

Copy link
Copy Markdown

Review of #609: the fix is correct and well scoped. flattenJsonLdNodes is the right helper and is depth-capped. The tests cover the graph, parity and unparseable cases. I found one regression.

  • (medium) packages/rules/src/content/article-toc.ts (the itemListElement.some(...) callback): removing the try/catch also removed the guard around the item.url?.includes("#") || item.item?.includes("#") calls. Those calls ran inside the old try, so a malformed entry was swallowed. Now it throws out of the rule. Page-controlled JSON-LD can trigger it with any of these:

    • item as an object, e.g. "item": {"@id": "..."}, which is legal schema.org.
    • a numeric url.
    • a null entry in itemListElement.

    All of these hit .includes is not a function or a null dereference. The fix is to check typeof x === "string" and guard null entries before calling .includes. Please add a test for an object-valued item.

  • (nit) The as ItemListSchema[] cast hides that the nodes are Record<string, unknown>. Narrowing at the use site would also fix the finding above.

@claude

claude Bot commented Oct 9, 2026

Copy link
Copy Markdown

Review of #609: the change is small and the fix is correct. It reuses the existing flattenJsonLdNodes helper, which is depth-capped and handles multi-block and top-level array input.

  • (medium) packages/rules/src/content/article-toc.ts:~125 The old try/catch is gone, and it covered more than JSON.parse. The itemListElement.some(item => item.url?.includes("#") || item.item?.includes("#")) call is now unguarded. JSON-LD comes from untrusted pages. A null entry, a string entry, or a non-string url/item (for example item as an object, which is common in BreadcrumbList-style markup) will throw a TypeError. null and strings fail at item.url, and objects fail at .includes. Before this PR that was swallowed. Now it can crash the rule. Please check typeof item === "object" && item and typeof x === "string" before calling .includes, or keep a try/catch around the loop. A test with itemListElement: [null, {item: {}}] would pin this.
  • (low) @type is compared with === "ItemList", so ["ItemList"] is missed. This was already the case before the PR, and it is optional to fix here.
  • (nit) The // ... (#339) comment can go. The CHANGELOG entry and the tests already carry that context.

Tests cover the @graph pass case, parity with flat markup, a graph with no ItemList, and unparseable JSON. The malformed-item case above is the one missing.

@nc9
nc9 merged commit cef40ed into main Oct 9, 2026
15 checks passed
@nc9
nc9 deleted the fix/article-toc-graph-339 branch October 9, 2026 16:05
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

Successfully merging this pull request may close these issues.

rules(content): article-toc does not read JSON-LD nested in @graph, so a Yoast-style ItemList reports as missing

1 participant