Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | 8x 85x 85x 85x 85x 85x 85x 232x 15x 15x 232x 232x 232x 10x 17x | import path from 'path';
import nunjucks, { Environment } from 'nunjucks';
import { PageSources } from '../Page/PageSources.js';
import {
dateFilter,
SetExternalExtension,
} from '../lib/nunjucks-extensions/index.js';
import * as fsUtil from '../utils/fsUtil.js';
import '../patches/nunjucks/index.js'; // load patch
const unescapedEnv = nunjucks.configure({ autoescape: false })
.addFilter('date', dateFilter);
/**
* Wrapper class over a nunjucks environment configured for the respective (sub)site.
*/
export class VariableRenderer {
private pageSources = new PageSources();
private nj: Environment;
constructor(private siteRootPath: string) {
this.nj = nunjucks.configure(siteRootPath, { autoescape: false });
this.nj.addFilter('date', dateFilter);
this.nj.addExtension('SetExternalExtension', new SetExternalExtension(siteRootPath, this.nj));
this.nj.on('load', (name, source) => {
this.pageSources.staticIncludeSrc.push({ to: source.path });
});
}
/**
* Processes content with the instance's nunjucks environment.
* @param content to process
* @param variables to render the content with
* @param pageSources to add dependencies found during nunjucks rendering to
* @return nunjucks processed content
*/
renderString(
content: string,
variables: Record<string, any>,
pageSources: PageSources,
) {
this.pageSources = pageSources;
return this.nj.renderString(content, variables);
}
/**
* Processes file content with the instance's nunjucks environment.
* @param contentFilePath to process
* @param variables to render the content with
* @param pageSources to add dependencies found during nunjucks rendering to
* @return nunjucks processed content
*/
renderFile(
contentFilePath: string,
variables: Record<string, any>,
pageSources: PageSources,
) {
this.pageSources = pageSources;
const templateName = fsUtil.ensurePosix(path.relative(this.siteRootPath, contentFilePath));
return this.nj.render(templateName, variables);
}
/**
Invalidate the internal nunjucks template cache
*/
invalidateCache() {
// Custom method from our patch
// @ts-ignore
this.nj.invalidateCache();
}
/**
* Compiles a template specified at src independent of the template directory.
* This is used for the page template file (page.njk), where none of nunjucks' features
* involving path resolving are used.
* @param templatePath of the template to compile
*/
static compile(templatePath: string) {
return nunjucks.compile(templatePath, unescapedEnv);
}
}
|