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

rehype-og-card

v1.0.11

Published

Rehype plugin to convert text links to link cards

Downloads

350

Readme

rehype-og-card

Rehype plugin to convert text links to link cards inspired by remark-link-card. Bare links are converted to link cards, but not links in lists or non-bare links.

| Features | remark-link-card | rehype-og-card | | :--------------------------------------------- | :---------------------------------------------------------------------------------------: | :------------: | | Plugin type | Remark | Rehype | | TypeScript support | ❌ | ✅ | | Convert bare links to link cards | ✅ | ✅ | | Shorten URLs displayed on link cards | ✅ | ✅ | | Server-side cache for images | ✅ | ✅ | | Cache images with long URLs | ❌ (implemented but not released) | ✅ | | Build cache for Open Graph metadata and images | ❌ | ✅ | | Exclude links in lists from conversion | ❌ | ✅ | | Exclude specific domains from conversion | ❌ | ✅ | | Lazy loading images | ❌ | ✅ | | Async decoding images | ❌ | ✅ | | Customize the crawler's user agent string | ❌ | ✅ |

Installation

npm install rehype-og-card

Usage

import rehypeOGCard, { RehypeOGCardOptions } from "rehype-og-card";
import remarkParse from "remark-parse";
import remarkRehype from "remark-rehype";
import rehypeStringify from "rehype-stringify";
import { unified } from "unified";

const processorFactory = (options?: RehypeOGCardOptions) => {
    return unified().use(remarkParse).use(remarkRehype).use(rehypeOGCard, options).use(rehypeStringify);
};

const main = async () => {
    const processor = processorFactory({ serverCache: false });

    const input = `
this is a link: https://blog.google/products/android/world-emoji-day-2024/

\`https://blog.google/products/android/world-emoji-day-2024/\`

https://blog.google/products/android/world-emoji-day-2024/

[https://blog.google/products/android/world-emoji-day-2024/](https://blog.google/products/android/world-emoji-day-2024/)

[Google blog](https://blog.google/products/android/world-emoji-day-2024/)
    `.trim();

    const result = await processor.process(input);
    console.log(result.toString());
};

main();

Output (formatted for readability):

<p>this is a link: https://blog.google/products/android/world-emoji-day-2024/</p>
<p><code>https://blog.google/products/android/world-emoji-day-2024/</code></p>
<p><div class="og-card-container">
    <a href="https://blog.google/products/android/world-emoji-day-2024/">
        <div class="og-card-info">
            <div class="og-card-title">10 fun facts about emoji for World Emoji Day</div>
            <div class="og-card-description">Celebrate World Emoji Day with Google, and check out what’s new for Emoji Kitchen.</div>
            <div class="og-card-url-container">
                <img class="og-card-favicon" alt="favicon" decoding="async" height="16" loading="lazy" src="https://www.google.com/s2/favicons?domain=blog.google" width="16">
                <span class="og-card-url">blog.google</span>
            </div>
        </div>
        <div class="og-card-image-container">
            <img class="og-card-image" alt="https://storage.googleapis.com/gweb-uniblog-publish-prod/images/world_emoji_day_v2_1.width-1300.png" decoding="async" loading="lazy" src="https://storage.googleapis.com/gweb-uniblog-publish-prod/images/world_emoji_day_v2_1.width-1300.png">
        </div>
    </a>
</div></p>
<p><a href="https://blog.google/products/android/world-emoji-day-2024/">https://blog.google/products/android/world-emoji-day-2024/</a></p>
<p><a href="https://blog.google/products/android/world-emoji-day-2024/">Google blog</a></p>

What will be converted to link cards and what won't?

The following links will be converted to link cards:

  • Bare links
  • Links that have the same URL and text (if you enable the enableSameTextURLConversion option)

The following links will NOT be converted to link cards:

  • Links in lists
  • Links in code blocks
  • Links in sentences
  • Non-bare links
  • Links that have the same URL and text (if you disable the enableSameTextURLConversion option)

Options

All options are optional.

buildCache

Whether to cache the OG metadata and images for the next build. buildCache option requires serverCache option to be enabled.

  • Type: boolean
  • Default: false

buildCachePath

Path to save the build cache. For Astro projects, specify the ./node_modules/.astro directory.

  • Type: string
  • Default: "./.cache/og-card"

crawlerUserAgent

User agent to use for fetching the OG metadata and images.

  • Type: string
  • Default: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36"

decoding

decoding attribute of the image element.

  • Type: "sync" | "async" | "auto"
  • Default: "async"

enableSameTextURLConversion

Whether to enable converting the links that have the same URL and text (e.g., [https://example.com](https://example.com)).

  • Type: boolean
  • Default: false

excludeDomains

Domains to exclude from conversion.

  • Type: string[]
  • Default: []

loading

loading attribute of the image element.

  • Type: "eager" | "lazy"
  • Default: "lazy"

openInNewTab

Whether to open the link in a new tab.

  • Type: boolean
  • Default: false

serverCache

Whether to cache the OG data on the server.

  • Type: boolean
  • Default: true

serverCachePath

Path to save the server cache. Specify the directory whose contents will be copied to the root. For example, for an Astro project, specify the public directory.

  • Type: string
  • Default: "./public"

shortenURL

Whether to shorten the displayed URL.

  • Type: boolean
  • Default: true