Skip to content

Commit 3dec620

Browse files
committed
fix: actually bind mainEntity
1 parent a694de7 commit 3dec620

File tree

3 files changed

+36
-22
lines changed

3 files changed

+36
-22
lines changed

example/pages/plaque/plaque.rq

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ PREFIX sparqlc: <https://sparqlc.described.at/>
44
CONSTRUCT {
55
?s ?p ?o
66
} {
7-
BIND(IRI(sparqlc:param(schema:mainEntity)) as ?plaque)
7+
BIND(sparqlc:param(schema:mainEntity) as ?plaque)
88
GRAPH ?plaque {
99
?s ?p ?o
1010
}

labs/pages/lib/pageParameters.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export function fillTemplate(pattern: string, subjectVariables: Record<string, string>): string | null {
2+
const variables: string[] = []
3+
const regexStr = pattern.replace(/\[(\w+)]/g, (_, name) => {
4+
variables.push(name)
5+
return '(?<' + name + '>[^/]+)'
6+
})
7+
const regex = new RegExp(`^${regexStr}$`)
8+
9+
// We need to find if any of the subjectVariables match the pattern's variables
10+
// Actually, if we are in [name].html, subjectVariables['name'] is available.
11+
// If the pattern is 'https://.../[name]', we can reconstruct the IRI.
12+
let iri = pattern
13+
let allVarsFound = true
14+
for (const v of variables) {
15+
if (subjectVariables[v]) {
16+
iri = iri.replace(`[${v}]`, subjectVariables[v])
17+
} else {
18+
allVarsFound = false
19+
break
20+
}
21+
}
22+
23+
return allVarsFound ? iri : null
24+
}

labs/pages/lib/ssr.ts

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { createLogger } from '@kopflos-cms/logger'
1717
import selectPagePatterns from '../queries/page-patterns.rq'
1818
import SparqlProcessor from './SparqlProcessor.js'
1919
import PageUrlTransform from './PageUrlTransform.js'
20+
import { fillTemplate } from './pageParameters.js'
2021
import type { Page, QueryMap } from '@kopflos-labs/pages'
2122

2223
const log = createLogger('ssr')
@@ -56,6 +57,7 @@ async function executeQueries(renderer: Page, queries: QueryMap, { env, subjectV
5657
const endpoint: string | undefined = typeof descriptor === 'object' ? descriptor.endpoint : undefined
5758

5859
const client = endpoint ? env.sparql[endpoint].stream : env.sparql.default.stream
60+
5961
const params: TermMap<Term, Term | Term[]> = new TermMap<Term, Term | Term[]>([
6062
...Object.entries(subjectVariables).map<ParamMapEntry>(([key, value]) => [env.literal(key), env.literal(value)]),
6163
...Object.entries(queryParams).reduce((acc, [key, value]): ParamMapEntry[] => {
@@ -75,33 +77,21 @@ async function executeQueries(renderer: Page, queries: QueryMap, { env, subjectV
7577

7678
if (params.has(keyTerm)) continue
7779

78-
const variables: string[] = []
79-
const regexStr = pattern.replace(/\[(\w+)]/g, (_, name) => {
80-
variables.push(name)
81-
return '(?<' + name + '>[^/]+)'
82-
})
83-
const regex = new RegExp(`^${regexStr}$`)
84-
85-
// We need to find if any of the subjectVariables match the pattern's variables
86-
// Actually, if we are in [name].html, subjectVariables['name'] is available.
87-
// If the pattern is 'https://.../[name]', we can reconstruct the IRI.
88-
let iri = pattern
89-
let allVarsFound = true
90-
for (const v of variables) {
91-
if (subjectVariables[v]) {
92-
iri = iri.replace(`[${v}]`, subjectVariables[v])
93-
} else {
94-
allVarsFound = false
95-
break
96-
}
97-
}
80+
const bound = fillTemplate(pattern, subjectVariables)
9881

99-
if (allVarsFound) {
82+
if (bound) {
10083
params.set(keyTerm, env.literal(iri))
10184
}
10285
}
10386
}
10487

88+
if (renderer.mainEntity) {
89+
const mainEntity = fillTemplate(renderer.mainEntity, subjectVariables)
90+
if (mainEntity) {
91+
params.set(env.ns.schema.mainEntity, mainEntity.startsWith('http') ? env.namedNode(mainEntity) : env.kopflos.appNs(mainEntity))
92+
}
93+
}
94+
10595
const result = await query(params, {
10696
env,
10797
client,

0 commit comments

Comments
 (0)