express-react-views
express-react-views copied to clipboard
Prepend doctype only when it is HTML
I am using React and Express to render SVG. The generated string is something like <svg>...</svg>.
- It is not a HTML, do not prepend doctype in front of the string.
- The SVG is the root element, it needs one more
xmlnsattribute to tell browser to render this SVG:xmlns="http://www.w3.org/2000/svg".
Currently, I hack the viewEngine callback as a workaround:
const viewEngine = react.createEngine({ transformViews: false });
app.engine('js', (filePath, options, callback) => {
return viewEngine(filePath, options, (error, html) => {
const raw = html.substring('<!DOCTYPE html>'.length);
const hack = raw.indexOf('<svg') === 0
? raw.replace('<svg', '<svg xmlns="http://www.w3.org/2000/svg"')
: html;
callback(error, hack);
});
});
Hope this package could handle it. Thanks!
/cc @zpao
I could like to help to send a RP if you think this is OK.