Skip to content

Commit

Permalink
er: add enableSchemaUrlRedirect config option
Browse files Browse the repository at this point in the history
  • Loading branch information
ludovicm67 committed Aug 6, 2024
1 parent 1dbc2e3 commit 2215e87
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-months-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@zazuko/trifid-entity-renderer": minor
---

Add `enableSchemaUrlRedirect` configuration option (experimental).
2 changes: 2 additions & 0 deletions packages/entity-renderer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 13 additions & 0 deletions packages/entity-renderer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ const defaultConfiguration = {
LIMIT 1
`,
followRedirects: false,
enableSchemaUrlRedirect: false, // Experimental
}

const fixContentTypeHeader = (contentType) => {
Expand Down Expand Up @@ -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,
{
Expand Down

0 comments on commit 2215e87

Please sign in to comment.