Created
June 23, 2023 13:28
-
-
Save m-arrieta-r/b4db86b93ab46f144f06b8fc6d933ec8 to your computer and use it in GitHub Desktop.
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
import jsdom from 'jsdom'; | |
const originalHTML = '<html><head></head><body><oembed url="https://www.youtube.com/watch?v=dQw4w9WgXcQ"></oembed><p>Some text.</p><oembed url="https://vimeo.com/123456789"></oembed></body></html>'; | |
const dom = new jsdom.JSDOM(originalHTML, { contentType: 'text/html' }); | |
const document = dom.window.document; | |
const oembeds = document.querySelectorAll('oembed'); | |
const promises = Array.from(embeds).map(async () => { | |
const url = element.attributes.url.value; | |
const contentResponse = await fetch(`https://oembed.ameliarueda.com/oembed?url=${url}`); | |
const content = await contentResponse.json(); | |
// oembed.parentNode.replaceChild(myEmbed, oembed); | |
}); | |
await Promise.all(promises); | |
oembed.parentNode.replaceChild(myEmbed, oembed); | |
// Serialize the modified HTML back into a string | |
const modifiedHTML = dom.serialize(); | |
console.log(modifiedHTML); // Output: <html><head></head><body><myembed url="https://www.youtube.com/watch?v=dQw4w9WgXcQ"></myembed><p>Some text.</p><myembed url="https://vimeo.com/123456789"></myembed></body></html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment