Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
run(_/* context */, name, model = {}, children = []) {
try {
const childrenComponents = children
.filter(_ => _)
.map(c => {
const Component = components[c.name](c.model);
return React.cloneElement(Component, { key: c.name });
});
const Component = components[name];
const modelWithChildren = Object.assign({}, model, {children: childrenComponents});
const instantiatedComponent = React.createElement(Component, modelWithChildren);
const html = ReactDOMServer.renderToString(instantiatedComponent);
return new nunjucks.runtime.SafeString(html);
} catch (e) {
console.error(e);
throw e;
}
};
};
constructor(app) {
const fileLoader = new FileLoader(app);
super(fileLoader, app.config.nunjucks);
this.app = app;
this[LOAD_FILTER]();
// monkey patch `escape` with `app.helper.escape` provided by `egg-security` for better performance
nunjucks.lib.escape = app.Helper.prototype.escape;
// http://disse.cting.org/2016/08/02/2016-08-02-sandbox-break-out-nunjucks-template-engine
const originMemberLookup = nunjucks.runtime.memberLookup;
nunjucks.runtime.memberLookup = function(...args) {
const val = args[1];
if (val === 'prototype' || val === 'constructor') return null;
return originMemberLookup(...args);
};
this.ViewHelper = createHelper(app, this.filters);
}
const safe = require('nunjucks').runtime.markSafe;
const marked = require('marked');
marked.setOptions({ smartypants: true });
module.exports = {
markdownFilter: (str, kwargs) => {
// strip outer <p> tags?
const strip = typeof kwargs === 'undefined' ?
false : kwargs.strip || false;
return !strip ? safe(marked(str)) :
safe(marked(str).trim().replace(/^</p><p>|<\/p>$/g, ''));
},
};
</p>
// Based on https://github.com/zephraph/nunjucks-markdown
const { SafeString } = require('nunjucks').runtime;
module.exports = function Render() {
this.tags = ['render'];
this.parse = function(parser, nodes, lexer) {
const tok = parser.nextToken();
// Parse the tag and collect any arguments
const args = parser.parseSignature(null, true);
parser.advanceAfterBlockEnd(tok.value);
// If arguments, return the fileTag constructed node
if (args.children.length > 0) return new nodes.CallExtensionAsync(this, 'fileTag', args);
// Otherwise parse until the close block and move the parser to the next position
const body = parser.parseUntilBlocks('endrender');
run(context, ...args) {
const [path, name, rest = { class: '', __exact: true }] = args;
const currentPath = `/${context.ctx.page.path}`;
const isExact = rest.__exact;
if (isExact && path === currentPath) rest.class = `active ${rest.class}`;
if (!isExact && !isEmpty(path.split('/'), currentPath.split('/')))
rest.class = `active ${rest.class}`;
const attrs = Object.keys(rest)
.filter(_ => !_.startsWith('__'))
.map(key => `${key}="${rest[key]}"`)
.join(' ');
return new nunjucks.runtime.SafeString(
`<a href="${path}">${name}</a>`
);
}
}
helper: function (...args) {
const page = this.env.pages.find(...args);
if (!page || !page.permalink) {
throw new Error(`Could not generate permalink for page`);
}
return new nunjucks.runtime.SafeString(`<a href="${page.permalink}">${page.label || page.title}</a>`);
}
};
run(context, name, data) {
const html = this.env.render(`components/${name}/${name}.njk`, data);
return new nunjucks.runtime.SafeString(html);
};
};
markdownTag.fileTag = (context, file) => {
return new nunjucks.runtime.SafeString(marked(env.render(file, context)));
};
environment.addFilter('submissionTable', function(obj, components) {
return new nunjucks.runtime.SafeString(util.renderFormSubmission(obj, components));
});
environment.addFilter('componentValue', function(obj, key, components) {
var compValue = util.renderComponentValue(obj, key, components);
return new nunjucks.runtime.SafeString(compValue.value);
});