Skip to content

リンクカード フォールバック処理を追加#9958

Open
mousu-a wants to merge 6 commits into
mainfrom
bug/fix-link-card-expansion-opengraph-fallback
Open

リンクカード フォールバック処理を追加#9958
mousu-a wants to merge 6 commits into
mainfrom
bug/fix-link-card-expansion-opengraph-fallback

Conversation

@mousu-a

@mousu-a mousu-a commented Apr 28, 2026

Copy link
Copy Markdown
Contributor

Issue

概要

このPRはhttps://ja.wikipedia.org/wiki/日本語のURLがリンクカードとして展開できないバグを修正するものです。
リンクカードでOGPを取得する際、OGP取得に使っているGemがnilを返す場合のフォールバック処理を追加しました。

変更確認方法

  1. bug/fix-link-card-expansion-opengraph-fallbackをローカルに取り込み、チェックアウト
  2. bin/devでサーバーを起動
  3. 日報を作成(ログインしていなければログイン 誰でも良いです)
  4. 以下テキストを"内容"に貼り付けする
@[card](https://ja.wikipedia.org/wiki/日本語)
  1. プレビューにて、リンクカードが正常に展開されることを確認

Screenshot

変更前

image

変更後

image

Summary by CodeRabbit

リリースノート

  • バグ修正
    • リンクカードのメタデータ取得の信頼性を向上しました。OpenGraphタグが利用できない場合、メタタグやHTMLのタイトルなど他のメタデータソースから自動的にタイトル、説明、画像、サイト名を取得するようになりました。

@coderabbitai

coderabbitai Bot commented Apr 28, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

OpenGraphReader の parse 結果が nil の場合に、meta タグから title、description、images、site_name を抽出する fallback パスを追加。favicon 抽出ロジックも調整され、test ファイルで empty/missing og:url シナリオのカバレッジが拡張。

Changes

Cohort / File(s) Summary
Metadata Fallback Logic
app/models/metadata.rb
OpenGraphReader.parse が nil を返す場合に metadata_fallback メソッドを呼び出し、OpenGraph/Twitter meta タグから title、description、images、site_name を抽出。HTML <title>meta[name="description"]/meta[name="application-name"] をバックアップとして使用。favicon 抽出で element['href'] への変更も実施。
System Test Coverage
test/system/markdown_test.rb
link-card 展開のシステムテストを拡張。既存テストを og:url empty シナリオ用に更新し、og:url missing シナリオの別テストケースを追加。両ケースで link card 表示とエラーなし挙動を検証。

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related issues

Possibly related PRs

Suggested reviewers

  • komagata
  • okuramasafumi

Poem

🐰 メタデータが無くても心配なし
フォールバック道で光を見つけ
タグから掘り出す宝の情報
空っぽな og:url も今は大丈夫
テスト増やして堅牢に進む!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description check ✅ Passed PR説明は必須セクション(Issue番号、概要、変更確認方法、スクリーンショット)をすべて含んでおり、ほぼ完全です。
Title check ✅ Passed PRのタイトル「リンクカード フォールバック処理を追加」は、変更の主な内容(OpenGraphパースが失敗した場合のフォールバック処理の追加)を正確に反映しており、目的と一致している。

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch bug/fix-link-card-expansion-opengraph-fallback

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions github-actions Bot requested a review from okuramasafumi April 28, 2026 23:52
@mousu-a mousu-a changed the title 特定のURLのリンクカードが展開されないバグを修正 リンクカードにフォールバック処理を追加 Apr 28, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@app/models/metadata.rb`:
- Around line 67-85: The fallback_* helpers stop the fallback chain when a meta
tag returns an empty string because they use ||; change each method
(fallback_title, fallback_description, fallback_images, fallback_site_name) to
call .presence on the card_content() result (and on the secondary extraction if
needed) so empty strings are treated as nil and the chain continues (e.g. use
card_content(doc, 'title').presence ||
doc.at_css('title')&.text&.strip.presence). Update fallback_title,
fallback_description, fallback_images, fallback_site_name and keep card_content
as-is.

In `@test/system/markdown_test.rb`:
- Line 155: The assertion uses the wrong href: replace the hardcoded selector
string 'a.before-replacement-link-card[href="https://bootcamp.fjord.jp/"]' in
the test (test/system/markdown_test.rb) with the actual URL used as input in
this test (e.g. 'https://ja.wikipedia.org/wiki/日本語') or reference the
variable/fixture that holds the test URL so assert_no_selector checks the same
link the test inserted; ensure the selector matches
'a.before-replacement-link-card[href="https://ja.wikipedia.org/wiki/日本語"]' (or
the variable) so the test fails if the pre-replacement link remains.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: cf1544f4-50a5-4f8e-a347-6f46dc792463

📥 Commits

Reviewing files that changed from the base of the PR and between 3b9e31f and 28cefdc.

📒 Files selected for processing (2)
  • app/models/metadata.rb
  • test/system/markdown_test.rb

Comment thread app/models/metadata.rb
Comment thread test/system/markdown_test.rb Outdated
@github-actions

github-actions Bot commented Apr 29, 2026

Copy link
Copy Markdown

🚀 Review App

URL: https://bootcamp-pr-9958-fvlfu45apq-an.a.run.app

Basic認証: fjord / (ステージングと同じ)
PR更新時に自動で再デプロイされます。

@mousu-a

mousu-a commented Apr 29, 2026

Copy link
Copy Markdown
Contributor Author

@machida
お疲れ様です。
リンクカードの見た目の相談なのですが、現在リンクカードのdescriptionが存在しない場合、descriptionの場所にurlを表示するようになっています。(該当箇所)
以下はdescriptionが存在しないURLです。
image

ですがtitleも同じくURLになっているため、役割が重複しているように感じます。
descriptionが存在しない場合はそこは何も表示しないようにした方が良いのではと思ったのですがどうでしょうか?

参考にしているZENNでもそのようになっているようです。ご検討いただければと思います🙏
Image

Comment thread app/models/metadata.rb
url: @url,
site_url: site_url
}
return nil if metadata[:title].blank?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issueにある通り、titleがなければリンクカードの展開を失敗させます。
(urlはユーザー入力である@[card](url)urlをそのまま流用しているのでtitleのみのチェックとしています)

#9920 (comment)
GemのOGP取得に失敗した場合フォールバック処理をする。
基本的にmetadataのプロパティは全て埋めるようにする。
ただしリンクカードとして必須のプロパティであるtitle、image, urlが揃っていなければカードの展開を失敗させる。

imageは必須ではないのではないかと思いました。

@mousu-a mousu-a changed the title リンクカードにフォールバック処理を追加 リンクカード フォールバック処理を追加 Apr 29, 2026
@machida

machida commented Jul 3, 2026

Copy link
Copy Markdown
Member

@mousu-a
提案ありがとうございます!!
それでお願いしたいです🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants