Last active
February 11, 2022 17:48
-
-
Save nleush/c8c7c6dc28743ccc84cc33039e23a8d4 to your computer and use it in GitHub Desktop.
Generate required AMP script based on the component HTML
This file contains hidden or 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 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