aboutsummaryrefslogtreecommitdiffstats
path: root/stories/react/utils/InsertSVGIcons.js
blob: 5a5e390048f1657ba1b22d55dff024f38d1f32c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import {renderToStaticMarkup} from 'react-dom/server';
import beautifyHTML from './BeautifyHTML.js';

const insertSVGIcons = ({html, jsx, indentChar = '    '}) => {
	let svgCode = renderToStaticMarkup(jsx).match(/(<svg\b[^<>]*>)[\s\S]*?(<\/svg>)/g);
	let newHTML = html.replace(/\s*<!-- insert SVG -->/g, str => {
		let html = '\n' + svgCode.shift();
		let indentRegExp = new RegExp(`[${indentChar}]*`);
		let startingIndentCount = str.slice(2).match(indentRegExp)[0].length / indentChar.length;
		return beautifyHTML({html, startingIndentCount, indentChar});
	});
	return newHTML;
};

export default insertSVGIcons;