@automattic/social-previews

中文文档
3.4.2发布于 2026/11/03GPL-2.0-or-laterESM + CJS构建来源已验证近 30 天下载 7.2万

A suite of components to generate previews for a post for both social and search engines.

原始使用文档
文档目录5 节
  1. Usage
  2. Properties
  3. Auto Truncation
  4. Security
  5. License

social-previews

This package contains low level components that can be used to display an approximation of how a given post might like look when viewed on various social media / search platforms.

Usage

Here's a simple usage example using the preview component for Facebook:

import { FacebookPreviews } from '@automattic/social-previews';

<FacebookPreviews
    title="Five for the Future"
    description="Launched in 2014, Five for the Future encourages organizations to contribute five percent of their resources to WordPress development. WordPress co-founder Matt Mullenweg proposed this benchmark to maintain a “golden ratio” of contributors to users."
    url="https://wordpress.org/five-for-the-future/"
    user={ { displayName: 'Matt Mullenweg' } }
/>;

You must import CSS separately to style the components. You can import the CSS file directly from the package like so:

import '@automattic/social-previews/style.css';

Here is another example using the Search result component:

import { GoogleSearchPreview } from '@automattic/social-previews';

<GoogleSearchPreview
    title="Five for the Future"
    description="Launched in 2014, Five for the Future encourages organizations to contribute five percent of their resources to WordPress development. WordPress co-founder Matt Mullenweg proposed this benchmark to maintain a “golden ratio” of contributors to users."
    url="https://wordpress.org/five-for-the-future/"
    siteTitle="Five for the Future"
/>;

Twitter previews support the same properties for previewing a single tweet, but can also preview multiple tweets in the form of a Twitter thread. For that, the tweets property takes an array of tweets. Each item in this array can take additional information about the tweet, giving the preview a more native feel.

import { TwitterPreviews } from '@automattic/social-previews';

const tweetTemplate = {
    date: Date.now(),
    name: 'My Account Name',
    profileImage: 'https://abs.twimg.com/sticky/default_profile_images/default_profile_bigger.png',
    screenName: '@myAccount',
};

<TwitterPreviews
    tweets={ [
        {
            ...tweetTemplate,
            text: 'This is the first tweet in a thread, it only has text in it.',
        },
        {
            ...tweetTemplate,
            text: 'The second tweet has some images attached, too!',
            media: [
                {
                    alt: 'The alt text for the first image.',
                    url: 'https://url.for.the/first/image.png',
                    type: 'image/png',
                },
                {
                    alt: 'The alt text for the second image.',
                    url: 'https://url.for.the/second/image.png',
                    type: 'image/png',
                },
            ],
        },
    ] }
/>;

An example of LinkedIn preview

import { LinkedInPreviews } from '@automattic/social-previews';

<LinkedInPreviews
    jobTitle="Job Title (Company Name)"
    image="https://url.for.the/image.png"
    name="LinkedIn Account Name"
    profileImage="https://static.licdn.com/sc/h/1c5u578iilxfi4m4dvc4q810q"
    title="Post title goes here"
    text="The text of the post goes here."
/>;

Tumblr preview

import { TumblrPreviews } from '@automattic/social-previews';

<TumblrPreviews
    title="Five for the Future"
    description="Launched in 2014, Five for the Future encourages organizations to contribute five percent of their resources to WordPress development. WordPress co-founder Matt Mullenweg proposed this benchmark to maintain a “golden ratio” of contributors to users."
    image="https://url.for.the/image.png"
    url="https://wordpress.org/five-for-the-future/"
    user={ { displayName: 'Matt Mullenweg' } }
    customText="Some custom text here"
/>;

Properties

There are a number of common properties used across all components:

  • title - the title of the post being previewed.
  • description - a longer description of the post being previewed.
  • url - the full URL of the post being previewed.

In addition each individual component accepts optional additional properties that may be specific to their given platform (eg: image, author...etc).

Auto Truncation

Note that due to limits enforced by each given platform some strings may need to be truncated. Each component has its individual rules governing the number of characters in a given field allowed before truncation occurs. Limits are typically higher for description fields and shorter for title fields. Truncation by SPACE character is preferred, but where that is not possible a hard truncation is imposed. In both cases an ellipsis character (…)`is appended to the end of the string to indicate that a truncation has occurred.

Security

Need to report a security vulnerability? Go to https://automattic.com/security/ or directly to our security bug bounty site https://hackerone.com/automattic.

License

social-previews is licensed under GNU General Public License v2 (or later)

版本与发布记录

@automattic/social-previews 共发布 93 个版本,首次发布于 2020/07/22;过去 12 个月发布了 46 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.4.2latest2026/11/035 天前
3.4.12026/10/2910 天前
3.4.02026/10/2612 天前
3.3.172026/10/2019 天前
3.3.162026/10/1523 天前
3.3.152026/10/1326 天前

关于 @automattic/social-previews 的常见问题

@automattic/social-previews 是什么?

A suite of components to generate previews for a post for both social and search engines.

如何安装 @automattic/social-previews?

在项目目录运行 npm install @automattic/social-previews,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^18.0.0 || ^19.0.0)、react-dom(^18.0.0 || ^19.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@automattic/social-previews 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/automattic__social-previews 是否存在。

@automattic/social-previews 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@automattic/social-previews 可以商用吗?

@automattic/social-previews 使用 GPL-2.0-or-later 许可。强 Copyleft:分发包含该代码的软件时,整个作品需以兼容的 GPL 许可开源。仅在内部使用、不对外分发时不触发此义务。以上为概括,不构成法律意见,请以许可原文为准。

@automattic/social-previews 的下载量有多少?

根据 npm 官方下载统计,@automattic/social-previews 在 2026/10/07 – 2026/11/05 这 30 天内被下载 72,150 次,日均约 2,405 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@automattic/social-previews 还在维护吗?

最新版本 3.4.2 发布于 2026/11/03(5 天前);至今共发布 93 个版本,其中过去 12 个月发布了 46 个;首次发布于 2020/07/22。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。