@@ -17,6 +17,7 @@ import { createLogger } from '@kopflos-cms/logger'
1717import selectPagePatterns from '../queries/page-patterns.rq'
1818import SparqlProcessor from './SparqlProcessor.js'
1919import PageUrlTransform from './PageUrlTransform.js'
20+ import { fillTemplate } from './pageParameters.js'
2021import type { Page , QueryMap } from '@kopflos-labs/pages'
2122
2223const 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