Using Netlify Edge Functions to transform RDFa (text/html) into Turtle

A little Sunday night hack: @Virginia writes her website with RDFa in the HTML, and we wanted to be able to query it as a Solid Dataset, without needing to move it to a solid server. Sure, you can query RDFa through comunica and other tools, but ~175 lines of code and a lot of cursing at Deno later, and we have her site content negotiating to text/turtle.

Try it out:

curl -i -H 'Accept: text/turtle' https://virginiabalseiro.com/

Code: A small netlify edge function to transform text/html documents with RDFa in them into text/turtle, see: https://forum.solidproject.org/t/using-netlify-functions-to-transform-rdfa-text-html-into-turtle/5765 · GitHub

This leverages the packages that @rubensworks and contributors have written for working with RDFa, RDF, and turtle. It’s deployed using Netlify Edge Functions, but with a few modifications, you could almost definitely deploy similar on Cloudflare Workers or any other edge runtime.

There were some issues where the packages that handle parsing and serialising rely on Node.js types, and this trips Deno up, so we don’t quite have full type safety in the code, but that’s a minor issue (it did work with the types in development, just not when we went to build for production).

9 Likes

Update: the code now supports json-ld and more, thanks to @RubenVerborgh & @jeswr pointing me in the direction of https://www.npmjs.com/package/rdf-serialize

I’ve updated the code accordingly: A small netlify edge function to transform text/html documents with RDFa in them into text/turtle, see: https://forum.solidproject.org/t/using-netlify-functions-to-transform-rdfa-text-html-into-turtle/5765 · GitHub

You could also use a similar approach if you had .ttl that you wanted as json-ld or json-ld as turtle.

5 Likes