2017-07-19 18:56:51 +00:00
|
|
|
// @flow
|
2017-02-20 18:32:27 +00:00
|
|
|
import nunjucks from 'nunjucks';
|
2017-06-01 02:04:27 +00:00
|
|
|
import BaseExtension from './base-extension';
|
2018-06-25 17:42:50 +00:00
|
|
|
import type { NunjucksParsedTag } from './utils';
|
2017-11-26 20:45:40 +00:00
|
|
|
import * as plugins from '../plugins/index';
|
2021-02-25 21:10:21 +00:00
|
|
|
import type { TemplateTag } from '../plugins/index';
|
2017-02-20 18:32:27 +00:00
|
|
|
|
2017-07-19 18:56:51 +00:00
|
|
|
export class RenderError extends Error {
|
|
|
|
message: string;
|
|
|
|
path: string | null;
|
2018-06-25 17:42:50 +00:00
|
|
|
location: { line: number, column: number };
|
2017-07-19 18:56:51 +00:00
|
|
|
type: string;
|
|
|
|
reason: string;
|
|
|
|
}
|
|
|
|
|
2017-07-11 01:05:54 +00:00
|
|
|
// Some constants
|
|
|
|
export const RENDER_ALL = 'all';
|
|
|
|
export const RENDER_VARS = 'variables';
|
|
|
|
export const RENDER_TAGS = 'tags';
|
2020-10-01 18:49:05 +00:00
|
|
|
export const NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME = '_';
|
2017-07-11 01:05:54 +00:00
|
|
|
|
2017-05-15 20:55:05 +00:00
|
|
|
// Cached globals
|
|
|
|
let nunjucksVariablesOnly = null;
|
2017-07-11 01:05:54 +00:00
|
|
|
let nunjucksTagsOnly = null;
|
|
|
|
let nunjucksAll = null;
|
2017-05-15 20:55:05 +00:00
|
|
|
|
2017-02-20 18:32:27 +00:00
|
|
|
/**
|
|
|
|
* Render text based on stuff
|
|
|
|
* @param {String} text - Nunjucks template in text form
|
|
|
|
* @param {Object} [config] - Config options for rendering
|
|
|
|
* @param {Object} [config.context] - Context to render with
|
2017-03-28 22:45:23 +00:00
|
|
|
* @param {Object} [config.path] - Path to include in the error message
|
2017-07-11 01:05:54 +00:00
|
|
|
* @param {Object} [config.renderMode] - Only render variables (not tags)
|
2017-02-20 18:32:27 +00:00
|
|
|
*/
|
2018-07-17 23:34:28 +00:00
|
|
|
export function render(
|
|
|
|
text: string,
|
2018-12-12 17:36:11 +00:00
|
|
|
config: { context?: Object, path?: string, renderMode?: string } = {},
|
2018-07-17 23:34:28 +00:00
|
|
|
): Promise<string> {
|
2017-02-20 18:32:27 +00:00
|
|
|
const context = config.context || {};
|
2020-10-01 18:49:05 +00:00
|
|
|
// context needs to exist on the root for the old templating syntax, and in _ for the new templating syntax
|
|
|
|
// old: {{ arr[0].prop }}
|
|
|
|
// new: {{ _['arr-name-with-dash'][0].prop }}
|
|
|
|
const templatingContext = { ...context, [NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME]: context };
|
2017-03-28 22:45:23 +00:00
|
|
|
const path = config.path || null;
|
2017-07-11 01:05:54 +00:00
|
|
|
const renderMode = config.renderMode || RENDER_ALL;
|
2017-02-20 18:32:27 +00:00
|
|
|
|
2017-07-20 01:55:40 +00:00
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
const nj = await getNunjucks(renderMode);
|
2020-10-01 18:49:05 +00:00
|
|
|
nj.renderString(text, templatingContext, (err, result) => {
|
2017-02-20 18:32:27 +00:00
|
|
|
if (err) {
|
2017-02-27 21:00:13 +00:00
|
|
|
const sanitizedMsg = err.message
|
2017-03-31 21:59:12 +00:00
|
|
|
.replace(/\(unknown path\)\s/, '')
|
2017-03-28 22:45:23 +00:00
|
|
|
.replace(/\[Line \d+, Column \d*]/, '')
|
|
|
|
.replace(/^\s*Error:\s*/, '')
|
|
|
|
.trim();
|
2017-02-27 21:00:13 +00:00
|
|
|
|
2017-03-28 22:45:23 +00:00
|
|
|
const location = err.message.match(/\[Line (\d)+, Column (\d)*]/);
|
|
|
|
const line = location ? parseInt(location[1]) : 1;
|
|
|
|
const column = location ? parseInt(location[2]) : 1;
|
2018-10-17 16:42:33 +00:00
|
|
|
const reason = err.message.includes('attempted to output null or undefined value')
|
2017-03-28 22:45:23 +00:00
|
|
|
? 'undefined'
|
|
|
|
: 'error';
|
|
|
|
|
2017-07-19 18:56:51 +00:00
|
|
|
const newError = new RenderError(sanitizedMsg);
|
2017-12-21 14:01:51 +00:00
|
|
|
newError.path = path || '';
|
2017-03-28 22:45:23 +00:00
|
|
|
newError.message = sanitizedMsg;
|
2018-06-25 17:42:50 +00:00
|
|
|
newError.location = { line, column };
|
2017-03-28 22:45:23 +00:00
|
|
|
newError.type = 'render';
|
|
|
|
newError.reason = reason;
|
|
|
|
reject(newError);
|
2017-02-20 18:32:27 +00:00
|
|
|
} else {
|
|
|
|
resolve(result);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-01 02:04:27 +00:00
|
|
|
/**
|
|
|
|
* Reload Nunjucks environments. Useful for if plugins change.
|
|
|
|
*/
|
2018-06-25 17:42:50 +00:00
|
|
|
export function reload(): void {
|
2017-07-11 01:05:54 +00:00
|
|
|
nunjucksAll = null;
|
2017-06-01 02:04:27 +00:00
|
|
|
nunjucksVariablesOnly = null;
|
2017-07-11 01:05:54 +00:00
|
|
|
nunjucksTagsOnly = null;
|
2017-06-01 02:04:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get definitions of template tags
|
|
|
|
*/
|
2018-06-25 17:42:50 +00:00
|
|
|
export async function getTagDefinitions(): Promise<Array<NunjucksParsedTag>> {
|
2017-07-20 01:55:40 +00:00
|
|
|
const env = await getNunjucks(RENDER_ALL);
|
2017-05-23 22:05:31 +00:00
|
|
|
|
|
|
|
return Object.keys(env.extensions)
|
|
|
|
.map(k => env.extensions[k])
|
2017-06-01 02:04:27 +00:00
|
|
|
.filter(ext => !ext.isDeprecated())
|
2018-06-25 17:42:50 +00:00
|
|
|
.sort((a, b) => (a.getPriority() > b.getPriority() ? 1 : -1))
|
2017-05-23 22:05:31 +00:00
|
|
|
.map(ext => ({
|
|
|
|
name: ext.getTag(),
|
|
|
|
displayName: ext.getName(),
|
2020-03-11 22:15:15 +00:00
|
|
|
liveDisplayName: ext.getLiveDisplayName(),
|
2017-05-23 22:05:31 +00:00
|
|
|
description: ext.getDescription(),
|
2019-04-18 21:01:15 +00:00
|
|
|
disablePreview: ext.getDisablePreview(),
|
2018-12-12 17:36:11 +00:00
|
|
|
args: ext.getArgs(),
|
2020-11-30 23:15:17 +00:00
|
|
|
actions: ext.getActions(),
|
2017-06-01 02:04:27 +00:00
|
|
|
}));
|
2017-05-23 22:05:31 +00:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
async function getNunjucks(renderMode: string) {
|
2017-07-11 01:05:54 +00:00
|
|
|
if (renderMode === RENDER_VARS && nunjucksVariablesOnly) {
|
2017-05-15 20:55:05 +00:00
|
|
|
return nunjucksVariablesOnly;
|
|
|
|
}
|
2017-02-27 21:00:13 +00:00
|
|
|
|
2017-07-11 01:05:54 +00:00
|
|
|
if (renderMode === RENDER_TAGS && nunjucksTagsOnly) {
|
|
|
|
return nunjucksTagsOnly;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (renderMode === RENDER_ALL && nunjucksAll) {
|
|
|
|
return nunjucksAll;
|
2017-05-15 20:55:05 +00:00
|
|
|
}
|
2017-02-20 18:32:27 +00:00
|
|
|
|
2017-05-15 20:55:05 +00:00
|
|
|
// ~~~~~~~~~~~~ //
|
|
|
|
// Setup Config //
|
|
|
|
// ~~~~~~~~~~~~ //
|
2017-02-20 18:32:27 +00:00
|
|
|
|
2017-05-15 20:55:05 +00:00
|
|
|
const config = {
|
|
|
|
autoescape: false, // Don't escape HTML
|
|
|
|
throwOnUndefined: true, // Strict mode
|
|
|
|
tags: {
|
|
|
|
blockStart: '{%',
|
|
|
|
blockEnd: '%}',
|
|
|
|
variableStart: '{{',
|
|
|
|
variableEnd: '}}',
|
|
|
|
commentStart: '{#',
|
2018-12-12 17:36:11 +00:00
|
|
|
commentEnd: '#}',
|
|
|
|
},
|
2017-05-15 20:55:05 +00:00
|
|
|
};
|
|
|
|
|
2017-07-11 01:05:54 +00:00
|
|
|
if (renderMode === RENDER_VARS) {
|
2017-05-15 20:55:05 +00:00
|
|
|
// Set tag syntax to something that will never happen naturally
|
|
|
|
config.tags.blockStart = '<[{[{[{[{[$%';
|
|
|
|
config.tags.blockEnd = '%$]}]}]}]}]>';
|
2017-02-20 18:32:27 +00:00
|
|
|
}
|
|
|
|
|
2017-07-11 01:05:54 +00:00
|
|
|
if (renderMode === RENDER_TAGS) {
|
|
|
|
// Set tag syntax to something that will never happen naturally
|
|
|
|
config.tags.variableStart = '<[{[{[{[{[$%';
|
|
|
|
config.tags.variableEnd = '%$]}]}]}]}]>';
|
|
|
|
}
|
|
|
|
|
2017-05-15 20:55:05 +00:00
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~ //
|
|
|
|
// Create Env with Extensions //
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~ //
|
2017-02-27 21:00:13 +00:00
|
|
|
|
2017-05-15 20:55:05 +00:00
|
|
|
const nj = nunjucks.configure(config);
|
2017-02-27 21:00:13 +00:00
|
|
|
|
2021-02-25 21:10:21 +00:00
|
|
|
let allTemplateTagPlugins: Array<TemplateTag>;
|
|
|
|
try {
|
|
|
|
plugins.ignorePlugin('insomnia-plugin-kong-bundle');
|
|
|
|
allTemplateTagPlugins = await plugins.getTemplateTags();
|
|
|
|
} finally {
|
|
|
|
plugins.clearIgnores();
|
|
|
|
}
|
|
|
|
|
2018-06-09 03:22:39 +00:00
|
|
|
const allExtensions = allTemplateTagPlugins;
|
2017-06-01 02:04:27 +00:00
|
|
|
for (let i = 0; i < allExtensions.length; i++) {
|
2018-06-25 17:42:50 +00:00
|
|
|
const { templateTag, plugin } = allExtensions[i];
|
2018-06-09 03:22:39 +00:00
|
|
|
templateTag.priority = templateTag.priority || i * 100;
|
|
|
|
const instance = new BaseExtension(templateTag, plugin);
|
2017-06-09 01:10:12 +00:00
|
|
|
nj.addExtension(instance.getTag(), instance);
|
2017-07-11 01:05:54 +00:00
|
|
|
|
|
|
|
// Hidden helper filter to debug complicated things
|
|
|
|
// eg. `{{ foo | urlencode | debug | upper }}`
|
2020-10-01 18:49:05 +00:00
|
|
|
nj.addFilter('debug', o => o);
|
2017-05-15 20:55:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~ //
|
|
|
|
// Cache Env and Return //
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~ //
|
|
|
|
|
2017-07-11 01:05:54 +00:00
|
|
|
if (renderMode === RENDER_VARS) {
|
2017-05-15 20:55:05 +00:00
|
|
|
nunjucksVariablesOnly = nj;
|
2017-07-11 01:05:54 +00:00
|
|
|
} else if (renderMode === RENDER_TAGS) {
|
|
|
|
nunjucksTagsOnly = nj;
|
2017-05-15 20:55:05 +00:00
|
|
|
} else {
|
2017-07-11 01:05:54 +00:00
|
|
|
nunjucksAll = nj;
|
2017-02-27 21:00:13 +00:00
|
|
|
}
|
|
|
|
|
2017-05-15 20:55:05 +00:00
|
|
|
return nj;
|
2017-02-27 21:00:13 +00:00
|
|
|
}
|