Skip to content

Commit

Permalink
fix(view-nunjucks): add autoescape config && add ts interface (#2148)
Browse files Browse the repository at this point in the history
  • Loading branch information
wakeGISer committed Jul 23, 2022
1 parent d5db880 commit 8d39739
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 41 deletions.
36 changes: 3 additions & 33 deletions packages/view-nunjucks/src/config/config.default.ts
@@ -1,3 +1,5 @@
import { INunjucksConfig } from '../interface';

export default {
nunjucks: {
autoescape: true,
Expand All @@ -7,37 +9,5 @@ export default {
cache: true,
},
} as {
nunjucks: {
/**
* controls if output with dangerous characters are escaped automatically.
*/
autoescape: boolean;
/**
* throw errors when outputting a null/undefined value
*/
throwOnUndefined: boolean;
/**
* automatically remove trailing newlines from a block/tag
*/
trimBlocks: boolean;
/**
* automatically remove leading whitespace from a block/tag
*/
lstripBlocks: boolean;
/**
* use a cache and recompile templates each time. false in local env.
*/
cache: boolean;
/**
* defines the syntax for nunjucks tags.
*/
tags?: {
blockStart?: string;
blockEnd?: string;
variableStart?: string;
variableEnd?: string;
commentStart?: string;
commentEnd?: string;
};
};
nunjucks: INunjucksConfig;
};
42 changes: 34 additions & 8 deletions packages/view-nunjucks/src/engine.ts
Expand Up @@ -7,15 +7,26 @@ import {
Scope,
ScopeEnum,
} from '@midwayjs/decorator';
import { Environment, FileSystemLoader, runtime } from 'nunjucks';
import {
ConfigureOptions,
Environment,
FileSystemLoader,
ILoader,
runtime,
TemplateCallback,
} from 'nunjucks';
import { RenderOptions } from '@midwayjs/view';

class MidwayNunjucksEnvironment extends Environment {
constructor(fileLoader, config) {
constructor(
fileLoader?: ILoader | ILoader[] | null,
config?: ConfigureOptions
) {
super(fileLoader, config);

// http://disse.cting.org/2016/08/02/2016-08-02-sandbox-break-out-nunjucks-template-engine
const originMemberLookup = runtime.memberLookup;
runtime.memberLookup = function (...args) {
runtime.memberLookup = function (...args: unknown[]) {
const val = args[1];
if (val === 'prototype' || val === 'constructor') return null;
return originMemberLookup(...args);
Expand All @@ -26,7 +37,7 @@ class MidwayNunjucksEnvironment extends Environment {
@Provide()
@Scope(ScopeEnum.Singleton)
export class NunjucksEnvironment {
protected nunjucksEnvironment;
protected nunjucksEnvironment: Environment;

@App()
protected app;
Expand All @@ -53,6 +64,7 @@ export class NunjucksEnvironment {
trimBlocks: config.trimBlocks,
lstripBlocks: config.lstripBlocks,
tags: config.tags,
autoescape: config.autoescape,
};

const fileLoader = new FileSystemLoader(this.globalConfig.view.root, {
Expand All @@ -64,12 +76,26 @@ export class NunjucksEnvironment {
);
}

render(name, locals, cb) {
return this.nunjucksEnvironment.render(name, locals, cb);
render(
name: string,
context?: Record<string, any>,
callback?: TemplateCallback<string>
) {
return this.nunjucksEnvironment.render(name, context, callback);
}

renderString(tpl, locals, opts, cb) {
return this.nunjucksEnvironment.renderString(tpl, locals, opts, cb);
renderString(
tpl: string,
context?: Record<string, any>,
options?: RenderOptions,
callback?: TemplateCallback<string>
) {
return this.nunjucksEnvironment.renderString(
tpl,
context,
options,
callback
);
}

addFilter(name: string, callback: (...args) => string) {
Expand Down
34 changes: 34 additions & 0 deletions packages/view-nunjucks/src/interface.ts
@@ -0,0 +1,34 @@

export interface INunjucksConfig {
/**
* controls if output with dangerous characters are escaped automatically.
*/
autoescape: boolean;
/**
* throw errors when outputting a null/undefined value
*/
throwOnUndefined: boolean;
/**
* automatically remove trailing newlines from a block/tag
*/
trimBlocks: boolean;
/**
* automatically remove leading whitespace from a block/tag
*/
lstripBlocks: boolean;
/**
* use a cache and recompile templates each time. false in local env.
*/
cache: boolean;
/**
* defines the syntax for nunjucks tags.
*/
tags?: {
blockStart?: string;
blockEnd?: string;
variableStart?: string;
variableEnd?: string;
commentStart?: string;
commentEnd?: string;
};
};

0 comments on commit 8d39739

Please sign in to comment.