Skip to content

Server-render schema.org Dataset JSON-LD on dataset pages - #205

Merged
neuromechanist merged 3 commits into
mainfrom
feature/issue-156-dataset-jsonld
Jul 29, 2026
Merged

neuromechanist merged 3 commits into
mainfrom
feature/issue-156-dataset-jsonld

Conversation

@neuromechanist

Copy link
Copy Markdown
Contributor

Addresses #156.

Scope

The issue was written against the legacy nemar.org ("client-rendered single-page app"); that premise is stale on this redesigned, server-rendered Astro build. src/pages/dataset/[id].astro already does the SSR fetch fan-out (catalog row + metadata.json + landing payload) -- this PR only adds the JSON-LD emission from data the page already has.

In scope / done:

  • Every dataset page returns a valid schema.org Dataset JSON-LD in the server-rendered HTML (src/lib/jsonld.ts, wired through Base.astro's new jsonLdScript prop).
  • license, conditionsOfAccess, required citations, and the data.nemar.org download path are present and correct (see field notes below).
  • name, description, identifier (concept DOI), license/conditionsOfAccess, creator, citation, measurementTechnique, keywords, includedInDataCatalog, and a distribution (DataDownload) with contentUrl/contentSize/the nemar-cli access note.
  • Validates in the schema.org validator -- see below.

Explicitly out of scope (issue stays open for these):

  • Shipping dataset.jsonld / summary.json to data.nemar.org/<id>/latest/. That's backend work in nemar-cli/nemar-tools, not this repo -- AGENTS.md is explicit about not reimplementing the backend here.
  • A CI generation step. The page renders per request; there's nothing to pre-generate.
  • llms.txt, an AI-aware robots.txt, a site AGENTS.md for nemar.org -- called out as follow-ups in the issue itself.
  • The markdown mirror per dataset page.

What's new

  • src/lib/jsonld.ts -- pure builder (buildDatasetJsonLd) + safe serializer (datasetJsonLdScript). No network calls; takes exactly what the page already fetched.
  • src/lib/jsonld.test.ts + test/fixtures/jsonld-*.json -- 36 tests against three real captures (nm000103, on007753, nm000154) plus synthetic edge cases for missing-DOI and </script> escaping.
  • Base.astro grows an optional jsonLdScript prop (pre-escaped string in, set:html into a <script type="application/ld+json"> tag). Layout stays generic; all mapping logic lives in src/lib/jsonld.ts.
  • dataset/[id].astro builds the script from the catalog row, metadata, provenance, and resolved version it already has in scope, then passes it to <Base>.

Escaping (the actual security consideration)

escapeJsonLdForScript replaces <, >, &, U+2028, and U+2029 with \uXXXX JSON string escapes before the payload goes into set:html. Since \uXXXX is a valid JSON string escape, this round-trips through JSON.parse unchanged -- it only changes what the HTML parser sees, so a dataset name/description containing </script> cannot break out. Tested directly (escapeJsonLdForScript unit tests) and end-to-end (a hostile Evil</script><script>alert(1)</script>Dataset name embedded in a real <script>...</script> wrapper, asserting exactly one </script> substring survives -- the real closing tag).

Field mapping notes / discrepancies found

  • identifier (concept DOI): the OSCAR reference (dataset-nm000103.jsonld) uses the Zenodo DOI (10.5281/zenodo.17306881) as identifier. Real NEMAR data mints its own concept DOI via EZID at dataset creation (10.82901/nemar.<id>, exposed as catalogRow.concept_doi / metadata.external_links.dataset_doi) -- that's what the site already uses everywhere else (citations, ProvenanceToggle), so it's what we use as identifier here too. The Zenodo DOI is real (nm000103 does have one, via a related_identifiers entry with relation_type: "IsIdenticalTo") and is surfaced as sameAs instead.
  • isBasedOn: the OSCAR reference hardcodes "https://openneuro.org" for every dataset. Real data distinguishes NEMAR-native (nm*) from OpenNeuro-derived (on*) datasets, and for derived ones the site already resolves the specific source dataset id via detectProvenance() (reused, not duplicated). We emit https://openneuro.org/datasets/ds007753-style links, and omit isBasedOn entirely for native datasets.
  • citation: reuses datasetCitation() from cite.ts (per AGENTS.md, don't duplicate citation formatting) for the dataset's own APA self-citation, plus any DOIs the metadata marks relation_type: "References". Deliberately excludes "IsReferencedBy" DOIs -- on007753's related_identifiers are all IsReferencedBy (papers that cite the dataset, not papers to cite when using it); including them as citation would misrepresent the relationship. Covered by a dedicated test.
  • Two pre-existing type/reality mismatches found while capturing real fixtures (unrelated to this PR, not fixed here):
    • metadata.json's keywords ships as {term: string}[] objects, but neuroschema.ts declares keywords: string[]. Already known/handled defensively in DetailRail.astro (comment + runtime check) and mirrored the same way here.
    • Funding.funder in neuroschema.ts doesn't match the real API's funder_name field -- confirmed this actually breaks production today (empty Funding chips on nm000103's live page). Filed as Funding.funder type doesn't match API: empty funder chips on dataset pages #204; not touched in this PR (JSON-LD doesn't surface funding).

Verification

  • bun run test -- 976/976 green (36 new).
  • bun run typecheck -- 0 errors.
  • bun run lint -- clean.
  • bun run dev + plain curl (no JS) against /dataset/nm000103, /dataset/on007753, /dataset/nm000154 -- JSON-LD present in the raw server-rendered HTML in all three, parses cleanly, degrades correctly on the sparse case (no license, description, creator, keywords, or sameAs keys -- omitted, never null).
  • /dataset/ds007753 still 301s to /dataset/on007753 before reaching this code, unchanged -- confirms every legacy OpenNeuro pointer currently in the catalog already has an on* mirror (100% migrated), so there is no live ds*-only page to test against right now. Used nm000154 (real, captured 2026-07-29) as the closest real analogue of the sparse null-heavy contract AGENTS.md describes for ds*/unsynced on* rows.
  • Schema.org validator: ran the actual escaped server-rendered output (as shipped, <-escaped) through validator.schema.org's "Code snippet" tester for both nm000103 (fully populated) and nm000154 (sparse) -- both report 0 errors, 0 warnings, and every field decodes back to the correct unescaped value (verified the ampersand in the citation string and the <files> placeholder in the distribution description both round-trip correctly).

Datasets tested

  • nm000103 -- NEMAR-native, fully populated (8 authors with ORCID, 8 keywords, CC-BY-NC-SA, Data Descriptor citations, Zenodo sameAs).
  • on007753 -- OpenNeuro-derived, CC0, isBasedOn resolves to the specific ds007753 source.
  • nm000154 -- sparse real dataset: null description, null license, empty authors/keywords/related_identifiers. Covers "missing license" from real data; see the ds* note above for why this stands in for the sparse/no-mirror case.

Pure mapping from catalog row + metadata.json + landing payload to a
schema.org Dataset object, plus a safe script-tag serializer. Every
field degrades to omission on sparse/null data instead of throwing.

Tests cover a fully-populated nm* dataset, an OpenNeuro-derived on*
dataset, a sparse real dataset with nulls throughout, a synthetic
missing-DOI case, and </script> escaping. Fixtures are real captures
from api.nemar.org / data.nemar.org (2026-07-29).
Base.astro grows an optional jsonLdScript prop that drops pre-escaped
JSON-LD into a <script type="application/ld+json"> tag; the layout
stays generic and carries no mapping logic. The dataset detail page
builds the script via datasetJsonLdScript with the catalog row,
metadata, provenance, and resolved version it already fetched.
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Jul 29, 2026 •

Copy link
Copy Markdown

@neuromechanist

Copy link
Copy Markdown
Contributor Author

Thanks — the end-to-end injection probe is the check that mattered, and building a real Astro page against Base.astro rather than unit-testing the escape function in isolation is the right way to have done it. set:html not re-transforming the escaped bytes was the specific thing worth proving.

The live-catalog sweep is also useful beyond this PR: confirming zero ds*-prefixed ids across all 754 datasets validates the nm000154 stand-in, and finding no file_size under 10KB suggests nemar-cli#895 has actually been backfilled even though the issue is still open. Worth reporting upstream so it can be closed.

No changes made here. The two observations are filed rather than carried forward, per the no-deferred-findings rule:

  • JSON-LD: conditionsOfAccess skips share-alike licenses #209 — conditionsOfAccess skips share-alike. Agreed it is a completeness gap rather than a correctness bug, but it matters a little more in a machine-readable card than in UI copy, since the whole point of the field is letting an automated consumer decide before fetching.
  • nemar-cli#895 annex sizes — nothing to do in this repo per AGENTS.md; the existing hedge (preferring data_summary.size_human over catalogRow.file_size) already covers the recurrence case, and DatasetCard/MyDatasetCard surface the same field today, so this PR adds no new exposure path.

@neuromechanist
neuromechanist merged commit 6b5044c into main Jul 29, 2026
5 checks passed
@neuromechanist
neuromechanist deleted the feature/issue-156-dataset-jsonld branch September 30, 2026 19:25
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.

1 participant