Skip to content

Commit

Permalink
escaping curly braces before parsing as JSX
Browse files Browse the repository at this point in the history
  • Loading branch information
Clint Goodman committed Feb 13, 2017
1 parent 3da7129 commit 24e3bef
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/escape.js
@@ -0,0 +1,11 @@
/* If the SVG has text that has curly braces, or
if there is a <style> element (which will, of
course, have curly braces) then we need to
escape those for JSX parsing. */
export default function escape(raw) {
// converts
// <style> .class1 {} .class2{}</style>
// to
// <style> .class1 {`{`}{`}`} .class2{`{`}{`}`}</style>
return raw.replace(/(\{|\})/g, '{`$1`}');
}
4 changes: 3 additions & 1 deletion src/index.js
Expand Up @@ -4,6 +4,7 @@ import template from 'babel-template';
import traverse from 'babel-traverse';
import { parse } from 'babylon';
import optimize from './optimize';
import escape from './escape';
import transformSvg from './transformSvg';

const buildSvg = template(`
Expand Down Expand Up @@ -32,8 +33,9 @@ export default ({ types: t }) => ({
const svgPath = join(dirname(iconPath), path.node.source.value);
const svgSource = readFileSync(svgPath, 'utf8');
const optimizedSvgSource = optimize(svgSource);
const escapeSvgSource = escape(optimizedSvgSource);

const parsedSvgAst = parse(optimizedSvgSource, {
const parsedSvgAst = parse(escapeSvgSource, {
sourceType: 'module',
plugins: ['jsx'],
});
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/close.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 24e3bef

Please sign in to comment.