This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(/"/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