express-react-views icon indicating copy to clipboard operation
express-react-views copied to clipboard

Prepend doctype only when it is HTML

Open lijunle opened this issue 10 years ago • 1 comments

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 xmlns attribute 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!

lijunle avatar Oct 21 '15 17:10 lijunle

/cc @zpao

I could like to help to send a RP if you think this is OK.

lijunle avatar Oct 22 '15 15:10 lijunle