690 words
3 minutes
Link Cards Example

Link Cards are similar to the <LinkCard> component in Starlight, displaying links in a card format.

Usage#

Include only a single “bare” link (a link without descriptive text), or something similar, within a paragraph in Markdown, and it will automatically be converted into a Link Card.


**External Links**

https://astro.build/

<https://github.com/saicaca/fuwari/>

[https://fuwari.vercel.app/](https://fuwari.vercel.app/)

**Internal Links**

[/posts/guide/](/posts/guide/)

For more details, see the internalLink option section.

**IDN (Internationalized Domain Name)**

https://はじめよう.みんな/
NOTE

Once the cards are displayed, try changing the theme color or enabling dark mode!

Options#

You can specify the options in the astro.config.mjs file.

...
import fuwariLinkCard from "./src/plugins/fuwari-link-card.js"
...
export default defineConfig({
  ...
  integrations: [
    ...
    fuwariLinkCard(), // Plugin here
    ...

If the order of plugins is complex, you can also specify it as a remark plugin.

...
import remarkLinkCard from "./src/plugins/remark-link-card.js"
...
export default defineConfig({
  ...
  markdown: {
    ...
    remarkPlugins: [
      ...
      remarkLinkCard, // Plugin here
      ...
NameTypeDefaultDescription
devModebooleanimport.meta.env.DEVEnable or disable development mode.
linkAttributesObject{ target: ”, rel: ” }Set the target and relationship attributes for external links. These attributes can also be left unset to delegate handling to other plugins.
rewriteRulesArray<Object>[]Rewrite specific metadata attributes fetched from links, such as the title and description.
basestring’/‘Specify the same base path as Astro’s. For details, refer here. When used as an integration, if not specified, this option will be determined automatically.
defaultThumbnailstringPath to the default thumbnail image to use when the metadata does not include image data. It should be relative to the public directory. For example, set defaultThumbnail to ‘images/default-thumbnail.jpg’ if the image is located at public/images/default-thumbnail.jpg.
internalLinkObject{ enabled: false, site: ” }Enable internal link processing within your site.
cacheObjectSee detailed options below.Download and cache images during the build process.

linkAttributes#

NameTypeDefaultDescription
targetstringSpecify where to open linked documents. The default (empty) does not set a target on links.
relstringDefine the relationship between the current document and the linked document. The default (empty) does not set any relationship.

rewriteRules#

NameTypeDefaultDescription
urlRegExpA regular expression pattern is used to match a specific URL.
rewriteStepsArray<Object>Defines rewrite rules for specific metadata attributes.

Below is an example that shows how to rewrite the “title” and “description” for metadata fetched from links pointing to a GitHub repository.

rewriteRules: [
  {
    url: /^https:\/\/github\.com\/[^\/]+\/[^\/]+\/?$/,
    rewriteSteps: [
      { key: "title", pattern: /:.*/, replacement: "" },
      {
        key: "description",
        pattern: /(?: (?:\. )?Contribute to (?:.+\/.+) .+\.?)|(?: - (?:.+\/.+))$/,
        replacement: "",
      },
      {
        key: "description",
        pattern: /^Contribute to (?:.+\/.+) .+\.?$/,
        replacement: "No description provided.",
      },
    ],
  },
],
NameTypeDefaultDescription
keystringMetadata attribute key to be rewritten.
patternRegExpRegular expression pattern used to match the current value of the metadata attribute. The part of the value that matches this pattern will be replaced.
replacementstringString to replace the matched pattern in the metadata attribute.

Set enabled to true to enable internal link processing within your site. The site and base options resolve internal links to absolute URLs. Internal links must point to files that exist on the server.

NameTypeDefaultDescription
enabledbooleanfalseEnable or disable internal link processing.
sitestringSpecify the same deployed URL as Astro’s. For details, refer here. When used as an integration, if not specified, this option will be determined automatically.

cache#

The following related options allow you to easily control the caching behavior.

NameTypeDefaultDescription
enabledbooleanfalseEnable or disable caching.
outDirstring’./dist/‘Output directory path. For details, refer here. Aligning with Astro allows you to benefit from features like image optimization.
cacheDirstring’./link-card/‘Cache directory path. If devMode is set to true, the final URL to the cached images will be base + outDir + cacheDir. Otherwise, it will be base + cacheDir.
maxFileSizenumber0Maximum file size (in bytes) to cache. Set to 0 for no limit.
maxCacheSizenumber0Maximum total cache size (in bytes). Set to 0 for no limit.
userAgentstring’Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36’Identifier included in HTTP request headers to specify the client.

Quick and Easy Options Setup#

This plugin uses @fastify/deepmerge to simplify options setup.

HTML Structure for Styling#

The styles are specified in src/styles/link-card.css, and the HTML is automatically generated. Below is an example structure to guide you when customizing the styles:

<div class="link-card__container">
  <a href="https://astro.build/" class="link-card">
    <div class="link-card__info">
      <div class="link-card__title">Astro</div>
      <div class="link-card__description">Astro builds fast content sites, powerful web applications, dynamic server APIs, and everything in-between.</div>
      <div class="link-card__metadata">
        <div class="link-card__domain">
          <img alt="favicon" class="link-card__favicon" src="https://www.google.com/s2/favicons?domain=astro.build">
          <span class="link-card__domain-name">astro.build</span>
        </div>
      </div>
    </div>
    <div class="link-card__thumbnail">
      <img alt="Astro - Build the web you want." class="link-card__image" src="https://astro.build/og/astro.jpg">
    </div>
  </a>
</div>
Link Cards Example
https://fuwari.vercel.app/posts/link-cards/
Author
Lorem Ipsum
Published at
2025-02-23