npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

remark-oembed

v1.2.2

Published

Converts URLs surrounded by newlines into embeds that are loaded asynchronously

Downloads

2,634

Readme

remark-oembed

Converts URLs surrounded by newlines into embeds that are loaded asynchronously.

For the most part, this code is taken directly from agentofuser/remark-oembed 🙏

installation

yarn add remark-oembed

usage

Say we have the following markdown:

This is a youtube video:

https://www.youtube.com/watch?v=aoLhACqJCUg

This is a photo:

https://www.flickr.com/photos/pedrocaetano/27432477888

This another photo:

http://www.23hq.com/mprove/photo/66422006

Check it out 👆

And our script looks as follows:

const remark = require('remark');

remark()
  .use(require('remark-oembed'))
  .use(require('remark-html'))
  .process(src, (err, file) => console.log(String(file)));

Now, running it yields:

<p>This is a youtube video:</p>
<div class="remark-oembed-inline" data-oembed>
  <a
    href="https://www.youtube.com/watch?v=aoLhACqJCUg"
    class="remark-oembed-anchor"
    data-oembed
    rel="noopener noreferrer nofollow"
    target="_blank"
  >
    <img
      src="https://i.ytimg.com/vi/aoLhACqJCUg/hqdefault.jpg"
      title="Há Música na Cidade 2015"
      width="480"
      height="270"
      class="remark-oembed-photo"
      data-oembed
    />
  </a>
  <template data-oembed-template>
    <iframe
      width="480"
      height="270"
      src="https://www.youtube.com/embed/aoLhACqJCUg?feature=oembed"
      frameborder="0"
      allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
      allowfullscreen
    ></iframe>
  </template>
</div>
<p>This is a photo:</p>
<div class="remark-oembed-inline" data-oembed>
  <a
    href="https://www.flickr.com/photos/pedrocaetano/27432477888"
    class="remark-oembed-anchor"
    data-oembed
    rel="noopener noreferrer nofollow"
    target="_blank"
  >
    <img
      src="https://live.staticflickr.com/872/27432477888_8236c59e1d_b.jpg"
      title="Leiria by night"
      width="1024"
      height="678"
      class="remark-oembed-photo"
      data-oembed
    />
  </a>
  <template data-oembed-template>
    <a
      data-flickr-embed="true"
      href="https://www.flickr.com/photos/pedrocaetano/27432477888/"
      title="Leiria by night by Pedro Nuno Caetano, on Flickr"
    >
      <img
        src="https://live.staticflickr.com/872/27432477888_8236c59e1d_b.jpg"
        width="1024"
        height="678"
        alt="Leiria by night"
      />
    </a>
    <script
      async
      src="https://embedr.flickr.com/assets/client-code.js"
      charset="utf-8"
    ></script>
  </template>
</div>
<p>This another photo:</p>
<a
  href="http://www.23hq.com/mprove/photo/66422006"
  class="remark-oembed-anchor"
  data-oembed
  rel="noopener noreferrer nofollow"
  target="_blank"
>
  <img
    src="http://www.23hq.com/9484736/66422006_0e82f5775b3b15207f50f5af5a625652_large.jpg"
    data-src="http://www.23hq.com/9484736/66422006_0e82f5775b3b15207f50f5af5a625652_large.jpg"
    title="Hamburg. Uhlenhorst. Das F&#x26;auml;hrhaus (33)"
    width="756"
    height="495"
    class="remark-oembed-photo"
    data-oembed
  />
</a>
<p>Check it out 👆</p>
<script async defer>
  // ...
  document.querySelectorAll('div[data-oembed]').forEach((el) => {
    const template = el
      .querySelector('[data-oembed-template]')
      .content.cloneNode(true);
    el.innerHTML = '';
    el.attachShadow({ mode: 'closed' }).appendChild(template);
  });

  document.querySelectorAll('img[data-oembed][data-src]').forEach((img) => {
    img.setAttribute('src', img.getAttribute('data-src'));
    img.removeAttribute('data-src');
  });

  document.querySelectorAll('[data-oembed]').forEach((el) => {
    el.removeAttribute('data-oembed');
  });
  //...
</script>

Take a look at how it would look before executing the script, and the unchanged HTML output.

syncWidget

If you don't want to load the widget asynchronously and just default to how it would work in any other plugin, you can use syncWidget.

remark()
  .use(require('remark-oembed'), { syncWidget: true })
  .use(require('remark-html'))
  .process(src, (err, file) => console.log(String(file)));
<div class="remark-oembed-inline" data-oembed>
  <iframe
    width="480"
    height="270"
    src="https://www.youtube.com/embed/aoLhACqJCUg?feature=oembed"
    frameborder="0"
    allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
    allowfullscreen
  ></iframe>
</div>

As you can see above, it doesn't generate a preview <img /> neither a <a /> to the source. Also, it doesn't try to replace the <img /> and the <a /> with the <iframe />.

Take a look at how it would look before executing the script, and the unchanged HTML output.

asyncImg

Not only with thumbnails, but also with oembed's type === 'photo', you can make pictures also load asyncronously by setting asyncImg. This is accomplishing by generating an empty base64 src with the same size as the original image:

remark()
  .use(require('remark-oembed'), { asyncImg: true })
  .use(require('remark-html'))
  .process(src, (err, file) => console.log(String(file)));
<p>This is a youtube video:</p>
<div class="remark-oembed-inline" data-oembed>
  <a
    href="https://www.youtube.com/watch?v=aoLhACqJCUg"
    class="remark-oembed-anchor"
    data-oembed
    rel="noopener noreferrer nofollow"
    target="_blank"
  >
    <img
      src="data:image/png;base64,iVBOR...QmCC"
      title="Há Música na Cidade 2015"
      width="480"
      height="270"
      class="remark-oembed-photo"
      data-oembed
    />
  </a>
  <template data-oembed-template>
    <iframe
      width="480"
      height="270"
      src="https://www.youtube.com/embed/aoLhACqJCUg?feature=oembed"
      frameborder="0"
      allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
      allowfullscreen
    ></iframe>
  </template>
</div>
<p>This is a photo:</p>
<div class="remark-oembed-inline" data-oembed>
  <a
    href="https://www.flickr.com/photos/pedrocaetano/27432477888"
    class="remark-oembed-anchor"
    data-oembed
    rel="noopener noreferrer nofollow"
    target="_blank"
  >
    <img
      src="data:image/png;base64,iVBOR...QmCC"
      title="Leiria by night"
      width="1024"
      height="678"
      class="remark-oembed-photo"
      data-oembed
    />
  </a>
  <template data-oembed-template>
    <a
      data-flickr-embed="true"
      href="https://www.flickr.com/photos/pedrocaetano/27432477888/"
      title="Leiria by night by Pedro Nuno Caetano, on Flickr"
    >
      <img
        src="https://live.staticflickr.com/872/27432477888_8236c59e1d_b.jpg"
        width="1024"
        height="678"
        alt="Leiria by night"
      />
    </a>
    <script
      async
      src="https://embedr.flickr.com/assets/client-code.js"
      charset="utf-8"
    ></script>
  </template>
</div>
<p>This another photo:</p>
<a
  href="http://www.23hq.com/mprove/photo/66422006"
  class="remark-oembed-anchor"
  data-oembed
  rel="noopener noreferrer nofollow"
  target="_blank"
>
  <img
    src="data:image/png;base64,iVBOR...QmCC"
    data-src="http://www.23hq.com/9484736/66422006_0e82f5775b3b15207f50f5af5a625652_large.jpg"
    title="Hamburg. Uhlenhorst. Das F&#x26;auml;hrhaus (33)"
    width="756"
    height="495"
    class="remark-oembed-photo"
    data-oembed
  />
</a>
<p>Check it out 👆</p>
<script async defer>
  // ...
  document.querySelectorAll('div[data-oembed]').forEach((el) => {
    const template = el
      .querySelector('[data-oembed-template]')
      .content.cloneNode(true);
    el.innerHTML = '';
    el.attachShadow({ mode: 'closed' }).appendChild(template);
  });

  document.querySelectorAll('img[data-oembed][data-src]').forEach((img) => {
    img.setAttribute('src', img.getAttribute('data-src'));
    img.removeAttribute('data-src');
  });

  document.querySelectorAll('[data-oembed]').forEach((el) => {
    el.removeAttribute('data-oembed');
  });
  // ...
</script>

Take a look at how it would look before executing the script, and the unchanged HTML output.

jsx

To use with @mdx-js/mdx, set jsx as true.

const mdx = require('@mdx-js/mdx');

await mdx(src, {
  remarkPlugins: [[require('remark-oembed'), { jsx: true }],
});

license

BSD-3-Clause