Skip to content

Instantly share code, notes, and snippets.

@nleush
Last active February 11, 2022 17:48
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 nleush/c8c7c6dc28743ccc84cc33039e23a8d4 to your computer and use it in GitHub Desktop.
Save nleush/c8c7c6dc28743ccc84cc33039e23a8d4 to your computer and use it in GitHub Desktop.
Generate required AMP script based on the component HTML
const AMP_SCRIPTS = {
"apester-media": "https://cdn.ampproject.org/v0/amp-apester-media-0.1.js",
"iframely": "https://cdn.ampproject.org/v0/amp-iframely-0.1.js",
"iframe": "https://cdn.ampproject.org/v0/amp-iframe-0.1.js",
"youtube": "https://cdn.ampproject.org/v0/amp-youtube-1.0.js"
};
function getScript(html) {
const m = html && html.match(/^<amp-([a-z\-]+)/);
if (m) {
const component = m[1];
if (component in AMP_SCRIPTS) {
return `<script async custom-element="amp-${ component }" src="${ AMP_SCRIPTS[component] }"></script>`;
} else {
return `<script async custom-element="amp-${ component }" src="https://cdn.ampproject.org/v0/amp-${ component }-0.1.js"></script>\n`;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment