npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@gearbox-built/sanity-structured-data

v0.3.0

Published

A Sanity plugin that enables easy implementation of JSON-LD structured data using Schema.org vocabulary.

Downloads

162

Readme

⚡️ Sanity Structured Data

A Sanity plugin that enables easy implementation of JSON-LD structured data using Schema.org vocabulary.

Inspired by Operation Nation's sanity-plugin-schema-markup plugin.

Installation

npm install @gearbox-built/sanity-structured-data

Sanity Usage

Add it as a plugin in sanity.config.ts (or .js):

Basic Implementation

// sanity.config.ts
import {defineConfig} from 'sanity'
import {structuredData} from '@gearbox-built/sanity-structured-data'

export default defineConfig({
  //...
  plugins: [structuredData()],
})

Add the structuredData schemaField to your document:

const page = {
  type: 'document',
  name: 'Page',
  fields: [
    {
      title: 'Structured Data',
      name: 'structuredData',
      type: 'structuredData',
    },
  ],
}

Or if you're using @gearbox-built/sanity-schema-tool:

import {F} from '@gearbox-built/sanity-schema-tool'
const page = F.document({
  name: 'Page',
  fields: [F.field('structuredData')],
})

Config Options

structuredData({
  apiVersion: '',
  types: {
    article: {},
    webSite: {},
    webPage: {},
    //...
  },
  queries: {
    company: '',
    websiteDescription: '',
    //...
  },
})

apiVersion

Default: v2021-03-25

Sanity API version.

types

Schema type overrides map. Replaces Sanity schema for the respective Schema.org schema type.

Note: Directly overrides default schema structure. Use Types Helpers for default schema structure.

queries

Query overrides map. Default GROQ queries for schema fields that query the Sanity dataset for values (query fields).


Helpers

To make customization more simple, default schema, types, fields, utilites.

Types

Default schema types (ie. WebPage, WebSite, etc.).

import {types} from '@gearbox-built/sanity-structured-data'
const {webPage, videoObject} = types

console.log(webPage)
// {
//   type: 'object',
//   name: 'WebPageDataType',
//   title: 'WebPage',
//   fields: [ ... ]
//   //...
// }

Fields

Default schema types and custom Schema Tool fields as field fuctions.

Supports merging props with full TypeScript support.

import {fields} from '@gearbox-built/sanity-structured-data'
const {webPage} = fields

console.log(webPage({title: 'Definitely Not WebPage'}))
// {
//   type: 'object',
//   name: 'WebPageDataType',
//   title: 'Definitely Not WebPage', // ← Prop merged
//   fields: [ ... ]
//   //...
// }

Structured Data Config

For more complex configuration needs, abstracting configuration may be desired. To assist with this, you can use the structuredDataConfig helper:

// sanity.config.ts
import {defineConfig} from 'sanity'
import {structuredData} from '@gearbox-built/sanity-structured-data'
import {config} from './structuredDataConfig'

export default defineConfig({
  //...
  plugins: [structuredData(config)],
})
// structuredDataConfig.ts
import {structuredDataConfig, types} from '@gearbox-built/sanity-structured-data'
import {F} from '@gearbox-built/sanity-schema-tool'

// WebSite schema additions
const {fields: webSiteFields, ...webSiteType} = types.webSite

const webSiteAdditionalFields = [
  F.string({name: 'about', description: 'The subject matter of the content.'}),
]

const webSite = {
  ...webSiteType,
  title: 'Definitely Not WebSite',
  fields: webSiteFields.toSpliced(2, 0, ...webSiteAdditionalFields), // Order fields
}

export const config = structuredDataConfig({
  types: {
    webSite, // Replaces default WebSite schema
  },
  queries: {
    company: `*[_type == 'settings'][0].companyName`,
  },
})

Frontend Component

Render your structured data markup in your React/Next.js app. Can be used with or without the structuredData Sanity schema.

import StructuredData, {Schema} from '@gearbox-built/sanity-structured-data/component'

type Props = {
  structuredData: Schema[]
}

const projectId = process.env.NEXT_PUBLIC_SANITY_PROJECT_ID
const dataset = process.env.NEXT_PUBLIC_SANITY_DATASET

const SchemaMarkup = ({structuredData}: Props) => (
  <StructuredData data={structuredData} projectId={projectId} dataset={dataset} />
)

export default SchemaMarkup

License

MIT © Gearbox Development Inc.

Develop & test

This plugin uses @sanity/plugin-kit with default configuration for build & watch scripts.

See Testing a plugin in Sanity Studio on how to run this plugin with hotreload in the studio.