Skip to content

Commit adf76ad

Browse files
author
Carlos Fenollosa
committed
Added support for configuring a Twitter Card default image
1 parent c126913 commit adf76ad

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

bb.sh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ global_variables() {
4949

5050
# Change this to your username if you want to use twitter for comments
5151
global_twitter_username=""
52+
# Default image for the Twitter cards. Use an absolute URL
53+
global_twitter_card_image=""
5254
# Set this to false for a Twitter button with share count. The cookieless version
5355
# is just a link.
5456
global_twitter_cookieless="true"
@@ -345,8 +347,17 @@ twitter_card() {
345347
echo "<meta name='twitter:title' content='$2' />" # Twitter truncates at 70 char
346348
description=$(grep -v "^<p>$template_tags_line_header" "$1" | sed -e 's/<[^>]*>//g' | tr '\n' ' ' | sed "s/\"/'/g" | head -c 250)
347349
echo "<meta name='twitter:description' content=\"$description\" />"
348-
image=$(sed -n '2,$ d; s/.*<img.*src="\([^"]*\)".*/\1/p' "$1") # First image is fine
350+
351+
# For the image we try to locate the first image in the article
352+
image=$(sed -n '2,$ d; s/.*<img.*src="\([^"]*\)".*/\1/p' "$1")
353+
354+
# If none, then we try a global setting image
355+
[[ -z $image ]] && [[ -n $global_twitter_card_image ]] && image=$global_twitter_card_image
356+
357+
# If none, return
349358
[[ -z $image ]] && return
359+
360+
# Final housekeeping
350361
[[ $image =~ ^https?:// ]] || image=$global_url/$image # Check that URL is absolute
351362
echo "<meta name='twitter:image' content='$image' />"
352363
}

0 commit comments

Comments
 (0)