Skip to content

Enrich shared pages with JSON-LD structured data#2912

Merged
arran4 merged 3 commits into
mainfrom
enrich-shared-jsonld-598031918209571218
Mar 7, 2026
Merged

Enrich shared pages with JSON-LD structured data#2912
arran4 merged 3 commits into
mainfrom
enrich-shared-jsonld-598031918209571218

Conversation

@google-labs-jules

Copy link
Copy Markdown
Contributor

This pull request enriches shared pages (e.g., forum threads, blog posts, news posts) by including JSON-LD structured data.

Features

  • JSONLD Struct: Implemented a generic JSONLD struct in core/common/jsonld.go with custom MarshalJSON and UnmarshalJSON to automatically inject @context (defaulting to schema.org) and switch schemas based on the @type property.
  • OpenGraph Updates: Extended the core/common.OpenGraph and handlers/share.OpenGraphData types to include a JSONLD interface{} field.
  • Template Methods: Created JSONLDScript() template.HTML helpers to safely generate the script tags without manual HTML composition.
  • Options System: Expanded handlers/share/options.go with a new WithJSONLD struct to cleanly pipe structured data into shared previews.
  • Widespread Integration: Updated all relevant shared_preview.go handlers across the codebase (Forum, Private Forum, Blogs, News, Writings) to support accepting and applying the WithJSONLD option.
  • Template Engine: Inserted the {{.JSONLDScript}} invocation directly into the core headdata.gohtml and openGraphPreview.gohtml templates for seamless frontend rendering.

PR created automatically by Jules for task 598031918209571218 started by @arran4

Introduces a `JSONLD` struct to dynamically inject `@context` and `@type`
fields for rendering `<script type="application/ld+json">` tags on
OpenGraph preview pages.

- Adds `core/common/jsonld.go` with custom JSON marshaling logic.
- Updates `OpenGraph` and `OpenGraphData` structs to support a `JSONLD` field.
- Implements `JSONLDScript()` method to safely output the script tag.
- Plumbs `WithJSONLD` option into all `handlers/*/shared_preview.go` components.
- Updates `headdata.gohtml` and `openGraphPreview.gohtml` to render the script.
@google-labs-jules

Copy link
Copy Markdown
Contributor Author

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Comment thread handlers/share/options.go Outdated
// Used by: share.Generate
type WithGeneratorType string

// WithJSONLD specifies the JSON-LD struct.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jules it should be constructed from the same data or at the same data level as the other things, so each component will need to be made a with rather than the whole thing sorry.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed WithJSONLD and added separate component options like WithJSONLDType, WithHeadline, WithDatePublished, WithDateModified, WithAuthorName, WithAuthorURL, and WithImages.

Comment thread core/common/coredata.go Outdated
ModifiedTime *time.Time
SiteName string
UpdatedTime *time.Time
JSONLD interface{}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jules this should be an interface or indicated through some sort of generic wrapper

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've replaced interface{} with a new generic JSONLDer interface that defines LDType() string and MarshalJSONLD() ([]byte, error).

Comment thread core/common/jsonld.go Outdated
)

// JSONLD is a concrete struct for LD-JSON data.
type JSONLD struct {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jules context and type should be implicit, and added during serialization and used to guide deserialization.

I am also expecting interfaces not any or interface{}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've refactored core/common/jsonld.go to use specific structs (like Article, BlogPosting) that implement the new JSONLDer interface. These specific structs implicitly provide their @type when serialized and we automatically inject the @context and @type during serialization via marshalWithContextAndType. I also updated deserialization to return an interface rather than parsing untyped generic maps.

@arran4

arran4 commented Mar 3, 2026

Copy link
Copy Markdown
Owner

@jules please ensure that every page that can provide the details are doing so

1 similar comment
@arran4

arran4 commented Mar 6, 2026

Copy link
Copy Markdown
Owner

@jules please ensure that every page that can provide the details are doing so

- Converted `JSONLD` from `interface{}` to a strongly-typed `JSONLDer` interface within OpenGraph data structures.
- Implemented `Article`, `NewsArticle`, `BlogPosting`, etc., to intrinsically provide their own `@type` through the `LDType()` method.
- Replaced the single `WithJSONLD` wrapper with multiple component-level generator options (`WithJSONLDType`, `WithHeadline`, `WithDatePublished`, `WithAuthorName`, `WithAuthorURL`, etc.) across `handlers/share/options.go`.
- Restructured `handlers/*/shared_preview.go` to construct structured data iteratively from parameters.
- Implemented robust internal `marshalWithContextAndType` helper to guarantee `@context` and `@type` inclusion during struct serialization.
Comment thread handlers/share/options.go

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jules WithAuhtor and WithAuthorName look like the same element, can we merge as many of these as possible. We can use sanity checks like "jsonld requires these 2 proprites to be populated" to sort out issues. There are others which are sssentially the same thing, what about 'headline' and 'title'?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have consolidated the options by removing WithHeadline and WithAuthorName, standardizing on WithTitle and WithAuthor across the options. The JSON-LD schema builder automatically maps the generic title variable into the Headline structure where needed.

@google-labs-jules

Copy link
Copy Markdown
Contributor Author

@jules please ensure that every page that can provide the details are doing so

I've gone through the various handler preview packages (news/, blogs/, writings/, forum/, privateforum/) and ensured they extract every detail available from the database rows, including formatting dates (DatePublished / DateModified) and fetching Author metadata securely.

@google-labs-jules

Copy link
Copy Markdown
Contributor Author

@jules please ensure that every page that can provide the details are doing so

All the individual page handlers have been updated to provide fully populated JSONLD fields.

…w data

- Removed redundant `WithHeadline` and `WithAuthorName` options, standardizing on `WithTitle` and `WithAuthor` for both the image generator and the JSON-LD payload.
- Enhanced all discrete content handler preview pages (`blogs/`, `news/`, `writings/`, `forum/`, `privateforum/`) to accurately parse and pass structural metrics like publish date, update date, and author attribution to the underlying structured data renderer.
@arran4 arran4 merged commit f365c74 into main Mar 7, 2026
24 checks passed
@arran4 arran4 deleted the enrich-shared-jsonld-598031918209571218 branch March 7, 2026 00:32
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