@@ -3,14 +3,45 @@ function resolveReferences(ast, options = {}) {
33 if ( ! ast [ group ] || ! ast [ group ] [ id ] ) {
44 if ( options . strict )
55 throw new Error ( `Unresolved reference @${ group } =${ id } ` ) ;
6- return { id, unresolved : true } ;
6+ return { id, unresolved : true } ;
77 }
8- return { id, ...ast [ group ] [ id ] } ;
8+ return { id, ...ast [ group ] [ id ] } ;
9+ } ;
10+
11+ const expandMacros = ( block ) => {
12+ return block . flatMap ( ( line ) => {
13+ const match = line . match ( / @ @ m a c r o = ( [ \w - ] + ) : ( .* ) / ) ;
14+ if ( ! match || ! ast . _macroCache ) return [ line ] ;
15+
16+ const [ _ , macroName , rawParams ] = match ;
17+ const template = ast . _macroCache [ macroName ] ;
18+ if ( ! template ) return [ line ] ;
19+
20+ const params = { } ;
21+ rawParams . split ( ";" ) . forEach ( ( p ) => {
22+ const [ k , v ] = p . split ( "=" ) ;
23+ params [ k . trim ( ) ] = v . trim ( ) ;
24+ } ) ;
25+
26+ const rendered = template . replace ( / \{ \{ ( .* ?) \} \} / g, ( _ , key ) => {
27+ const val = params [ key ] || "" ;
28+ if ( val . startsWith ( "@@e=" ) ) {
29+ const id = val . slice ( 5 ) ;
30+ return ast . subjects ?. [ id ]
31+ || ast . participants ?. [ id ] ?. name
32+ || ast . tags ?. [ id ]
33+ || id ;
34+ }
35+ return val ;
36+ } ) ;
37+
38+ return [ rendered ] ;
39+ } ) ;
940 } ;
1041
1142 if ( Array . isArray ( ast . tasks ) ) {
1243 ast . tasks = ast . tasks . map ( ( task ) => {
13- const out = { ...task } ;
44+ const out = { ...task } ;
1445
1546 const ptpMatch = task . raw . match ( / @ p t p = ( [ ^ \s ] + ) / ) ;
1647 const subjMatch = [ ...task . raw . matchAll ( / (?: \s | ^ ) = ( [ ^ \s ] + ) / g) ] . pop ( ) ;
@@ -25,32 +56,37 @@ function resolveReferences(ast, options = {}) {
2556 }
2657
2758 if ( Array . isArray ( ast . meeting ) ) {
28- ast . meeting = ast . meeting . map ( ( line ) => {
29- const echoMatches = [ ...line . matchAll ( / @ @ e = ( [ ^ \s ] + ) / g) ] ;
30- let resolved = line ;
31-
32- for ( const match of echoMatches ) {
33- const id = match [ 1 ] ;
34- let replacement = id ;
35-
36- if ( ast . subjects ?. [ id ] ) {
37- replacement = ast . subjects [ id ] ;
38- } else if ( ast . participants ?. [ id ] ) {
39- replacement = ast . participants [ id ] . name ;
40- } else if ( ast . tags ?. [ id ] ) {
41- replacement = ast . tags [ id ] ;
42- } else if ( options . strict ) {
43- throw new Error ( `@@e=${ id } not found` ) ;
59+ ast . meeting = ast . meeting
60+ . map ( ( line ) => {
61+ const echoMatches = [ ...line . matchAll ( / @ @ e = ( [ ^ \s ] + ) / g) ] ;
62+ let resolved = line ;
63+
64+ for ( const match of echoMatches ) {
65+ const id = match [ 1 ] ;
66+ let replacement = id ;
67+
68+ if ( ast . subjects ?. [ id ] ) {
69+ replacement = ast . subjects [ id ] ;
70+ } else if ( ast . participants ?. [ id ] ) {
71+ replacement = ast . participants [ id ] . name ;
72+ } else if ( ast . tags ?. [ id ] ) {
73+ replacement = ast . tags [ id ] ;
74+ } else if ( options . strict ) {
75+ throw new Error ( `@@e=${ id } not found` ) ;
76+ }
77+
78+ resolved = resolved . replace ( match [ 0 ] , replacement ) ;
4479 }
4580
46- resolved = resolved . replace ( match [ 0 ] , replacement ) ;
47- }
81+ return resolved ;
82+ } ) ;
4883
49- return resolved ;
50- } ) ;
84+ ast . meeting = expandMacros ( ast . meeting ) ;
5185 }
5286
5387 return ast ;
5488}
5589
90+
91+
5692module . exports = { resolveReferences} ;
0 commit comments