Giter Club home page Giter Club logo

metalsmith-twitter-card's Introduction

metalsmith-twitter-card

npm version npm

A Metalsmith plugin that adds Twitter Card meta tags to enable Twitter share rich snippets. Plugin supports all twitter card types and validates file metadata to have all required properties.

Installation

$ npm install --save-dev metalsmith-twitter-card

Usage

Place metalsmith-twitter-card plugin after html files generation, for example after metalsmith-layouts.

const Metalsmith  = require('metalsmith');
const twitterCard = require('metalsmith-twitter-card');

Metalsmith(__dirname)
  .use(twitterCard({
    siteurl: 'my-site.com'
  }));

To enable twitter tags generation add twitter to frontmatter with required sub-properties. Or you can just set twitter: true to use default defined in plugin options. Properties cound be just a string value, css selector (only class and id supported), or metadata property name. Example:

---
title: Hello World
description: My pretty description.
twitter:
  card: summary            // Sets card type to `summary`
  site: '@myaccount'       // Sets twitter account to `@myaccount`
  title: '#page-title'     // Will look for element with id `page-title`
                           // and take its text content as twitter card title.
  description: description // Will look for a description property in file frontmatter,
                           // if nothing founded in it, will look for this property in plugin options.
---

To disable twitter tags generation for particular file just ommit twitter property in frontmatter.

Options

You can pass any twitter card properties to plugin to set default values. If file contain properties, it will use properties from file, override defaults. Example:

const Metalsmith  = require('metalsmith');
const twitterCard = require('metalsmith-twitter-card');

Metalsmith(__dirname)
  .use(twitterCard({
    siteurl: 'my-site.com',
    card: 'summary_large_image', // Sets default card type.
    site: '@myaccount',          // Sets default twitter username.
    title: 'title',              // By default will use title property from file metadata as twitter card title.
    description: 'description'   // By default will use description property
                                 // from file metadata as twitter card description.
  }));

In file you can override defaults, for example:

 ---
 title: Hello World
 description: My pretty description.
 twitter:
   card: summary            // Will override default card type `summary_large_image` with `summary`
   site: '@another-account' // Will override default twitter account `@myaccount` with `@another-account`
 ---

siteurl - Required

Type: String

Default: ''

Your site url, used to generate absolute path for images.

CLI

You can also use the plugin with the Metalsmith CLI by adding a key to your metalsmith.json file:

{
  "plugins": {
    "metalsmith-twitter-card": {
      "siteurl": "my-site.com"
    }
  }
}

License

MIT

metalsmith-twitter-card's People

Contributors

vitaliy-bobrov avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

metalsmith-twitter-card's Issues

isSelector is Overly Aggressive

isSelector() as defined here:

const isSelector = value => value[0] === '.' || value[0] === '#';

Incorrectly returns true for titles or descriptions such as ...lorem ipsum that start with an ellipsis. It also doesn't allow for tag names to come first in the selector.

Maybe a better way would be to check el to see if it found any results here:

let el = $cheerio(value);

High Dependency Vulnerability: cheerio

From npm audit:

┌───────────────┬──────────────────────────────────────────────────────────────┐
│ High          │ Prototype Pollution                                          │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Package       │ lodash.merge                                                 │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Dependency of │ metalsmith-twitter-card                                      │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Path          │ metalsmith-twitter-card > cheerio > lodash.merge             │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ More info     │ https://npmjs.com/advisories/1066                            │
└───────────────┴──────────────────────────────────────────────────────────────┘

cheerio/htmlparser2 decodeEntities:false Mangles HTML Attribute Values

Issue broken out from my follow-up comments in #1.


The cheerio upgrade in v1.0.5 combined with decodeEntities: false causes HTML such as:

<img src="static/img/blog/what-is-the-jamstack.png" alt="What is the &quot;JAMstack&quot;?" class="w-100 og-image">

to be mangled to:

<img src="static/img/blog/what-is-the-jamstack.png" alt="What is the " jamstack"?" class="w-100 og-image">

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.