Skip to content

Commit

Permalink
feat: 模板 base64 filter
Browse files Browse the repository at this point in the history
  • Loading branch information
geekdada committed Aug 25, 2019
1 parent 2f2d340 commit 3d45a23
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
11 changes: 11 additions & 0 deletions lib/template.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,14 @@ test('renderString #4', t => {

t.is(result, '# Comment');
});

test('base64', t => {
const body = `{{ str | base64 }}`;
const str = `testtesttesttest`;

const result = templateEngine.renderString(body, {
str,
});

t.is(result, 'dGVzdHRlc3R0ZXN0dGVzdA==');
});
5 changes: 4 additions & 1 deletion lib/template.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import nunjucks from 'nunjucks';
import { JsonObject } from 'type-fest';
import YAML from 'yaml';
import { toBase64 } from './utils';

export default function getEngine(templateDir: string): nunjucks.Environment {
const engine = nunjucks.configure(templateDir, {
Expand Down Expand Up @@ -28,8 +29,10 @@ export default function getEngine(templateDir: string): nunjucks.Environment {
.join('\n');
});

// istanbul ignore next
// istanbul ignore next
engine.addFilter('yaml', (obj: JsonObject) => YAML.stringify(obj));

engine.addFilter('base64', (str: string) => toBase64(str));

return engine;
};

0 comments on commit 3d45a23

Please sign in to comment.