$content = Get-Content 'site/tools/merchant-feed-generator.html' -Raw
$content = $content.Replace('Google Merchant feed generator', 'Pinterest product feed generator')
$content = $content.Replace('Free Google Merchant Feed Generator', 'Free Pinterest Product Feed Generator')
$content = $content.Replace('merchant-feed-generator', 'pinterest-product-feed-generator')
$content = $content.Replace('Google Merchant Center-ready XML feed', 'Pinterest-ready CSV feed')
$content = $content.Replace('Google Merchant Center', 'Pinterest Catalogs')
$content = $content.Replace('Merchant feed XML', 'Pinterest feed CSV')
$content = $content.Replace('merchant-feed.xml', 'pinterest-feed.csv')
$content = $content.Replace('application/xml', 'text/csv')
$content = $content.Replace('xml', 'csv')
$content = $content.Replace('XML', 'CSV')

$csvBuildFeed = "  function buildFeed(items){`n    var header = 'id,title,description,availability,condition,price,link,image_link,brand';`n    var rows = items.map(function(p){`n      var v = [p.id, p.title, p.description, p.availability || 'in_stock', p.condition || 'new', p.price, p.link, p.image_link, p.brand];`n      return v.map(function(val){`n        var str = String(val == null ? `"`" : val);`n        if (/[`",\n]/.test(str)) return '`"' + str.replace(/`"/g, '`"`"') + '`"';`n        return str;`n      }).join(',');`n    });`n    $('output').textContent = [header].concat(rows).join('\n');`n    $('feed-output-section').classList.remove('hidden');`n  }"

$content = $content -replace '(?s)  function buildFeed\(items\)\{.*?  \}', $csvBuildFeed

Set-Content -Path 'site/tools/pinterest-product-feed-generator.html' -Value $content -Encoding UTF8
