项目作者: syntra

项目描述 :
Gatsby remark plugin for generating social card graphics
高级语言: JavaScript
项目地址: git://github.com/syntra/gatsby-remark-social-cards.git
创建时间: 2018-09-13T21:48:57Z
项目社区:https://github.com/syntra/gatsby-remark-social-cards

开源协议:MIT License

下载







Elevator Pitch

Do you wish your Gatsby blog posts could look like this when getting shared to Twitter?

custom twitter blog post card

With this plugin, they can!

gatsby-remark-social-cards iterates over your markdown files and automatically generates graphical representations of the frontmatter data! It’s highly customizable and can help increase your click rates.

Features/Roadmap

Greenkeeper badge

  • Generates Twitter Card
  • Generates Facebook Card
  • Custom Background Colors
  • Custom Background Image
  • Custom Font Colors (currently supports white and black) See #1
  • Custom Font Family (currently only supports DejaVuSansCondensed)
  • Custom Font Size
  • Custom Font Style (normal, italic, and bold)
  • Custom Font Positioning
  • Custom Metadata Strings
  • Watermark/Logo Support
  • Stackable Text Effects (opacity, drop shadow, stroke, pattern overlay)
  • Stackable Background Shapes (rect, circle, polygon)
  • Automatic Injection of Required <meta/> Tags

Installation

Prerequisites

It’s recommended that you use gatsby-plugin-react-helmet for managing the <head></head> of your website. This plugin only generates the card images, it’s your responsibility to add the <meta/> tags to the <head></head> of your layout (step 4).

The URLs set in the <meta/> tags also needs to be absolute paths. So make sure that you have siteUrl set in your gatsby-config.js:

  1. siteMetadata: {
  2. title: "My blog title",
  3. siteUrl: "https://mydomain.com", // no trailing slash!
  4. }
  1. Install the plugin
  1. yarn add gatsby-remark-social-cards
  1. Add to your gatsby-transformer-remark plugins in gatsby-config.js
  1. plugins: [
  2. // ...
  3. {
  4. resolve: `gatsby-transformer-remark`,
  5. options: {
  6. plugins: [
  7. `gatsby-remark-social-cards`,
  8. // ...
  9. ],
  10. },
  11. },
  12. ],
  1. Restart Gatsby

  2. Add the <meta/> tags in the head

Note: it’s typically recommended to have your <Helmet></Helmet> section inside your main layout component.

Add a prop for pathname to your layout component and use that along with the siteUrl to get the absolute path to the twitter card.

  1. const Layout = ({ pathname, children }) => (
  2. <StaticQuery
  3. query={graphql`
  4. query SiteTitleQuery {
  5. site {
  6. siteMetadata {
  7. title
  8. siteUrl
  9. }
  10. }
  11. }
  12. `}
  13. render={data => (
  14. <Helmet title={data.site.siteMetadata.title}>
  15. <meta name="twitter:card" content="summary_large_image" />
  16. <meta
  17. name="twitter:image"
  18. content={`${data.site.siteMetadata.siteUrl}${pathname}twitter-card.jpg`}
  19. />
  20. </Helmet>
  21. { /* ... */ }
  22. )}
  23. />
  24. );

Then inside your blog post template, pass location.pathname to the layout.

Note: router’s location object is passed to every page in Gatsby

  1. export default ({ location }) => {
  2. return <Layout pathname={location.pathname}>{/* ... */}</Layout>;
  3. };
  1. There are additional meta tags that can and should be used, although the ones I showed above are the only ones required to properly see the card image. For more information, see the official docs for large summary cards

Configuration

I built this plugin to be as flexible as possible, so at first the config may seem daunting. But keep in mind, none of these settings are required. You can add as few or as much configuration as you desire. All values shown below are default.

  1. plugins: [
  2. // ...
  3. {
  4. resolve: `gatsby-transformer-remark`,
  5. options: {
  6. plugins: [
  7. {
  8. resolve: `gatsby-remark-social-cards`,
  9. options: {
  10. title: {
  11. // This is the frontmatter field the title should come from
  12. field: "title",
  13. // Currently only supports DejaVuSansCondensed
  14. // More fonts coming soon!
  15. font: "DejaVuSansCondensed",
  16. color: "black", // black|white
  17. size: 48, // 16|24|32|48|64
  18. style: "bold", // normal|bold|italic
  19. x: null, // Will default to xMargin
  20. y: null, // Will default to yMargin
  21. },
  22. meta: {
  23. // The parts array is what generates the bottom text
  24. // Pass an array with strings and objects
  25. // The following array will generate:
  26. // "- Author Name » September 13"
  27. // The objects are used to pull data from your markdown's
  28. // frontmatter. { field: "author" } pulls the author set
  29. // in the frontmatter. { field: "category" } would pull
  30. // the category set. Any field can be used as parts
  31. // Note: Only pass the "format" property on date fields
  32. parts: [
  33. "- ",
  34. { field: "author" },
  35. " » ",
  36. { field: "date", format: "mmmm dS" },
  37. ],
  38. // Currently only supports DejaVuSansCondensed
  39. // More fonts coming soon!
  40. font: "DejaVuSansCondensed",
  41. color: "black", // black|white
  42. size: 24, // 16|24|32|48|64
  43. style: "normal", // normal|bold|italic
  44. x: null, // Will default to xMargin
  45. y: null, // Will default to cardHeight - yMargin - size
  46. },
  47. background: "#FFFFFF", // Background color for the card
  48. xMargin: 24, // Edge margin used when x value is not set
  49. yMargin: 24,// Edge margin used when y value is not set
  50. }
  51. }
  52. // ...
  53. ],
  54. },
  55. },
  56. ],

Contributing

This plugin is in it’s early stages, so any and all help is warmly welcomed!

  • ⇄ Pull/Merge requests and ★ Stars are always welcome!
  • For bugs and feature requests, please create an issue.

Changelog

See CHANGELOG.md

License

This project is licensed under the MIT License - see the
LICENCE.md file for details