Created
March 29, 2024 12:07
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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