1
1
import { Liquid , type Template } from "liquidjs" ;
2
- import type { RenderOptions } from "liquidjs/dist/liquid-options" ;
2
+ import type { LiquidOptions , RenderOptions } from "liquidjs/dist/liquid-options" ;
3
3
import compact from "lodash-es/compact" ;
4
4
import uniq from "lodash-es/uniq" ;
5
5
@@ -10,7 +10,7 @@ import type { GlobalData } from "eleventy.config";
10
10
import { biblioPattern , getBiblio } from "./biblio" ;
11
11
import { flattenDom , load , type CheerioAnyNode } from "./cheerio" ;
12
12
import { generateId } from "./common" ;
13
- import { getAcknowledgementsForVersion , getTermsMap } from "./guidelines" ;
13
+ import { getAcknowledgementsForVersion , type TermsMap } from "./guidelines" ;
14
14
import { resolveTechniqueIdFromHref , understandingToTechniqueLinkSelector } from "./techniques" ;
15
15
import { techniqueToUnderstandingLinkSelector } from "./understanding" ;
16
16
@@ -22,7 +22,6 @@ const techniquesPattern = /\btechniques\//;
22
22
const understandingPattern = / \b u n d e r s t a n d i n g \/ / ;
23
23
24
24
const biblio = await getBiblio ( ) ;
25
- const termsMap = await getTermsMap ( ) ;
26
25
const termLinkSelector = "a:not([href])" ;
27
26
28
27
/** Generates {% include "foo.html" %} directives from 1 or more basenames */
@@ -72,6 +71,10 @@ function expandTechniqueLink($el: CheerioAnyNode) {
72
71
73
72
const stripHtmlComments = ( html : string ) => html . replace ( / < ! - - [ \s \S ] * ?- - > / g, "" ) ;
74
73
74
+ interface CustomLiquidOptions extends LiquidOptions {
75
+ termsMap : TermsMap ;
76
+ }
77
+
75
78
// Dev note: Eleventy doesn't expose typings for its template engines for us to neatly extend.
76
79
// Fortunately, it passes both the content string and the file path through to Liquid#parse:
77
80
// https://github.com/11ty/eleventy/blob/9c3a7619/src/Engines/Liquid.js#L253
@@ -84,6 +87,11 @@ const stripHtmlComments = (html: string) => html.replace(/<!--[\s\S]*?-->/g, "")
84
87
* - generating/expanding sections with auto-generated content
85
88
*/
86
89
export class CustomLiquid extends Liquid {
90
+ termsMap : TermsMap ;
91
+ constructor ( options : CustomLiquidOptions ) {
92
+ super ( options ) ;
93
+ this . termsMap = options . termsMap ;
94
+ }
87
95
public parse ( html : string , filepath ?: string ) {
88
96
// Filter out Liquid calls for computed data and includes themselves
89
97
if ( filepath && ! filepath . includes ( "_includes/" ) && isHtmlFileContent ( html ) ) {
@@ -300,7 +308,7 @@ export class CustomLiquid extends Liquid {
300
308
public async render ( templates : Template [ ] , scope : GlobalData , options ?: RenderOptions ) {
301
309
// html contains markup after Liquid tags/includes have been processed
302
310
const html = ( await super . render ( templates , scope , options ) ) . toString ( ) ;
303
- if ( ! isHtmlFileContent ( html ) || ! scope ) return html ;
311
+ if ( ! isHtmlFileContent ( html ) || ! scope || scope . page . url === false ) return html ;
304
312
305
313
const $ = load ( html ) ;
306
314
@@ -414,7 +422,7 @@ export class CustomLiquid extends Liquid {
414
422
. toLowerCase ( )
415
423
. trim ( )
416
424
. replace ( / \s * \n + \s * / , " " ) ;
417
- const term = termsMap [ name ] ;
425
+ const term = this . termsMap [ name ] ;
418
426
if ( ! term ) {
419
427
console . warn ( `${ scope . page . inputPath } : Term not found: ${ name } ` ) ;
420
428
return ;
@@ -428,7 +436,7 @@ export class CustomLiquid extends Liquid {
428
436
const $el = $ ( el ) ;
429
437
const termName = extractTermName ( $el ) ;
430
438
$el
431
- . attr ( "href" , `${ scope . guidelinesUrl } #${ termName ? termsMap [ termName ] . trId : "" } ` )
439
+ . attr ( "href" , `${ scope . guidelinesUrl } #${ termName ? this . termsMap [ termName ] . trId : "" } ` )
432
440
. attr ( "target" , "terms" ) ;
433
441
} ) ;
434
442
} else if ( scope . isUnderstanding ) {
@@ -442,7 +450,7 @@ export class CustomLiquid extends Liquid {
442
450
// since terms may reference other terms in their own definitions.
443
451
// Each iteration may append to termNames.
444
452
for ( let i = 0 ; i < termNames . length ; i ++ ) {
445
- const term = termsMap [ termNames [ i ] ] ;
453
+ const term = this . termsMap [ termNames [ i ] ] ;
446
454
if ( ! term ) continue ; // This will already warn via extractTermNames
447
455
448
456
const $definition = load ( term . definition ) ;
@@ -459,7 +467,7 @@ export class CustomLiquid extends Liquid {
459
467
return 0 ;
460
468
} ) ;
461
469
for ( const name of termNames ) {
462
- const term = termsMap [ name ] ; // Already verified existence in the earlier loop
470
+ const term = this . termsMap [ name ] ; // Already verified existence in the earlier loop
463
471
$termsList . append (
464
472
`<dt id="${ term . id } ">${ term . name } </dt>` +
465
473
`<dd><definition>${ term . definition } </definition></dd>`
@@ -469,7 +477,7 @@ export class CustomLiquid extends Liquid {
469
477
// Iterate over non-href links once more in now-expanded document to add hrefs
470
478
$ ( termLinkSelector ) . each ( ( _ , el ) => {
471
479
const name = extractTermName ( $ ( el ) ) ;
472
- el . attribs . href = `#${ name ? termsMap [ name ] . id : "" } ` ;
480
+ el . attribs . href = `#${ name ? this . termsMap [ name ] . id : "" } ` ;
473
481
} ) ;
474
482
} else {
475
483
// No terms: remove skeleton that was placed in #parse
0 commit comments