Skip to content

Instantly share code, notes, and snippets.

@mccarthy-m-g
Last active May 15, 2023 22:05
Show Gist options
  • Save mccarthy-m-g/00f755efa6e983a3e0fc06b16ab30bb3 to your computer and use it in GitHub Desktop.
Save mccarthy-m-g/00f755efa6e983a3e0fc06b16ab30bb3 to your computer and use it in GitHub Desktop.
Embed Mastodon posts in Quarto documents
.callout-mastodon {
border-left-color: #6364ff !important;
}
.callout-header {
border-bottom: none;
font-size: .9rem;
font-weight: 600;
opacity: 75%;
}
div.callout-mastodon.callout-titled .callout-icon::before {
background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" style="fill: %236364ff" class="fab fa-mastodon" viewBox="0 0 448 512"><path d="M433 179.11c0-97.2-63.71-125.7-63.71-125.7-62.52-28.7-228.56-28.4-290.48 0 0 0-63.72 28.5-63.72 125.7 0 115.7-6.6 259.4 105.63 289.1 40.51 10.7 75.32 13 103.33 11.4 50.81-2.8 79.32-18.1 79.32-18.1l-1.7-36.9s-36.31 11.4-77.12 10.1c-40.41-1.4-83-4.4-89.63-54a102.54 102.54 0 0 1-.9-13.9c85.63 20.9 158.65 9.1 178.75 6.7 56.12-6.7 105-41.3 111.23-72.9 9.8-49.8 9-121.5 9-121.5zm-75.12 125.2h-46.63v-114.2c0-49.7-64-51.6-64 6.9v62.5h-46.33V197c0-58.5-64-56.6-64-6.9v114.2H90.19c0-122.1-5.2-147.9 18.41-175 25.9-28.9 79.82-30.8 103.83 6.1l11.6 19.5 11.6-19.5c24.11-37.1 78.12-34.8 103.83-6.1 23.71 27.3 18.4 53 18.4 175z"/></svg>');
}
.callout-mastodon-username {
color: inherit;
text-decoration: none;
}
.callout-body {
padding-left: 1.6em;
margin-top: .2em;
font-size: .9rem;
font-weight: 400;
}
.callout-footer {
font-size: .825em;
margin-top: 1.25rem;
align-items: center;
}
#' Embed a Mastodon post in Quarto
#'
#' Embeds a Mastodon post in the style of a Quarto callout block using
#' {rtoot} and {htmltools}. No API key required.
#'
#' @param id Character. Local ID of a post.
#' @param instance Character. Server name of the post's author.
#' @return
#' A `list()` with a `shiny.tag` class that can be converted into an HTML string
#' via `as.character()` and saved to a file with `htmltools::save_html()`.
embed_post <- function(id, instance) {
post <- rtoot::get_status(id = id, instance = instance, anonymous = TRUE)
# Post author's account
account <- post$account[[1]]
author <- htmltools::tags$a(
href = account$url,
target = "_blank",
class = "callout-mastodon-username",
paste0(account$display_name, " @", account$username, "@", instance)
)
# Post content
content <- htmltools::HTML(post$content)
# Post date and time
created_at <- trimws(format(post$created_at, "%l:%M %P - %B%e, %Y (%Z)"))
created_at <- sub("am", "AM", created_at)
created_at <- sub("pm", "PM", created_at)
created_at <- htmltools::tags$a(href = post$url, target = "_blank", created_at)
htmltools::tags$div(
class = "callout callout-style-simple callout-mastodon callout-titled",
cite = post$url,
# Post author
htmltools::tags$div(
class = "callout-header d-flex align-content-center",
htmltools::tags$div(
class = "callout-icon-container",
htmltools::tag("i", list(class = "callout-icon"))
),
htmltools::tags$div(
class = "callout-title-container flex-fill",
author
)
),
# Post content
htmltools::tags$div(
class = "callout-body-container callout-body",
content,
htmltools::tags$div(
class = "callout-footer",
created_at
)
)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment