Skip to content

Instantly share code, notes, and snippets.

Created September 11, 2019 05:53
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
const ampBoilerplate =
'<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>'
const modifyHtml = html => {
// Add amp-custom tag to added CSS and join all the CSS into one <style-tag>
let styleConcat = ''
html = html.replace(/(<style\b[^<>]*>)([^<]*)(<\/style>)/gi, (_match, p1, p2) => {
styleConcat += p2
return ''
html = html.replace('</head>', `<style amp-custom data-vue-ssr>${styleConcat}</style></head>`)
// Remove every script tag from generated HTML except the JSON type for the amp-state or the AMP templates
html = html.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, match => {
if (match.includes(`type="application/json"`)) {
return match.replace(/&quot;/g, '"')
return ''
// Add AMP script before </head>
const ampScript = `<script async src=""></script>
<script async custom-element="amp-carousel" src=""></script>
<script async custom-element="amp-bind" src=""></script>
<script async custom-element="amp-list" src=""></script>
<script async custom-template="amp-mustache" src=""></script>`
html = html.replace('</head>', ampScript + ampBoilerplate + '</head>')
html = html.replace(/<amp-carousel([^>]*)>/gi, match => {
return match.replace('autoplay="autoplay"', 'autoplay')
return html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment