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

@cucumber/microdata

v2.1.0

Published

Extract WHATWG microdata from a DOM

Downloads

2,913

Readme

Node.js CI

Microdata

This zero-dependency library converts a DOM to Microdata.

It can be used to extract "interesting" pieces of information from a DOM, such as Person, Order, MusicEvent etc.

All you need to do is to add the appropriate itemscope, itemtype and itemprop attributes to your HTML, and this library will be able to extract the data.

The library supports all schema.org types, and also allows custom Microdata types.

The returned Mircodata uses the JSON-LD format.

Installation

npm install @cucumber/microdata

Example

Given a sample DOM:

<!DOCTYPE html>
<div itemscope itemtype="https://schema.org/Person">
  <span itemprop="name">Jane Doe</span>
</div>

We can extract the Person on that page to a JSON-LD compliant JavaScript object:

const { microdata } = require('@cucumber/microdata')

const person = microdata('https://schema.org/Person', document)
console.log(person.name) // "Jane Doe"

If you are using TypeScript you can cast the result to a type from schema-dts:

import { microdata } from '@cucumber/microdata'
import { Person } from 'schema-dts'

const person = microdata('https://schema.org/Person', document) as Person
if (typeof person === 'string') throw new Error('Expected a Person object')
console.log(person.name) // "Jane Doe"

Custom value extraction

In some cases you may want finer grained control over how to extract values from the DOM. For example, you may have a CodeMirror editor sitting inside of an element:

<div itemtype="https://schema.org/Text">
  <!-- CodeMirror here -->
</div>

You can pass a custom extractValue function as the last argument to microdata or microdataAll:

const data = microdata(
  someSchemaType, 
  someElement,
  element => element.querySelector('.CodeMirror')?.CodeMirror?.getValue()
)

This function may return undefined. In that case, the default lookup mechanisms will be used.

Custom types

We recommend using the official types defined by schema.org if you can. Sometimes however, you may want to define your own types if the official types are insufficient.

You can see an example of how this is done in test/microdataTest.ts.

Usage in testing

This library can be used to write assertions against web pages. It works with any UI library as it only inspects the DOM. The only requirement is that the HTML has Microdata in it.

Here is an example from a hypothetical TODO list application:

import { microdata } from '@cucumber/microdata'

const itemList = microdata('https://schema.org/ItemList', element) as ItemList
const todos = itemList.itemListElement as Text[]
assert.deepStrictEqual(todos, ['Get milk', 'Feed dog'])

Arrays

Some microdata itemScopes allow itemProp elements that can be specified more than once. For example, if an ItemList has two or more itemListElement children, then the itemListElement field in the LD-JSON object will be an Array.

However, if there is only one child, it will have the value of that child rather than an array with one element.

And if there are none, the value of that child will be undefined.

The toArray function of this library will convert a value to an array with 0, 1 or more elements so you don't need to worry about this.

import { microdata, toArray } from '@cucumber/microdata'

const itemList = microdata('https://schema.org/ItemList', element) as ItemList
const todos = toArray(itemList.itemListElement) as Text[]
assert.deepStrictEqual(todos, ['Get milk', 'Feed dog'])

Credit

This library is based on the excellent, but abandoned microdata. It's been ported to TypeScript, and some bug fixes have been applied to make it compliant with JSON-LD.