JSON-LD

node v12.22.12
version: 1.0.0
endpointsharetweet
const { JSDOM } = require('jsdom') const { compact, expand, frame } = require('jsonld') const url = 'https://www.bbc.co.uk/schedules/p00fzl6p/2020/06/14' // fetch and parse HTML const { window: { document } } = await JSDOM.fromURL(url) // select the script elements containing JSON-LD const elements = document.querySelectorAll('script[type="application/ld+json"]') for (const element of elements) { // parse the JSON const doc = JSON.parse(element.textContent) console.log({ doc }) // expand the doc to use full URIs for property names // const expanded = await expand(doc) // console.log({ expanded }) // frame the doc to filter and pick certain properties const framed = await frame(doc, { '@context': 'https://schema.org/', '@type': 'TVEpisode', '@explicit': true, identifier: {}, description: {}, name: {}, published: {}, image: {}, partOfSeries: { '@type': 'TVSeries', '@explicit': true, '@default': [], identifier: {}, name: {}, }, partOfSeason: { '@type': 'TVSeason', '@explicit': true, '@default': [], identifier: {}, name: {}, position: {}, }, }) console.log({ framed }) // compact the framed doc to use own property names const compacted = await compact(framed, { '@context': { identifier: 'http://schema.org/identifier', description: 'http://schema.org/description', name: 'http://schema.org/name', series: 'http://schema.org/partOfSeries', season: 'http://schema.org/partOfSeason', position: 'http://schema.org/position', image: { '@type': '@id', '@id': 'http://schema.org/image', }, published: { '@type': 'http://schema.org/Date', '@id': 'http://schema.org/datePublished', }, }, }) console.log({ compacted }) }
Loading…

no comments

    sign in to comment