MCPcopy
hub / github.com/11ty/buildawesome / compile

Function compile

src/Plugins/RenderPlugin.js:20–63  ·  view source on GitHub ↗

@this {object}

(content, templateLang, options = {})

Source from the content-addressed store, hash-verified

18
19/** @this {object} */
20async function compile(content, templateLang, options = {}) {
21 let { templateConfig, extensionMap } = options;
22 let strictMode = options.strictMode ?? false;
23
24 if (!templateConfig) {
25 templateConfig = new TemplateConfig(null, false);
26 templateConfig.setDirectories(new ProjectDirectories());
27 await templateConfig.init();
28 }
29
30 // Breaking change in 2.0+, previous default was `html` and now we default to the page template syntax
31 if (!templateLang) {
32 templateLang = this.page.templateSyntax;
33 }
34
35 if (!extensionMap) {
36 if (strictMode) {
37 throw new Error("Internal error: missing `extensionMap` in RenderPlugin->compile.");
38 }
39 extensionMap = new EleventyExtensionMap(templateConfig);
40 extensionMap.engineManager = new TemplateEngineManager(templateConfig);
41 }
42 let tr = new TemplateRender(templateLang, templateConfig);
43 tr.extensionMap = extensionMap;
44
45 if (templateLang) {
46 await tr.setEngineOverride(templateLang);
47 } else {
48 await tr.init();
49 }
50
51 // TODO tie this to the class, not the extension
52 if (
53 tr.engine.name === "11ty.js" ||
54 tr.engine.name === "11ty.cjs" ||
55 tr.engine.name === "11ty.mjs"
56 ) {
57 throw new Error(
58 "11ty.js is not yet supported as a template engine for `renderTemplate`. Use `renderFile` instead!",
59 );
60 }
61
62 return tr.getCompiledTemplate(content);
63}
64
65// No templateLang default, it should infer from the inputPath.
66async function compileFile(inputPath, options = {}, templateLang) {

Callers 1

compileMethod · 0.85

Calls 5

setEngineOverrideMethod · 0.95
initMethod · 0.95
getCompiledTemplateMethod · 0.95
setDirectoriesMethod · 0.80
initMethod · 0.45

Tested by

no test coverage detected