1- import type { BladeSyntaxPlugin } from "./statamic.js" ;
1+ import type { TreeDirectiveDefinition } from "../tree/directive-definitions.js" ;
2+ import { SAGE_PLUGIN_NAME , sagePlugin } from "./sage/index.js" ;
3+ import type { BladeSyntaxPlugin } from "./types.js" ;
24import { statamicPlugin } from "./statamic.js" ;
35
46export interface BladeSyntaxProfile {
57 lexerDirectives : string [ ] ;
6- treeDirectives : unknown [ ] ;
8+ treeDirectives : TreeDirectiveDefinition [ ] ;
79 verbatimStartDirectives : string [ ] ;
810 verbatimEndDirectives : string [ ] ;
911}
1012
1113const BUILTIN_BLADE_SYNTAX_PLUGINS = new Map < string , BladeSyntaxPlugin > ( [
14+ [ SAGE_PLUGIN_NAME , sagePlugin ] ,
1215 [ "statamic" , statamicPlugin ] ,
1316] ) ;
17+ const resolvedBladeSyntaxPluginsCache = new WeakMap < object , BladeSyntaxPlugin [ ] > ( ) ;
1418
1519function isRecord ( value : unknown ) : value is Record < string , unknown > {
1620 return typeof value === "object" && value !== null ;
@@ -66,26 +70,11 @@ function resolveBladeSyntaxPluginEntry(value: unknown): BladeSyntaxPlugin | null
6670}
6771
6872export function resolveBladeSyntaxProfile ( options ?: unknown ) : BladeSyntaxProfile {
69- const optionRecord = isRecord ( options ) ? options : { } ;
70- const rawPlugins = parseBladeSyntaxPluginTokens ( optionRecord . bladeSyntaxPlugins ) ;
71-
72- const seenPluginNames = new Set < string > ( ) ;
73- const resolvedPlugins : BladeSyntaxPlugin [ ] = [ ] ;
74-
75- for ( const entry of rawPlugins ) {
76- const plugin = resolveBladeSyntaxPluginEntry ( entry ) ;
77- if ( ! plugin ) continue ;
78-
79- const key = plugin . name . toLowerCase ( ) ;
80- if ( seenPluginNames . has ( key ) ) continue ;
81- seenPluginNames . add ( key ) ;
82- resolvedPlugins . push ( plugin ) ;
83- }
84-
73+ const resolvedPlugins = resolveBladeSyntaxPlugins ( options ) ;
8574 const lexerDirectives = new Set < string > ( ) ;
8675 const verbatimStartDirectives = new Set < string > ( ) ;
8776 const verbatimEndDirectives = new Set < string > ( ) ;
88- const treeDirectives : unknown [ ] = [ ] ;
77+ const treeDirectives : TreeDirectiveDefinition [ ] = [ ] ;
8978
9079 for ( const plugin of resolvedPlugins ) {
9180 for ( const directive of plugin . lexerDirectives ) {
@@ -112,3 +101,29 @@ export function resolveBladeSyntaxProfile(options?: unknown): BladeSyntaxProfile
112101 verbatimEndDirectives : [ ...verbatimEndDirectives ] ,
113102 } ;
114103}
104+
105+ export function resolveBladeSyntaxPlugins ( options ?: unknown ) : BladeSyntaxPlugin [ ] {
106+ const optionRecord = isRecord ( options ) ? options : { } ;
107+ const cached = resolvedBladeSyntaxPluginsCache . get ( optionRecord ) ;
108+ if ( cached ) {
109+ return cached ;
110+ }
111+
112+ const rawPlugins = parseBladeSyntaxPluginTokens ( optionRecord . bladeSyntaxPlugins ) ;
113+
114+ const seenPluginNames = new Set < string > ( ) ;
115+ const resolvedPlugins : BladeSyntaxPlugin [ ] = [ ] ;
116+
117+ for ( const entry of rawPlugins ) {
118+ const plugin = resolveBladeSyntaxPluginEntry ( entry ) ;
119+ if ( ! plugin ) continue ;
120+
121+ const key = plugin . name . toLowerCase ( ) ;
122+ if ( seenPluginNames . has ( key ) ) continue ;
123+ seenPluginNames . add ( key ) ;
124+ resolvedPlugins . push ( plugin ) ;
125+ }
126+
127+ resolvedBladeSyntaxPluginsCache . set ( optionRecord , resolvedPlugins ) ;
128+ return resolvedPlugins ;
129+ }
0 commit comments