hugoでリンクカードを作る

hugoでリンクカードを作る

すごく参考にした記事

半分というかほぼ全部こちらを流用しています。
上の記事にいいねとかしてください!!(マジで)

お前は何をした

情報が古く使っているメソッドの仕様が変わったためそれへの対応、tailwindcssではなくプレーンなCSSを使うように変更を加えました。

コード

{{- $url := (.Get 0) -}}
{{- $u := urls.Parse $url -}}
{{- $opts := dict "headers" (dict "Accept-Language" "ja") -}}

{{- with try (resources.GetRemote $url $opts) -}}
  {{- with .Err -}}
    {{- errorf "Failed to get remote resource %q: %s" $url . -}}
  {{- else with .Value -}}
    {{- $result := . -}}
    {{- $title := "" -}}
    {{- $description := "" -}}
    {{- $image := "" -}}
    {{- $find := "" -}}

    {{- $find = index (findRE "<head.*?>(.|\n)*?</head>" $result.Content) 0 -}}

    {{- range $meta := findRE "<meta.*?>" $find -}}
      {{- $name := replaceRE "<.*name=\"(.*?)\".*>" "$1" $meta -}}
      {{- $property := replaceRE "<.*property=\"(.*?)\".*>" "$1" $meta -}}
      {{- $content := replaceRE "<.*content=\"(.*?)\".*>" "$1" $meta -}}

      {{- if eq $property "og:title" -}}
        {{- $title = $content -}}
      {{- else if eq $property "og:description" -}}
        {{- $description = $content -}}
      {{- else if eq $property "og:image" -}}
        {{- $image = $content -}}
      {{- end -}}
      {{- if and (eq $description "") (eq $name "description") -}}
        {{- $description = $content -}}
      {{- end -}}
    {{- end -}}

    <div class="web-card default-card">
      <a href="{{ $url }}" class="card-link" target="_blank">
        <div class="image-container">
          {{ with $image }} 
            <div class="card-image-bg" style="background-image: url('{{ htmlUnescape $image }}');"></div>
          {{ else }}
            <div class="image-placeholder default-image-placeholder">
              <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="icon-placeholder">
                <path stroke-linecap="round" stroke-linejoin="round" d="M2.25 15.75l5.159-5.159a2.25 2.25 0 013.182 0l5.159 5.159m-1.5-1.5l1.409-1.409a2.25 2.25 0 013.182 0l2.909 2.909m-18 3.75h16.5a1.5 1.5 0 001.5-1.5V6a1.5 1.5 0 00-1.5-1.5H3.75A1.5 1.5 0 002.25 6v12a1.5 1.5 0 001.5 1.5zm10.5-11.25h.008v.008h-.008V8.25zm.375 0a.375.375 0 11-.75 0 .375.375 0 01.75 0z" />
              </svg>
            </div>
          {{ end }}
        </div>
        
        <div class="text-content default-text-content">
          <div class="text-group">
            <div class="title-text default-title-text">
              {{ with $title }} 
                {{ $title | htmlUnescape }}
              {{ else }} 
                {{ $u.Host | truncate 30 }}
              {{ end }}
            </div>
            <div class="description-text">
              {{ $description | htmlUnescape | truncate 100 }}
            </div>
          </div>
          
          <div class="footer-group">
            <div class="domain-info">
              <img src="https://www.google.com/s2/favicons?sz=14&domain_url={{ $u.Scheme }}://{{ $u.Host }}" class="fav-icon" alt="{{ $u.Host }} favicon image">
              <span class="domain-text">{{ $u.Host | truncate 30 }}</span>
            </div>
          </div>
        </div>
      </a>
    </div>
  {{- else -}}
    <div class="web-card error-card">
      <div class="card-content-wrapper">
        <div class="image-placeholder error-image-placeholder">
          <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="icon-placeholder">
            <path stroke-linecap="round" stroke-linejoin="round" d="M18.364 18.364A9 9 0 005.636 5.636m12.728 12.728A9 9 0 015.636 5.636m12.728 12.728L5.636 5.636" />
          </svg>       
        </div>

        <div class="text-content error-text-content">
          <div class="text-group">
            <div class="title-text error-title-text">
              {{ $u.Host | truncate 30 }} にアクセスできません
            </div>
          </div>
          
          <div class="footer-group">
            <div class="domain-info">
              <img src="https://www.google.com/s2/favicons?sz=14&domain_url={{ $u.Scheme }}://{{ $u.Host }}" class="fav-icon" alt="{{ $u.Host }} favicon image">
              <span class="domain-text">{{ $u.Host | truncate 30 }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  {{- end -}}
{{- else -}}
  {{- errorf "Unable to get remote resource %q" $url -}}
{{- end -}}

CSSは

:root {
    --card-bg: #ffffff;
    --card-border: #e5e7eb;
    --card-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06);
    --card-shadow-hover: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
    --text-primary: #111827;
    --text-secondary: #6b7280;
    --text-muted: #9ca3af;
    --error-bg: #fef2f2;
    --error-border: #fecaca;
    --error-text: #dc2626;
}

/* ウェブカードの基本スタイル */
.web-card {
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: 12px;
    box-shadow: var(--card-shadow);
    transition: all 0.2s ease-in-out;
    overflow: hidden;
    margin: 1.5rem 0;
    position: relative;
}

.web-card:hover {
    box-shadow: var(--card-shadow-hover);
    transform: translateY(-1px);
}

/* カードリンク */
.card-link {
    display: flex;
    text-decoration: none;
    color: inherit;
    height: 100%;
}

.card-link:hover {
    box-shadow: 0 4px 16px -2px rgba(59, 130, 246, 0.15);
}

/* カードコンテンツラッパー */
.card-content-wrapper {
    display: flex;
    width: 100%;
}

/* 画像コンテナ */
.image-container {
    flex-shrink: 0;
    width: 200px;
    height: 140px;
    position: relative;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
}

.card-image-bg {
    width: 100%;
    height: 100%;
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    border-radius: 0;
    transition: transform 0.2s ease-in-out;
}

.web-card:hover .card-image-bg {
    transform: scale(1.05);
}

/* 画像プレースホルダー */
.image-placeholder {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #f3f4f6 0%, #e5e7eb 100%);
    color: var(--text-muted);
}

.default-image-placeholder {
    background: linear-gradient(135deg, #f3f4f6 0%, #e5e7eb 100%);
}

.error-image-placeholder {
    background: linear-gradient(135deg, #fef2f2 0%, #fecaca 100%);
    color: var(--error-text);
}

/* プレースホルダーアイコン */
.icon-placeholder {
    width: 2.5rem;
    height: 2.5rem;
    opacity: 0.6;
}

/* テキストコンテンツ */
.text-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    padding: 1.25rem;
    min-height: 140px;
}

/* テキストグループ */
.text-group {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

/* タイトル */
.title-text {
    font-size: 1.5rem;
    font-weight: 700;
    line-height: 1.5;
    color: #111827;
    margin: 0;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
    word-break: break-word;
}

/* 説明文 */
.description-text {
    font-size: 1.125rem;
    line-height: 1.7;
    color: #4b5563;
    margin: 0;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
    word-break: break-word;
}

/* フッター */
.footer-group {
    margin-top: auto;
    padding-top: 0.75rem;
}

.footer-group a, .footer-group .domain-info {
    color: #3b82f6 !important;
}

/* ドメイン情報 */
.domain-info {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

/* ファビコン */
.fav-icon {
    width: 16px;
    height: 16px;
    border-radius: 2px;
    flex-shrink: 0;
}

/* ドメインテキスト */
.domain-text {
    font-size: 1rem;
    font-weight: 500;
    color: #9ca3af;
    text-transform: lowercase;
}

/* エラーカード */
.error-card {
    border-color: var(--error-border);
    background: var(--error-bg);
    cursor: not-allowed;
}

.error-card:hover {
    transform: none;
    box-shadow: var(--card-shadow);
}

.error-text-content {
    background: var(--error-bg);
}

.error-title-text {
    color: var(--error-text);
}

/* レスポンシブデザイン */
@media screen and (max-width: 768px) {
    .image-container {
        width: 120px;
        height: 120px;
    }
    
    .text-content {
        padding: 1rem;
        min-height: 120px;
    }
    
    .title-text {
        font-size: 1.2rem;
    }
    
    .description-text {
        font-size: 1rem;
    }
    
    .domain-text {
        font-size: 0.95rem;
    }
}

@media screen and (max-width: 480px) {
    .web-card {
        margin: 1rem 0;
    }
    
    .image-container {
        width: 100px;
        height: 100px;
    }
    
    .text-content {
        padding: 0.875rem;
        min-height: 100px;
    }
    
    .title-text {
        font-size: 1rem;
        -webkit-line-clamp: 1;
    }
    
    .description-text {
        font-size: 0.875rem;
        -webkit-line-clamp: 1;
    }
    
    .domain-text {
        font-size: 0.8rem;
    }
}

こんなものです。はい。

使用する

((< webcard "https://www.youtube.com/watch?v=gsqRwAOfOnw" >))

(){}に変えてね。

おわり

以上です。重ねて参考にした記事の著者にSUGOI KANSHAしています。マジでありがとうございます!!

ってことでまたね~

Licensed under CC BY-NC-SA 4.0
Hugo で構築されています。
テーマ StackJimmy によって設計されています。