gadenbuie / metathis
1

2
#' Social Media Metadata
3
#'
4
#' Generate metadata tags for social media cards.
5
#'
6
#' @template describe-meta
7
#' @param title Content title
8
#' @param description Content description. If you have already used
9
#'   [meta_general()] to set the content's description, that description will
10
#'   automatically be used.
11
#' @param url Content URL
12
#' @param image Image url for card.
13
#' @param image_alt A description of what's in the image (not a caption)
14
#' @param image_width The width of the image in pixels
15
#' @param image_height The height of the image in pixels
16
#' @param og_site_name The name of the site hosting the content
17
#' @param og_type Open Graph card type, default is `"website"`. Other common
18
#'   options include `"article"`, `"book"`, or `"profile"`. The full list of
19
#'   valid options can be referenced at <https://ogp.me/>
20
#' @param og_locale The locale these tags are marked up in. Of the format `language_TERRITORY`. Default is `"en_US"`.
21
#' @param og_author Writers of the article. Multiple authors may be specified in
22
#'   a vector of character strings.
23
#' @param twitter_card_type One of `"summary"`, `"summary_large_image"`,
24
#'   `"app"`, or `"player"`.
25
#' @param twitter_creator `@username` for the content creator / author.
26
#' @param twitter_site `@username`` for the website used in the card footer.
27
#'   `twitter_creator` is used by default.
28
#' @param facebook_app_id The Facebook app ID. See the
29
#'   [Facebook Open Graph Markup](https://developers.facebook.com/docs/sharing/webmasters#markup)
30
#'   page for more information.
31
#' @param disable_pinterest If `TRUE`, adds a metadata tag disabling pins from
32
#'   your website. See the
33
#'   [Pinterest help center](https://help.pinterest.com/en/business/article/prevent-saves-to-pinterest-from-your-site)
34
#'   for more information.
35
#'
36
#' @references
37
#'   - [Open Graph](https://ogp.me/)
38
#'   - [Google Structured Data Testing Tool](https://search.google.com/structured-data/testing-tool)
39
#'   - [Facebook Sharing Debugger](https://developers.facebook.com/tools/debug/)
40
#'   - [Twitter Card Documentation](https://developer.twitter.com/en/docs/twitter-for-websites/cards/guides/getting-started)
41
#'   - [Twitter Card Validator](https://cards-dev.twitter.com/validator)
42
#'
43
#' @template describe-meta-return
44
#' @examples
45
#' meta() %>%
46
#'   meta_social(
47
#'     title = "R for Data Science",
48
#'     description = "This book with teach you how to do data science with R",
49
#'     url = "https://r4ds.had.co.nz",
50
#'     image = "https://r4ds.had.co.nz/cover.png",
51
#'     image_alt = "The cover of the R4DS book",
52
#'     og_type = "book",
53
#'     og_author = c("Garrett Grolemund", "Hadley Wickham"),
54
#'     twitter_card_type = "summary",
55
#'     twitter_creator = "@hadley"
56
#'   )
57
#'
58
#' @export
59
meta_social <- function(
60
  .meta = meta(),
61
  title = NULL,
62
  url = NULL,
63
  image = NULL,
64
  image_alt = NULL,
65
  image_width = NULL,
66
  image_height = NULL,
67
  description = NULL,
68
  twitter_card_type = c("summary", "summary_large_image", "app", "player"),
69
  twitter_creator = NULL,
70
  twitter_site = twitter_creator,
71
  og_type = "website",
72
  og_locale = "en_US",
73
  og_author = NULL,
74
  og_site_name = NULL,
75
  facebook_app_id = NULL,
76
  disable_pinterest = FALSE
77
) {
78 10
  assert_is_meta(.meta)
79 10
  twitter_card_type <- match.arg(twitter_card_type)
80

81 10
  description <- description %||% meta_find_description(.meta)
82

83 10
  social <- list(
84 10
    "twitter:title"       = title,
85 10
    "twitter:description" = description,
86 10
    "twitter:url"         = url,
87 10
    "twitter:image:src"   = image,
88 10
    "twitter:image:alt"   = image_alt,
89 10
    "twitter:image:width" = image_width,
90 10
    "twitter:image:height"= image_height,
91 10
    "twitter:card"        = twitter_card_type,
92 10
    "twitter:creator"     = twitter_creator,
93 10
    "twitter:site"        = twitter_site,
94 10
    "og:title"            = title,
95 10
    "og:description"      = description,
96 10
    "og:url"              = url,
97 10
    "og:image"            = image,
98 10
    "og:image:alt"        = image_alt,
99 10
    "og:image:width"      = image_width,
100 10
    "og:image:height"     = image_height,
101 10
    "og:type"             = og_type,
102 10
    "og:locale"           = og_locale,
103 10
    "og:site_name"        = og_site_name,
104 10
    "article:author"      = og_author %??% as.list(og_author),
105 10
    "fb:app_id"           = facebook_app_id
106
  )
107

108 10
  meta_social <-
109 10
    social %>%
110 10
    duplicate_vector_entries() %>%
111 10
    collapse_single_string() %>%
112 10
    purrr::imap(function(content, property) {
113 10
      if (grepl("^twitter:", property)) {
114 10
        tag_meta(name = property, content = content)
115
      } else {
116 10
        tag_meta(property = property, content = content)
117
      }
118
    }) %>%
119 10
    unname()
120

121 10
  if (disable_pinterest) {
122 10
    meta_social <- c(
123 10
      meta_social,
124 10
      list(tag_meta(
125 10
        name = "pinterest",
126 10
        content = "nopin",
127 10
        description = "Sorry, pins from this website are disabled."
128
      ))
129
    )
130
  }
131

132 10
  append_to_meta(.meta, meta_social)
133
}

Read our documentation on viewing source code .

Loading