Released remark-link-card-plus: A Remark Plugin to Convert Links into Cards

Introduction

A while ago, I rebuilt my blog from Gatsby to Astro.

During this process, I used a plugin called remark-link-card to convert links in articles into cards. However, I encountered an issue where links inside lists were also processed, causing the first node of the article to be converted into a card. Although I raised an issue, it seemed like there had been a significant gap since the last release. So, I decided to create my own plugin.

What is This Plugin?

This is a remark plugin that converts links in Markdown into cards. It is based on remark-link-card but rebuilt with additional features and fixes. Here are the similarities and differences with the original:

Similarities

Differences

Let’s take a closer look.

How to Use It

Like other plugins, you can use it by applying use(remarkLinkCard[, options]) to remark, which converts links in Markdown into cards.

import { remark } from "remark";
import remarkLinkCard from "remark-link-card-plus";

const exampleMarkdown = `
## Example

https://github.com

Inline links like [GitHub](https://github.com) will **not** be converted.
contents
`;

(async () => {
  const result = await remark()
    .use(remarkLinkCard, { cache: true, shortenUrl: true })
    .process(exampleMarkdown);

  console.log(result.value);
})();

The output will look like this:

## Example

<div class="remark-link-card-plus__container">
  <a href="https://github.com/" target="_blank" rel="noreferrer noopener" class="remark-link-card-plus__card">
    <div class="remark-link-card-plus__main">
      <div class="remark-link-card-plus__content">
        <div class="remark-link-card-plus__title">GitHub · Build and ship software on a single, collaborative platform</div>
        <div class="remark-link-card-plus__description">Join the world's most widely adopted, AI-powered developer platform where millions of developers, businesses, and the largest open source community build software that advances humanity.</div>
      </div>
      <div class="remark-link-card-plus__meta">
        <img src="https://www.google.com/s2/favicons?domain=github.com" class="remark-link-card-plus__favicon" width="14" height="14" alt="favicon">
        <span class="remark-link-card-plus__url">github.com</span>
      </div>
    </div>
    <div class="remark-link-card-plus__thumbnail">
      <img src="https://github.githubassets.com/assets/home24-5939032587c9.jpg" class="remark-link-card-plus__image" alt="ogImage">
    </div>
  </a>
</div>

Inline links like [GitHub](https://github.com) will **not** be converted.
contents

This is especially useful when you want to convert links into cards for blogs built with Astro. You just need to specify it in the remarkPlugins section of your astro.config.mjs file.

import { defineConfig } from 'astro/config';
import tailwind from '@astrojs/tailwind';
import remarkLinkCard from 'remark-link-card-plus';

export default defineConfig({
  integrations: [
    tailwind(),
  ],
  markdown: {
    remarkPlugins: [
      [
        remarkLinkCard, {
          cache: true,
          shortenUrl: true,
          thumbnailPosition: "right",
        },
      ],
    ],
  },
});

Note that styles are not included in the HTML tags generated by this plugin, so you need to apply your own styles. You can style the following classes:

.remark-link-card-plus__container {}

.remark-link-card-plus__card {}

.remark-link-card-plus__main {}

.remark-link-card-plus__content {}

.remark-link-card-plus__title {}

.remark-link-card-plus__description {}

.remark-link-card-plus__meta {}

.remark-link-card-plus__favicon {}

.remark-link-card-plus__url {}

.remark-link-card-plus__thumbnail {}

.remark-link-card-plus__image {}

Available Options

As of version v0.2.0, the following three options are available:

You can see a demo page showing the cards displayed in an Astro blog.

Conclusion

Feel free to try it out! The source code is available on GitHub, so you’re welcome to create issues for bugs or suggestions. If you like the plugin, I’d appreciate it if you could give it a star!

Related Posts