@@ -5,7 +5,7 @@ const blocksInit = require('./blocks.js');
55const operatorsInit = require ( './operators/index.js' ) ;
66const modifiersInit = require ( './modifiers.js' ) ;
77
8- const blockRegExp = / { ( [ a - z \d ] + ) } / gi;
8+ const blockRegExp = / { ( [ a - z \d : ] + ) } / gi;
99
1010function Dialect ( builder ) {
1111 this . builder = builder ;
@@ -62,18 +62,46 @@ Dialect.prototype.buildQuery = function (query) {
6262 return this . buildTemplate ( template . pattern , query ) ;
6363} ;
6464
65- Dialect . prototype . buildTemplate = function ( template , query ) {
66- const result = template . replaceAll ( blockRegExp , ( match , blockName ) => {
67- const block = this . blocks . get ( blockName ) ;
65+ Dialect . prototype . buildTemplate = function ( templateName , query ) {
66+ // If templateName is already a pattern string, use it directly
67+ if ( typeof templateName === 'string' && templateName . includes ( '{' ) ) {
68+ const template = templateName ;
69+ const result = template . replaceAll ( blockRegExp , ( match , blockName ) => {
70+ const block = this . blocks . get ( blockName ) ;
6871
69- if ( ! block ) {
70- // Return empty string for unknown blocks instead of throwing error
71- return '' ;
72- }
72+ if ( ! block ) {
73+ // Return empty string for unknown blocks instead of throwing error
74+ return '' ;
75+ }
76+
77+ const blockResult = block . call ( this , query ) ;
78+ return blockResult || '' ;
79+ } ) ;
80+
81+ // Clean up extra spaces
82+ return result . replaceAll ( / \s + / g, ' ' ) . trim ( ) ;
83+ }
84+
85+ // Otherwise, get the template by name
86+ const template = this . templates . get ( templateName ) ;
87+ if ( ! template ) {
88+ throw new Error ( 'Unknown template "' + templateName + '"' ) ;
89+ }
7390
74- const blockResult = block . call ( this , query ) ;
75- return blockResult || '' ;
76- } ) ;
91+ const result = template . pattern . replaceAll (
92+ blockRegExp ,
93+ ( match , blockName ) => {
94+ const block = this . blocks . get ( blockName ) ;
95+
96+ if ( ! block ) {
97+ // Return empty string for unknown blocks instead of throwing error
98+ return '' ;
99+ }
100+
101+ const blockResult = block . call ( this , query ) ;
102+ return blockResult || '' ;
103+ }
104+ ) ;
77105
78106 // Clean up extra spaces
79107 return result . replaceAll ( / \s + / g, ' ' ) . trim ( ) ;
0 commit comments