Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(view-nunjucks): add autoescape config && add ts interface #2148

Merged
merged 1 commit into from
Jul 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 3 additions & 33 deletions packages/view-nunjucks/src/config/config.default.ts
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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;
};
};