From 2215e87e79cfac3661d387ca6088a94750946ec8 Mon Sep 17 00:00:00 2001 From: Ludovic Muller Date: Tue, 6 Aug 2024 13:34:06 +0200 Subject: [PATCH] er: add enableSchemaUrlRedirect config option --- .changeset/lovely-months-mix.md | 5 +++++ packages/entity-renderer/README.md | 2 ++ packages/entity-renderer/index.js | 13 +++++++++++++ 3 files changed, 20 insertions(+) create mode 100644 .changeset/lovely-months-mix.md diff --git a/.changeset/lovely-months-mix.md b/.changeset/lovely-months-mix.md new file mode 100644 index 00000000..33e58433 --- /dev/null +++ b/.changeset/lovely-months-mix.md @@ -0,0 +1,5 @@ +--- +"@zazuko/trifid-entity-renderer": minor +--- + +Add `enableSchemaUrlRedirect` configuration option (experimental). diff --git a/packages/entity-renderer/README.md b/packages/entity-renderer/README.md index f6dc6f6c..61bb259b 100644 --- a/packages/entity-renderer/README.md +++ b/packages/entity-renderer/README.md @@ -93,6 +93,8 @@ The default redirect query supports `http://www.w3.org/2011/http#` and `http://w Each rule is either a string or an object with the following properties: - `find`: the string to find - `replace`: the string to replace with (optional, the default value will be the current hostname) +- `enableSchemaUrlRedirect` (experimental): If set to `true`, the plugin will perform a redirect if the URI contains a `schema:URL` predicate poiting to a resource of type `xsd:anyURI`. + The default value is `false`. ## Run an example instance diff --git a/packages/entity-renderer/index.js b/packages/entity-renderer/index.js index b8ef9329..d68430e2 100644 --- a/packages/entity-renderer/index.js +++ b/packages/entity-renderer/index.js @@ -103,6 +103,7 @@ const defaultConfiguration = { LIMIT 1 `, followRedirects: false, + enableSchemaUrlRedirect: false, // Experimental } const fixContentTypeHeader = (contentType) => { @@ -254,6 +255,18 @@ const factory = async (trifid) => { const dataset = await rdf.dataset().import(quadStream) + if (mergedConfig.enableSchemaUrlRedirect && acceptHeader === 'text/html') { + // Get all triples that have a schema:URL property with value of type xsd:anyURI + const urls = [] + dataset.match(iriUrlString, rdf.ns.schema.URL) + .filter(({ object }) => object.datatype.value === 'xsd:anyURI') + .map(({ object }) => urls.push(object.value)) + if (urls.length > 0) { + logger.debug(`Redirecting to ${urls[0]}`) + return reply.redirect(urls[0]) + } + } + const { entityHtml, entityLabel, entityUrl } = await entityRenderer( request, {