src/index.js
function inject(targetContent, sourceContent, opt, tagsRegExp) {
	const startTag = tagsRegExp.start;
	const endTag = tagsRegExp.end;
	let startMatch;
	let endMatch;

	while ((startMatch = startTag.exec(targetContent)) !== null) {
		// Take care of content length change
		endTag.lastIndex = startTag.lastIndex;
		endMatch = endTag.exec(targetContent);
		if (!endMatch) {
			throw error('Missing end tag for start tag: ' + startMatch[0]);
		}
		const toInject = [sourceContent];
		// content part before start tag
		let newContent = targetContent.slice(0, startMatch.index);

		if (opt.removeTags) {
			// Take care of content length change
			startTag.lastIndex -= startMatch[0].length;
		} else {
			// <startMatch> + partial body + <endMatch>
			toInject.unshift(startMatch[0]);
			toInject.push(endMatch[0]);
		}
		const previousInnerContent = targetContent.substring(startTag.lastIndex, endMatch.index);
		const indent = getLeadingWhitespace(previousInnerContent);
		// add new content
		newContent += toInject.join(indent);
		// append rest of target file
		newContent += targetContent.slice(endTag.lastIndex);
		// replace old content with new
		targetContent = newContent;
	}
	startTag.lastIndex = 0;
	endTag.lastIndex = 0;
	return targetContent;
}
function inject(targetContent, sourceContent, opt, tagsRegExp) {
	const startTag = tagsRegExp.start;
	const endTag = tagsRegExp.end;
	let startMatch;
	let endMatch;

	while ((startMatch = startTag.exec(targetContent)) !== null) {
		// Take care of content length change
		endTag.lastIndex = startTag.lastIndex;
		endMatch = endTag.exec(targetContent);
		if (!endMatch) {
			throw error('Missing end tag for start tag: ' + startMatch[0]);
		}
		const toInject = [sourceContent];
		// content part before start tag
		let newContent = targetContent.slice(0, startMatch.index);

		if (opt.removeTags) {
			// Take care of content length change
			startTag.lastIndex -= startMatch[0].length;
		} else {
			// <startMatch> + partial body + <endMatch>
			toInject.unshift(startMatch[0]);
			toInject.push(endMatch[0]);
		}
		const previousInnerContent = targetContent.substring(startTag.lastIndex, endMatch.index);
		const indent = getLeadingWhitespace(previousInnerContent);
		// add new content
		newContent += toInject.join(indent);
		// append rest of target file
		newContent += targetContent.slice(endTag.lastIndex);
		// replace old content with new
		targetContent = newContent;
	}
	startTag.lastIndex = 0;
	endTag.lastIndex = 0;
	return targetContent;
}
function inject ( targetContent , sourceContent , opt , tagsRegExp ) { const startTag = tagsRegExp . start ; const endTag = tagsRegExp . end ; let startMatch ; let endMatch ; while ( ( startMatch = startTag . exec ( targetContent ) ) !== null ) { // Take care of content length change endTag . lastIndex = startTag . lastIndex ; endMatch = endTag . exec ( targetContent ) ; if ( ! endMatch ) { throw error ( 'Missing end tag for start tag: ' + startMatch [ 0 ] ) ; } const toInject = [ sourceContent ] ; // content part before start tag let newContent = targetContent . slice ( 0 , startMatch . index ) ; if ( opt . removeTags ) { // Take care of content length change startTag . lastIndex -= startMatch [ 0 ] . length ; } else { // <startMatch> + partial body + <endMatch> toInject . unshift ( startMatch [ 0 ] ) ; toInject . push ( endMatch [ 0 ] ) ; } const previousInnerContent = targetContent . substring ( startTag . lastIndex , endMatch . index ) ; const indent = getLeadingWhitespace ( previousInnerContent ) ; // add new content newContent += toInject . join ( indent ) ; // append rest of target file newContent += targetContent . slice ( endTag . lastIndex ) ; // replace old content with new targetContent = newContent ; } startTag . lastIndex = 0 ; endTag . lastIndex = 0 ; return targetContent ; }
Inject tags into target content between given
start and end tags

@param {String} targetContent
@param {String} sourceContent
@param {Object} opt
@param {Object} tagsRegExp
@returns {String}
Inject tags into target content between given start and end tags