Skip to content

Instantly share code, notes, and snippets.

@nikolaswise
Created March 29, 2024 12:07
Show Gist options
  • Save nikolaswise/d500a4bf7d76134274377b8b47226301 to your computer and use it in GitHub Desktop.
Save nikolaswise/d500a4bf7d76134274377b8b47226301 to your computer and use it in GitHub Desktop.
Given a directory of html, extract all the RDFa statements and write a JSON-LD file.
import fs from 'fs'
import path from 'path'
import { JSDOM } from 'jsdom'
// https://gist.github.com/nikolaswise/d3c9cc914f1168bdf244cdc797c64eb8
import { rdfa2json } from './rdfa2json.js'
let origin = process.env.origin
global.DOMParser = new JSDOM().window.DOMParser
const handleError = (err) => {
console.error(err)
}
const parseFiles = (files) => {
const arr = files
.filter(file => path.extname(file) == ".html")
.map(file => {
let html = fs.readFileSync(`./src/${file}`, 'utf8')
let json = rdfa2json(html, `${origin}/${path.basename(file, '.html')}`)
return json
})
return {
"@context": `${origin}/context.json`,
"@graph": arr
}
}
fs.readdir('./src', (err, files) => {
if (err) {
handleError(err)
return
}
let data = parseFiles(files)
fs.writeFile('./src/index.json', JSON.stringify(data, null, 2), err => {
if (err)
handleError(err)
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment