diff --git a/src/Benchmark/BenchmarkGroup.js b/src/Benchmark/BenchmarkGroup.js index ee82f6b59..4835fad9e 100644 --- a/src/Benchmark/BenchmarkGroup.js +++ b/src/Benchmark/BenchmarkGroup.js @@ -28,7 +28,7 @@ class BenchmarkGroup { } // TODO use addAsync everywhere instead - add(type, callback) { + add(type, callback, misc = {}) { let benchmark = (this.benchmarks[type] = new Benchmark()); /** @this {any} */ @@ -43,6 +43,7 @@ class BenchmarkGroup { value: { type: isAsyncFunction(callback) ? "async" : "sync", callback, + ...misc, }, }); @@ -73,7 +74,9 @@ class BenchmarkGroup { setMinimumThresholdPercent(minimumThresholdPercent) { let val = parseInt(minimumThresholdPercent, 10); if (isNaN(val)) { - throw new Error("`setMinimumThresholdPercent` expects a number argument."); + throw new Error( + "`setMinimumThresholdPercent` expects a number argument.", + ); } this.minimumThresholdPercent = val; } diff --git a/src/Data/TemplateData.js b/src/Data/TemplateData.js index acd6f69e1..3d4973d05 100644 --- a/src/Data/TemplateData.js +++ b/src/Data/TemplateData.js @@ -58,15 +58,6 @@ class TemplateData { return this.dirs.data; } - // This was async in 2.0 and prior but doesn’t need to be any more. - getInputDir() { - return this.dirs.input; - } - - getDataDir() { - return this.dataDir; - } - exists(pathname) { // It's common for data files not to exist, so we avoid going to the FS to // re-check if they do via a quick-and-dirty cache. diff --git a/src/Eleventy.js b/src/Eleventy.js index 308e48fd3..2846001a6 100644 --- a/src/Eleventy.js +++ b/src/Eleventy.js @@ -978,7 +978,7 @@ Arguments: return ( path.endsWith(".css") && // TODO how to make this work with relative includes? - !TemplatePath.startsWithSubPath(path, this.eleventyFiles.getIncludesDir()) + !TemplatePath.startsWithSubPath(path, this.eleventyFiles?.includesDir) ); }); @@ -1106,7 +1106,7 @@ Arguments: return; } - let dataDir = TemplatePath.stripLeadingDotSlash(this.templateData.getDataDir()); + let dataDir = TemplatePath.stripLeadingDotSlash(this.templateData?.dataDir); function filterOutGlobalDataFiles(path) { return !dataDir || !TemplatePath.stripLeadingDotSlash(path).startsWith(dataDir); } diff --git a/src/EleventyFiles.js b/src/EleventyFiles.js index 73065d706..2e5cfd2e2 100644 --- a/src/EleventyFiles.js +++ b/src/EleventyFiles.js @@ -49,11 +49,6 @@ class EleventyFiles { return this.dirs.data; } - // Backwards compat - getDataDir() { - return this.dataDir; - } - setFileSystemSearch(fileSystemSearch) { this.fileSystemSearch = fileSystemSearch; } @@ -292,16 +287,6 @@ class EleventyFiles { return Array.from(ignoreFiles); } - /* Backwards compat */ - getIncludesDir() { - return this.includesDir; - } - - /* Backwards compat */ - getLayoutsDir() { - return this.layoutsDir; - } - getFileGlobs() { return this.normalizedTemplateGlobs; } diff --git a/src/Engines/JavaScript.js b/src/Engines/JavaScript.js index 383f9a806..84758a6c6 100644 --- a/src/Engines/JavaScript.js +++ b/src/Engines/JavaScript.js @@ -14,6 +14,7 @@ export default class JavaScript extends TemplateEngine { this.instances = {}; this.cacheable = false; + this.filters = templateConfig.config.__theCodeCriesInPain.javascript; this.config.events.on("eleventy#templateModified", (inputPath, metadata = {}) => { let { usedByDependants, relevantLayouts } = metadata; @@ -141,7 +142,7 @@ export default class JavaScript extends TemplateEngine { getJavaScriptFunctions(inst) { let fns = {}; - let configFns = this.config.javascriptFunctions; + let configFns = this.filters; for (let key in configFns) { // prefer pre-existing `page` javascriptFunction, if one exists diff --git a/src/Engines/Liquid.js b/src/Engines/Liquid.js index 9d5a92796..c6bc6a1ae 100644 --- a/src/Engines/Liquid.js +++ b/src/Engines/Liquid.js @@ -22,6 +22,8 @@ export default class Liquid extends TemplateEngine { this.liquidOptions = this.config.liquidOptions || {}; + this.filters = eleventyConfig.config.__theCodeCriesInPain.liquid.filters + this.setLibrary(this.config.libraryOverrides.liquid); this.argLexer = moo.compile(Liquid.argumentLexerOptions); @@ -32,9 +34,7 @@ export default class Liquid extends TemplateEngine { // warning, the include syntax supported here does not exactly match what Jekyll uses. this.liquidLib = override || new LiquidJs(this.getLiquidOptions()); this.setEngineLib(this.liquidLib); - - this.addFilters(this.config.liquidFilters); - + this.addFilters(this.filters); // TODO these all go to the same place (addTag), add warnings for overwrites this.addCustomTags(this.config.liquidTags); this.addAllShortcodes(this.config.liquidShortcodes); diff --git a/src/Engines/Markdown.js b/src/Engines/Markdown.js index b43c25c6e..24932c6fe 100644 --- a/src/Engines/Markdown.js +++ b/src/Engines/Markdown.js @@ -16,14 +16,6 @@ export default class Markdown extends TemplateEngine { setLibrary(mdLib) { this.mdLib = mdLib || markdownIt(this.getMarkdownOptions()); - // Overrides a highlighter set in `markdownOptions` - // This is separate so devs can pass in a new mdLib and still use the official eleventy plugin for markdown highlighting - if (this.config.markdownHighlighter && typeof this.mdLib.set === "function") { - this.mdLib.set({ - highlight: this.config.markdownHighlighter, - }); - } - if (typeof this.mdLib.disable === "function") { // Disable indented code blocks by default (Issue #2438) this.mdLib.disable("code"); diff --git a/src/Engines/Nunjucks.js b/src/Engines/Nunjucks.js index 8676d4c17..167ffbf1b 100755 --- a/src/Engines/Nunjucks.js +++ b/src/Engines/Nunjucks.js @@ -5,7 +5,6 @@ import { TemplatePath } from "@11ty/eleventy-utils"; import TemplateEngine from "./TemplateEngine.js"; import EleventyBaseError from "../Errors/EleventyBaseError.js"; import { augmentObject } from "./Util/ContextAugmenter.js"; -import { withResolvers } from "../Util/PromiseUtil.js"; const debug = debugUtil("Eleventy:Nunjucks"); @@ -20,6 +19,9 @@ export default class Nunjucks extends TemplateEngine { this.nunjucksPrecompiledTemplates = this.config.nunjucksPrecompiledTemplates || {}; this._usingPrecompiled = Object.keys(this.nunjucksPrecompiledTemplates).length > 0; + this.asyncFilters = eleventyConfig.config.__theCodeCriesInPain.nunjucks.asyncFilters || {}; + this.filters = eleventyConfig.config.__theCodeCriesInPain.nunjucks.filters + this.setLibrary(this.config.libraryOverrides.njk); // v3.1.0-alpha.1 we’ve moved to use Nunjucks’ internal cache instead of Eleventy’s @@ -28,7 +30,7 @@ export default class Nunjucks extends TemplateEngine { #getFileSystemDirs() { let paths = new Set(); - paths.add(super.getIncludesDir()); + paths.add(super.includesDir); paths.add(TemplatePath.getWorkingDir()); // Filter out undefined paths @@ -100,9 +102,8 @@ export default class Nunjucks extends TemplateEngine { }); this.setEngineLib(this.njkEnv); - - this.addFilters(this.config.nunjucksFilters); - this.addFilters(this.config.nunjucksAsyncFilters, true); + this.addFilters(this.filters); + this.addFilters(this.asyncFilters, true); // TODO these all go to the same place (addTag), add warnings for overwrites // TODO(zachleat): variableName should work with quotes or without quotes (same as {% set %}) @@ -121,7 +122,35 @@ export default class Nunjucks extends TemplateEngine { addFilters(filters, isAsync) { for (let name in filters) { - this.njkEnv.addFilter(name, Nunjucks.wrapFilter(name, filters[name]), isAsync); + let callback = filters[name]; + if (!isAsync) { + /** @this {any} */ + callback = function (...args) { + // Note that `callback` is already a function as the `#add` method throws an error if not. + let ret = filters[name].call(this, ...args); + if (ret instanceof Promise) { + throw new Error( + `Nunjucks *is* async-friendly with \`addFilter("${name}", async function() {})\` but you need to supply an \`async function\`. You returned a promise from \`addFilter("${name}", function() {})\`. Alternatively, use the \`addAsyncFilter("${name}")\` configuration API method.`, + ); + } + return ret; + } + } + else if (isAsync) { + // we need to wrap the async function in a nunjucks compatible callback + /** @this {any} */ + callback = async function (...args) { + let cb = args.pop(); + // Note that `callback` is already a function as the `#add` method throws an error if not. + let ret = await filters[name].call(this, ...args); + cb(null, ret); + } + } + this.njkEnv.addFilter( + name, + Nunjucks.wrapFilter(name, callback), + isAsync, + ); } } @@ -132,7 +161,6 @@ export default class Nunjucks extends TemplateEngine { source: this.ctx, lazy: false, // context.env?.opts.throwOnUndefined, }); - return fn.call(this, ...args); } catch (e) { throw new EleventyNunjucksError( @@ -458,17 +486,15 @@ export default class Nunjucks extends TemplateEngine { } return function (data) { - let { promise, resolve, reject } = withResolvers(); - - tmpl.render(data, (error, result) => { - if (error) { - reject(error); - } else { - resolve(result); - } + return new Promise((resolve, reject) => { + tmpl.render(data, (error, result) => { + if (error) { + reject(error); + } else { + resolve(result); + } + }) }); - - return promise; }; } } diff --git a/src/Engines/TemplateEngine.js b/src/Engines/TemplateEngine.js index 40c2282e4..33e8bc4c0 100644 --- a/src/Engines/TemplateEngine.js +++ b/src/Engines/TemplateEngine.js @@ -85,11 +85,6 @@ export default class TemplateEngine { return this.name; } - // Backwards compat - getIncludesDir() { - return this.includesDir; - } - /** * @protected */ diff --git a/src/TemplateContent.js b/src/TemplateContent.js index 8a87f81ff..fe2b2b967 100644 --- a/src/TemplateContent.js +++ b/src/TemplateContent.js @@ -174,10 +174,6 @@ class TemplateContent { return this.inputPath; } - getInputDir() { - return this.inputDir; - } - isVirtualTemplate() { let def = this.getVirtualTemplateDefinition(); return !!def; diff --git a/src/TemplateLayoutPathResolver.js b/src/TemplateLayoutPathResolver.js index 9f5a8ee67..a4670a4bd 100644 --- a/src/TemplateLayoutPathResolver.js +++ b/src/TemplateLayoutPathResolver.js @@ -1,4 +1,3 @@ -import fs from "node:fs"; import { TemplatePath } from "@11ty/eleventy-utils"; // import debugUtil from "debug"; // const debug = debugUtil("Eleventy:TemplateLayoutPathResolver"); @@ -43,11 +42,6 @@ class TemplateLayoutPathResolver { return this.dirs.layouts || this.dirs.includes; } - /* Backwards compat */ - getLayoutsDir() { - return this.layoutsDir; - } - setAliases() { this.aliases = Object.assign({}, this.config.layoutAliases, this.aliases); } diff --git a/src/TemplateRender.js b/src/TemplateRender.js index 89b82b432..f4bddf28b 100644 --- a/src/TemplateRender.js +++ b/src/TemplateRender.js @@ -44,11 +44,6 @@ class TemplateRender { return this.dirs.includes; } - /* Backwards compat */ - getIncludesDir() { - return this.includesDir; - } - get config() { return this.#config; } diff --git a/src/UserConfig.js b/src/UserConfig.js index 7623640ca..80ea22d9c 100644 --- a/src/UserConfig.js +++ b/src/UserConfig.js @@ -18,6 +18,7 @@ import EleventyBaseError from "./Errors/EleventyBaseError.js"; import BenchmarkManager from "./Benchmark/BenchmarkManager.js"; import JavaScriptFrontMatter from "./Engines/FrontMatter/JavaScript.js"; import { augmentFunction } from "./Engines/Util/ContextAugmenter.js"; +import ConsoleLogger from "./Util/ConsoleLogger.js"; const debug = debugUtil("Eleventy:UserConfig"); @@ -39,6 +40,7 @@ class UserConfig { /** @type {number} */ #concurrency = 1; // Before using os.availableParallelism(); see https://github.com/11ty/eleventy/issues/3596 + #logger; constructor() { // These are completely unnecessary lines to satisfy TypeScript @@ -56,7 +58,6 @@ class UserConfig { /** @type {object} */ this.directories = {}; /** @type {undefined} */ - this.logger; /** @type {string} */ this.dir; /** @type {string} */ @@ -75,6 +76,18 @@ class UserConfig { return this.#uniqueId; } + get logger() { + if (!this.#logger) { + this.#logger = new ConsoleLogger(); + } + return this.#logger; + } + + /* Setter for Logger */ + set logger(logger) { + this.#logger = logger; + } + reset() { debug("Resetting EleventyConfig to initial values."); @@ -137,13 +150,8 @@ class UserConfig { /** @type {object} */ this.javascript = { functions: {}, - filters: {}, - shortcodes: {}, - pairedShortcodes: {}, }; - this.markdownHighlighter = null; - /** @type {object} */ this.libraryOverrides = {}; @@ -287,14 +295,23 @@ class UserConfig { } getFilters(options = {}) { - if (options.type) { - return objectFilter( + let working = this.universal.filters; + if (options.async) { + working = objectFilter( this.universal.filters, - (entry) => entry.__eleventyInternal?.type === options.type, + (entry) => entry.__eleventyInternal?.async === options.async, + ); + } + if (options.lang) { + working = objectFilter( + working, + (entry) => + entry.__eleventyInternal?.langs?.includes(options.lang) || + entry.__eleventyInternal?.langs === undefined, ); } - return this.universal.filters; + return working; } getShortcode(name) { @@ -349,116 +366,126 @@ class UserConfig { debug(`Adding new ${description} "%o" via \`%o(%o)\``, name, functionName, originalName); } - target[name] = this.#decorateCallback(`"${name}" ${description}`, callback); + target[name] = this.#decorateCallback( + `"${name}" ${description}`, + callback, + options, + ); } - #decorateCallback(type, callback) { - return this.benchmarks.config.add(type, callback); + #decorateCallback(type, callback, misc) { + return this.benchmarks.config.add(type, callback, misc); } /* * Markdown */ - // This is a method for plugins, probably shouldn’t use this in projects. - // Projects should use `setLibrary` as documented here: - // https://github.com/11ty/eleventy/blob/master/docs/engines/markdown.md#use-your-own-options + + /** + * @deprecated Use {@link amendLibrary} with mdlib.set instead. + */ addMarkdownHighlighter(highlightFn) { - this.markdownHighlighter = highlightFn; + this.logger.warn( + "`addMarkdownHighlighter` is deprecated and will be removed in a future release. Use `amendLibrary` with `mdlib.set` instead.", + ); + this.amendLibrary("md", (mdlib) => { + mdlib.set({ + highlight: highlightFn, + }); + }) } /* * Filters */ + /** + * @deprecated Use {@link addFilter} instead, with options.langs set to ["liquid"] if you wish it to be scoped. + */ addLiquidFilter(name, callback) { - this.#add(this.liquid.filters, name, callback, { - description: "Liquid Filter", - functionName: "addLiquidFilter", - }); + this.logger.warn( + '`addLiquidFilter` is deprecated and will be removed in a future release.\ + Use `addFilter` instead, with options.lang set to ["liquid"] if you wish it to be scoped.', + ); + this.addFilter( + name, + callback, + { + langs: ["liquid"], + }, + ); } + /** + * @deprecated Use {@link addFilter} instead, with options.langs set to ["njk"] if you wish it to be scoped. + */ addNunjucksAsyncFilter(name, callback) { - this.#add(this.nunjucks.asyncFilters, name, callback, { - description: "Nunjucks Filter", - functionName: "addNunjucksAsyncFilter", + this.logger.warn( + '`addNunjucksAsyncFilter` is deprecated and will be removed in a future release.\ + Pass an async function to `addFilter` instead, with options.lang set to ["njk"] if you wish it to be scoped.', + ); + this.addFilter(name, callback, { + langs: ["njk"], + async: true, }); } // Support the nunjucks style syntax for asynchronous filter add + /** + * @deprecated Use {@link addFilter} instead, with options.langs set to ["njk"] if you wish it to be scoped. + */ addNunjucksFilter(name, callback, isAsync = false) { if (isAsync) { - // namespacing happens downstream this.addNunjucksAsyncFilter(name, callback); - } else { - this.#add(this.nunjucks.filters, name, callback, { - description: "Nunjucks Filter", - functionName: "addNunjucksFilter", - }); + return; } + this.logger.warn( + '`addNunjucksFilter` is deprecated and will be removed in a future release.\ + Use `addFilter` instead, with options.langs set to ["njk"] if you wish it to be scoped.', + ); + this.addFilter( + name, + callback, + { + langs: ["njk"], + }, + ); } - addJavaScriptFilter(name, callback) { - this.#add(this.javascript.filters, name, callback, { - description: "JavaScript Filter", - functionName: "addJavaScriptFilter", - }); - - // Backwards compat for a time before `addJavaScriptFilter` existed. - this.addJavaScriptFunction(name, callback); - } - - addFilter(name, callback) { - // This method *requires* `async function` and will not work with `function` that returns a promise - if (isAsyncFunction(callback)) { - this.addAsyncFilter(name, callback); - return; - } + /** + * + * @param {*} name - The name of the filter + * @param {*} callback - The filter function + * @param {{ + * langs?: string[], + * async?: boolean + * }} [options] - Options for the filter + * If `async` is undefined, it will be inferred, possibly incorrectly. + * @returns + */ + addFilter(name, callback, options) { + const async = options?.async || isAsyncFunction(callback); // namespacing happens downstream this.#add(this.universal.filters, name, callback, { description: "Universal Filter", functionName: "addFilter", + langs: options?.langs, + async, }); - - this.addLiquidFilter(name, callback); - this.addJavaScriptFilter(name, callback); - this.addNunjucksFilter( - name, - /** @this {any} */ - function (...args) { - // Note that `callback` is already a function as the `#add` method throws an error if not. - let ret = callback.call(this, ...args); - if (ret instanceof Promise) { - throw new Error( - `Nunjucks *is* async-friendly with \`addFilter("${name}", async function() {})\` but you need to supply an \`async function\`. You returned a promise from \`addFilter("${name}", function() {})\`. Alternatively, use the \`addAsyncFilter("${name}")\` configuration API method.`, - ); - } - return ret; - }, - ); } - // Liquid, Nunjucks, and JS only + /** + * Perhaps this should be deprecated. + * addAsyncFilter("...", function() {return Promise}) and + * addFilter("...", function() {return Promise}, {async: true}) + * are the same. + */ addAsyncFilter(name, callback) { - // namespacing happens downstream - this.#add(this.universal.filters, name, callback, { - description: "Universal Filter", - functionName: "addAsyncFilter", + this.addFilter(name, callback, { + async: true, }); - - this.addLiquidFilter(name, callback); - this.addJavaScriptFilter(name, callback); - this.addNunjucksAsyncFilter( - name, - /** @this {any} */ - async function (...args) { - let cb = args.pop(); - // Note that `callback` is already a function as the `#add` method throws an error if not. - let ret = await callback.call(this, ...args); - cb(null, ret); - }, - ); } /* @@ -478,7 +505,6 @@ class UserConfig { }); this.addLiquidShortcode(name, callback); - this.addJavaScriptShortcode(name, callback); this.addNunjucksShortcode(name, callback); } @@ -491,7 +517,6 @@ class UserConfig { // Related: #498 this.addNunjucksAsyncShortcode(name, callback); this.addLiquidShortcode(name, callback); - this.addJavaScriptShortcode(name, callback); } addNunjucksAsyncShortcode(name, callback) { @@ -533,7 +558,6 @@ class UserConfig { this.addPairedNunjucksShortcode(name, callback); this.addPairedLiquidShortcode(name, callback); - this.addPairedJavaScriptShortcode(name, callback); } // Related: #498 @@ -545,7 +569,6 @@ class UserConfig { this.addPairedNunjucksAsyncShortcode(name, callback); this.addPairedLiquidShortcode(name, callback); - this.addPairedJavaScriptShortcode(name, callback); } addPairedNunjucksAsyncShortcode(name, callback) { @@ -573,31 +596,16 @@ class UserConfig { }); } - addJavaScriptShortcode(name, callback) { - this.#add(this.javascript.shortcodes, name, callback, { - description: "JavaScript Shortcode", - functionName: "addJavaScriptShortcode", - }); - - // Backwards compat for a time before `addJavaScriptShortcode` existed. - this.addJavaScriptFunction(name, callback); - } - - addPairedJavaScriptShortcode(name, callback) { - this.#add(this.javascript.pairedShortcodes, name, callback, { - description: "JavaScript Paired Shortcode", - functionName: "addPairedJavaScriptShortcode", - }); - - // Backwards compat for a time before `addJavaScriptShortcode` existed. - this.addJavaScriptFunction(name, callback); - } - - // Both Filters and shortcodes feed into this + /** + * @deprecated Use {@link addFilter} with options.langs set to ["11ty.js"] if you wish it to be scoped. + */ addJavaScriptFunction(name, callback) { - this.#add(this.javascript.functions, name, callback, { - description: "JavaScript Function", - functionName: "addJavaScriptFunction", + this.logger.warn( + '`addJavaScriptFunction` is deprecated and will be removed in a future release.\ + Use `addFilter` instead, with options.lang set to ["11ty.js"] if you wish it to be scoped.', + ); + this.addFilter(name, callback, { + langs: ["11ty.js"], }); } @@ -1253,11 +1261,34 @@ class UserConfig { layoutResolution: this.layoutResolution, passthroughCopiesHtmlRelative: this.passthroughCopiesHtmlRelative, passthroughCopies: this.passthroughCopies, + filters: this.getFilters(), + + // Unfortunately this must still exist because nunjucks and liquid are not yet plugins + // This will happen in my next pr + __theCodeCriesInPain: { + nunjucks: { + asyncFilters: this.getFilters({ + lang: "njk", + async: true, + }), + filters: this.getFilters({ + lang: "njk", + async: false, + }), + }, + liquid: { + filters: this.getFilters({ + lang: "liquid", + }), + }, + javascript: this.getFilters({ + lang: "11ty.js", + }), + }, // Liquid liquidOptions: this.liquid.options, liquidTags: this.liquid.tags, - liquidFilters: this.liquid.filters, liquidShortcodes: this.liquid.shortcodes, liquidPairedShortcodes: this.liquid.pairedShortcodes, liquidParameterParsing: this.liquid.parameterParsing, @@ -1265,8 +1296,6 @@ class UserConfig { // Nunjucks nunjucksEnvironmentOptions: this.nunjucks.environmentOptions, nunjucksPrecompiledTemplates: this.nunjucks.precompiledTemplates, - nunjucksFilters: this.nunjucks.filters, - nunjucksAsyncFilters: this.nunjucks.asyncFilters, nunjucksTags: this.nunjucks.tags, nunjucksGlobals: this.nunjucks.globals, nunjucksAsyncShortcodes: this.nunjucks.asyncShortcodes, @@ -1276,12 +1305,6 @@ class UserConfig { // 11ty.js javascriptFunctions: this.javascript.functions, // filters and shortcodes, combined - javascriptShortcodes: this.javascript.shortcodes, - javascriptPairedShortcodes: this.javascript.pairedShortcodes, - javascriptFilters: this.javascript.filters, - - // Markdown - markdownHighlighter: this.markdownHighlighter, libraryOverrides: this.libraryOverrides, dynamicPermalinks: this.dynamicPermalinks, diff --git a/src/defaultConfig.js b/src/defaultConfig.js index fb1ab5dd2..cf59b35f2 100644 --- a/src/defaultConfig.js +++ b/src/defaultConfig.js @@ -46,8 +46,6 @@ import { HtmlRelativeCopyPlugin } from "./Plugins/HtmlRelativeCopyPlugin.js"; * @property {string} [dir.includes='_includes'] * @property {string} [dir.data='_data'] * @property {string} [dir.output='_site'] - * @deprecated handlebarsHelpers - * @deprecated nunjucksFilters */ /** @@ -171,8 +169,5 @@ export default function (config) { data: "_data", output: "_site", }, - - // deprecated, use config.addNunjucksFilter - nunjucksFilters: {}, }; } diff --git a/test/EleventyFilesTest.js b/test/EleventyFilesTest.js index 4b377bd62..656c5d2c7 100644 --- a/test/EleventyFilesTest.js +++ b/test/EleventyFilesTest.js @@ -23,7 +23,7 @@ test("Dirs paths", async (t) => { t.deepEqual(evf.inputDir, "./src/"); t.deepEqual(evf.includesDir, "./src/includes/"); - t.deepEqual(evf.getDataDir(), "./src/data/"); + t.deepEqual(evf.dataDir, "./src/data/"); t.deepEqual(evf.outputDir, "./dist/"); }); @@ -41,7 +41,7 @@ test("Dirs paths (relative)", async (t) => { t.deepEqual(evf.inputDir, "./src/"); t.deepEqual(evf.includesDir, "./includes/"); - t.deepEqual(evf.getDataDir(), "./data/"); + t.deepEqual(evf.dataDir, "./data/"); t.deepEqual(evf.outputDir, "./dist/"); }); @@ -260,7 +260,7 @@ test("getDataDir", async (t) => { let { eleventyFiles: evf } = getEleventyFilesInstance([], eleventyConfig); evf.init(); - t.is(evf.getDataDir(), "./_data/"); + t.is(evf.dataDir, "./_data/"); }); test("getDataDir subdir", async (t) => { @@ -272,7 +272,7 @@ test("getDataDir subdir", async (t) => { let { eleventyFiles: evf } = getEleventyFilesInstance([], eleventyConfig); evf.init(); - t.is(evf.getDataDir(), "./test/stubs/_data/"); + t.is(evf.dataDir, "./test/stubs/_data/"); }); test("Include and Data Dirs", async (t) => { diff --git a/test/TemplateConfigTest.js b/test/TemplateConfigTest.js index 26a0e9afc..a023b13ea 100644 --- a/test/TemplateConfigTest.js +++ b/test/TemplateConfigTest.js @@ -92,8 +92,7 @@ test("Add universal filter", async (t) => { await templateCfg.init(); let cfg = templateCfg.getConfig(); - t.not(Object.keys(cfg.liquidFilters).indexOf("myFilterName"), -1); - t.not(Object.keys(cfg.nunjucksFilters).indexOf("myFilterName"), -1); + t.not(Object.keys(cfg.filters).indexOf("myFilterName"), -1); }); test("Add namespaced universal filter", async (t) => { @@ -149,20 +148,19 @@ test("Add namespaced plugin using underscore", async (t) => { await templateCfg.init(); let cfg = templateCfg.getConfig(); - t.not(Object.keys(cfg.liquidFilters).indexOf("testNamespace_myFilterName"), -1); - t.not(Object.keys(cfg.nunjucksFilters).indexOf("testNamespace_myFilterName"), -1); + t.not(Object.keys(cfg.filters).indexOf("testNamespace_myFilterName"), -1); }); test("Empty namespace", async (t) => { let templateCfg = new TemplateConfig(defaultConfig, "./test/stubs/config.cjs"); templateCfg.userConfig.namespace("", function () { - templateCfg.userConfig.addNunjucksFilter("myFilterName", function () {}); + templateCfg.userConfig.addFilter("myFilterName", function () {}); }); await templateCfg.init(); let cfg = templateCfg.getConfig(); - t.not(Object.keys(cfg.nunjucksFilters).indexOf("myFilterName"), -1); + t.not(Object.keys(cfg.filters).indexOf("myFilterName"), -1); }); test("Nested Empty Inner namespace", async (t) => { @@ -563,7 +561,6 @@ test("Add async plugin", async (t) => { let cfg = templateCfg.getConfig(); t.not(Object.keys(cfg.liquidFilters).indexOf("myFilterName"), -1); - t.not(Object.keys(cfg.nunjucksFilters).indexOf("myFilterName"), -1); }); test("Async namespace", async (t) => { diff --git a/test/TemplateRenderJavaScriptTest.js b/test/TemplateRenderJavaScriptTest.js index ef15fe611..d88b3d7ef 100644 --- a/test/TemplateRenderJavaScriptTest.js +++ b/test/TemplateRenderJavaScriptTest.js @@ -158,14 +158,16 @@ test("JS Render with a function", async (t) => { t.plan(8); let tr = await getNewTemplateRender("./test/stubs/function-filter.11ty.cjs", undefined, { - javascriptFunctions: { - upper: function (val) { - t.is(this.page.url, "/hi/"); - // sanity check to make sure data didn’t propagate - t.not(this.name, "Zach"); - t.not(this.name, "Bill"); - return new String(val).toUpperCase(); - }, + __theCodeCriesInPain: { + javascript: { + upper: function (val) { + t.is(this.page.url, "/hi/"); + // sanity check to make sure data didn’t propagate + t.not(this.name, "Zach"); + t.not(this.name, "Bill"); + return new String(val).toUpperCase(); + }, + } }, }); @@ -178,13 +180,15 @@ test("JS Render with a function and async filter", async (t) => { t.plan(4); let tr = await getNewTemplateRender("./test/stubs/function-async-filter.11ty.cjs", undefined, { - javascriptFunctions: { - upper: function (val) { - return new Promise((resolve) => { - t.is(this.page.url, "/hi/"); - resolve(new String(val).toUpperCase()); - }); - }, + __theCodeCriesInPain: { + javascript: { + upper: function (val) { + return new Promise((resolve) => { + t.is(this.page.url, "/hi/"); + resolve(new String(val).toUpperCase()); + }); + }, + } }, }); @@ -196,12 +200,14 @@ test("JS Render with a function and async filter", async (t) => { test("JS Render with a function prototype", async (t) => { t.plan(4); let tr = await getNewTemplateRender("./test/stubs/function-prototype.11ty.cjs", undefined, { - javascriptFunctions: { - upper: function (val) { - t.is(this.page.url, "/hi/"); - return new String(val).toUpperCase(); + __theCodeCriesInPain: { + javascript: { + upper: function (val) { + t.is(this.page.url, "/hi/"); + return new String(val).toUpperCase(); + }, }, - }, + } }); let fn = await tr.getCompiledTemplate(); @@ -213,11 +219,13 @@ test("JS Class Render with a function", async (t) => { t.plan(4); let tr = await getNewTemplateRender("./test/stubs/class-filter.11ty.cjs", undefined, { - javascriptFunctions: { - upper: function (val) { - t.is(this.page.url, "/hi/"); - return new String(val).toUpperCase(); - }, + __theCodeCriesInPain: { + javascript: { + upper: function (val) { + t.is(this.page.url, "/hi/"); + return new String(val).toUpperCase(); + }, + } }, }); @@ -230,11 +238,13 @@ test("JS Class Async Render with a function", async (t) => { t.plan(4); let tr = await getNewTemplateRender("./test/stubs/class-async-filter.11ty.cjs", undefined, { - javascriptFunctions: { - upper: function (val) { - t.is(this.page.url, "/hi/"); - return new String(val).toUpperCase(); - }, + __theCodeCriesInPain: { + javascript: { + upper: function (val) { + t.is(this.page.url, "/hi/"); + return new String(val).toUpperCase(); + }, + } }, }); @@ -246,10 +256,12 @@ test("JS Class Async Render with a function", async (t) => { test("JS Class Async Render with a function (sync function, throws error)", async (t) => { let tr = await getNewTemplateRender("./test/stubs/function-throws.11ty.cjs", undefined, { - javascriptFunctions: { - upper: function (val) { - throw new Error("JS Class Async Render with a function (sync function, throws error)"); - }, + __theCodeCriesInPain: { + javascript: { + upper: function (val) { + throw new Error("JS Class Async Render with a function (sync function, throws error)"); + }, + } }, }); @@ -265,10 +277,12 @@ test("JS Class Async Render with a function (sync function, throws error)", asyn test("JS Class Async Render with a function (async function, throws error)", async (t) => { let tr = await getNewTemplateRender("./test/stubs/function-throws-async.11ty.cjs", undefined, { - javascriptFunctions: { - upper: async function (val) { - throw new Error("JS Class Async Render with a function (async function, throws error)"); - }, + __theCodeCriesInPain: { + javascript: { + upper: async function (val) { + throw new Error("JS Class Async Render with a function (async function, throws error)"); + }, + } }, }); diff --git a/test/TemplateRenderMarkdownTest.js b/test/TemplateRenderMarkdownTest.js index 2d6454678..5a36cdd5a 100644 --- a/test/TemplateRenderMarkdownTest.js +++ b/test/TemplateRenderMarkdownTest.js @@ -151,14 +151,7 @@ test("Markdown Render: use prism highlighter (no language)", async (t) => { } ); - let tr = await getNewTemplateRender("md", null, eleventyConfig); - - let markdownHighlight = eleventyConfig.getConfig().markdownHighlighter; - let mdLib = md(); - mdLib.set({ - highlight: markdownHighlight, - }); - tr.engine.setLibrary(mdLib); + let tr = await getNewTemplateRender("md",undefined,eleventyConfig); let fn = await tr.getCompiledTemplate(`\`\`\` This is some code. @@ -178,15 +171,7 @@ test("Markdown Render: use prism highlighter", async (t) => { } ); - let tr = await getNewTemplateRender("md"); - - let markdownHighlight = eleventyConfig.getConfig().markdownHighlighter; - - let mdLib = md(); - mdLib.set({ - highlight: markdownHighlight, - }); - tr.engine.setLibrary(mdLib); + let tr = await getNewTemplateRender("md",undefined,eleventyConfig); let fn = await tr.getCompiledTemplate(`\`\`\` js var key = "value"; @@ -207,14 +192,6 @@ test("Markdown Render: use prism highlighter (no space before language)", async let tr = await getNewTemplateRender("md", null, eleventyConfig); - let markdownHighlight = eleventyConfig.getConfig().markdownHighlighter; - - let mdLib = md(); - mdLib.set({ - highlight: markdownHighlight, - }); - tr.engine.setLibrary(mdLib); - let fn = await tr.getCompiledTemplate(`\`\`\`js var key = "value"; \`\`\``); @@ -233,13 +210,6 @@ test("Markdown Render: use prism highlighter, line highlighting", async (t) => { ); let tr = await getNewTemplateRender("md", null, eleventyConfig); - let markdownHighlight = eleventyConfig.getConfig().markdownHighlighter; - - let mdLib = md(); - mdLib.set({ - highlight: markdownHighlight, - }); - tr.engine.setLibrary(mdLib); let fn = await tr.getCompiledTemplate(`\`\`\`js/0 var key = "value"; @@ -260,15 +230,6 @@ test("Markdown Render: use prism highlighter, line highlighting with fallback `t let tr = await getNewTemplateRender("md", null, eleventyConfig); - let cfg = eleventyConfig.getConfig(); - let markdownHighlight = cfg.markdownHighlighter; - - let mdLib = md(); - mdLib.set({ - highlight: markdownHighlight, - }); - tr.engine.setLibrary(mdLib); - let fn = await tr.getCompiledTemplate(`\`\`\` text/0 var key = "value"; \`\`\``); diff --git a/test/TemplateRenderTest.js b/test/TemplateRenderTest.js index 403c583a3..54f9df6c9 100644 --- a/test/TemplateRenderTest.js +++ b/test/TemplateRenderTest.js @@ -30,7 +30,7 @@ test("Basic", async (t) => { test("Includes Dir", async (t) => { let tr = await getNewTemplateRender("liquid", "./test/stubs"); - t.is(tr.getIncludesDir(), "./test/stubs/_includes/"); + t.is(tr.includesDir, "./test/stubs/_includes/"); }); test("Invalid override", async (t) => { diff --git a/test/stubs/function-filter.11ty.cjs b/test/stubs/function-filter.11ty.cjs index e9db5bf42..df4cd96e7 100644 --- a/test/stubs/function-filter.11ty.cjs +++ b/test/stubs/function-filter.11ty.cjs @@ -1,4 +1,5 @@ function myFunction({ name }) { + console.log("myFunction called",this); return `
${this.upper(name)}${myFunction.staticMethod()}
`; }