Skip to content

Instantly share code, notes, and snippets.

@EpicKiwi
Created January 29, 2025 13:42
Show Gist options
  • Select an option

  • Save EpicKiwi/30c86c28fc0c9a8a0b3a7c14100e13c3 to your computer and use it in GitHub Desktop.

Select an option

Save EpicKiwi/30c86c28fc0c9a8a0b3a7c14100e13c3 to your computer and use it in GitHub Desktop.
Parse RDFa from HTML in Deno
import jsonld from "https://esm.sh/jsonld?target=esnext"
import jsonldRdfaParser from "https://esm.sh/jsonld8-rdfa-parser?target=esnext"
import { DOMParser, Node } from "https://esm.sh/linkedom?target=esnext";
import * as path from "jsr:@std/path"
//@ts-ignore
Node.prototype.getAttributeNodeNS = function(_, attr){ return this.getAttributeNode(attr) }
jsonld.registerRDFParser('text/html', jsonldRdfaParser);
const filePath = "index.html"
const document = parser.parseFromString(await Deno.readTextFile(filePath), "text/html")
const base = document.createElement("base")
base.setAttribute("href", path.toFileUrl(path.resolve(filePath)))
document.head.append(base)
const json = await jsonld.fromRDF(document.documentElement, {format: 'text/html'})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment