Skip to content

Instantly share code, notes, and snippets.

@cvetkovskin
Created September 11, 2019 05:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cvetkovskin/f823618a62f6b99e0132d0400b499e97 to your computer and use it in GitHub Desktop.
Save cvetkovskin/f823618a62f6b99e0132d0400b499e97 to your computer and use it in GitHub Desktop.
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="https://cdn.ampproject.org/v0.js"></script>
<script async custom-element="amp-carousel" src="https://cdn.ampproject.org/v0/amp-carousel-0.1.js"></script>
<script async custom-element="amp-bind" src="https://cdn.ampproject.org/v0/amp-bind-0.1.js"></script>
<script async custom-element="amp-list" src="https://cdn.ampproject.org/v0/amp-list-0.1.js"></script>
<script async custom-template="amp-mustache" src="https://cdn.ampproject.org/v0/amp-mustache-0.2.js"></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