Last active
May 5, 2025 01:52
-
-
Save disktnk/062ed0a660ef6d0ad96b82bd56b551e2 to your computer and use it in GitHub Desktop.
Hugo example: embed HTML to show like Twitter summary card,
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.embed-card { | |
display: flex; | |
flex-direction: row; | |
align-items: stretch; | |
max-width: 500px; | |
border: 1px solid #ddd; | |
border-radius: 12px; | |
overflow: hidden; | |
text-decoration: none; | |
margin-bottom: 1.5rem; | |
} | |
.embed-card img { | |
display: block; | |
width: 120px; | |
max-height: 140px; | |
margin: 0; | |
object-fit: cover; | |
border-radius: 0; | |
} | |
.embed-card-content { | |
flex: 1; | |
min-width: 0; | |
padding: 10px; | |
display: flex; | |
flex-direction: column; | |
justify-content: center; | |
} | |
.embed-card-content .title { | |
font-size: 0.8em; | |
font-weight: bold; | |
margin-bottom: 5px; | |
white-space: nowrap; | |
overflow: hidden; | |
text-overflow: ellipsis; | |
max-width: 360px; | |
} | |
.embed-card-content .description { | |
font-size: 0.7em; | |
margin-bottom: 5px; | |
display: -webkit-box; | |
-webkit-line-clamp: 2; | |
-webkit-box-orient: vertical; | |
overflow: hidden; | |
text-overflow: ellipsis; | |
} | |
.embed-card-content .url { | |
font-size: 0.7em; | |
} | |
.embed-card-content:hover { | |
background-color:rgb(106, 161, 200); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- retrieve OG and convert to embed HTML --> | |
{{- $url := .Get "url" -}} | |
{{- $html := "" -}} | |
{{- $og_title := .Get "og_title" -}} | |
{{- if eq $og_title "" -}} | |
<!-- try to get OGP information from source --> | |
{{- with try ($res := resources.GetRemote $url) -}} | |
{{- with .Err -}} | |
{{- warnf "%s" . -}} | |
{{- else with .Value -}} | |
{{- $html = $res.Content -}} | |
{{- else -}} | |
{{- warnf "Failed to get remote resource: %q" $url -}} | |
{{- end -}} | |
{{- end -}} | |
{{- $og_title = findRE `<meta property="og:title" content="(.*?)"` $html 1 -}} | |
{{- if gt (len $og_title) 0 -}} | |
{{- $og_title = index $og_title 0 | replaceRE `<meta property="og:title" content="|"` "" -}} | |
{{- end -}} | |
{{- end -}} | |
<!-- if get og:title, show card, else show simple linked title --> | |
{{- if ne $og_title "" -}} | |
{{- $og_description := .Get "og_description" -}} | |
{{- if eq $og_description "" -}} | |
{{- $og_description = findRE `<meta property="og:description" content="(.*?)"` $html 1 -}} | |
{{- $og_description = index $og_description 0 | replaceRE `<meta property="og:description" content="|"` "" -}} | |
{{- end -}} | |
{{- $og_image := .Get "og_image" -}} | |
{{- if eq $og_image "" -}} | |
{{- $og_image = findRE `<meta property="og:image" content="(.*?)"` $html 1 -}} | |
{{- $og_image = index $og_image 0 | replaceRE `<meta property="og:image" content="|"` "" -}} | |
{{- end -}} | |
{{- $host := urls.Parse $url -}} | |
<a href="{{ $url }}" target="_blank"> | |
<div class="embed-card"> | |
<img src="{{ $og_image }}" alt="{{ $og_title }}"> | |
<div class="embed-card-content"> | |
<div class="title">{{ $og_title | safeHTML }}</div> | |
<div class="description">{{ $og_description | safeHTML }}</div> | |
<div class="url">🔗{{ $host.Host }}</div> | |
</div> | |
</div> | |
{{- else -}} | |
{{- $title := findRE `<title>(.*?)</title>` $html -}} | |
{{- $title = index $title 0 | replaceRE `<title>|</title>` "" -}} | |
<b>{{ $title | safeHTML }}</b> | |
{{- end -}} | |
</a> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{{- if .HasShortcode "embed" -}} | |
<link rel="stylesheet" href="{{- .Site.BaseURL -}}css/embed.css"> | |
{{- end -}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment