-
-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor redirects by redirect_from
Jekyll plugin
#72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
/contests/terms.html などのパスをSNSに貼られた時 |
b917845
to
502febb
Compare
なるほど! とすると redirect template で解決できそうなので、こんな感じでどうでしょう...? 💭 502febb |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ありがとうございます。
しかし、_layout/redirect.htmlを使った方法だと
<meta property="og:title" content="『DojoCon Japan 2025 プログラミングコンテスト』応募方法 | DojoCon Japan 2025" />
この辺りの情報が取れないようなので、あえて採用を見送っております。
情報を小出しにする形になってしまい、すみません。
確かに...!! パッと手元で
|
興味本位でサクッとプラグインを書いてみましたが、確かにココまで書くなら既存のハードコーディングでも全然十分そうですね 🙇♂️💦 (恐らく今回のようなリダイレクト用ファイルも今後は増えないかなとも思いますので 🤔💭) ───────────────────────────────────────────────────────────────────────
added: _plugins/redirect_with_metadata.rb
───────────────────────────────────────────────────────────────────────
@ _plugins/redirect_with_metadata.rb:1 @
module Jekyll
class RedirectWithMetadata < Generator
priority :lowest
def generate(site)
redirect_map = build_redirect_map(site)
redirect_pages = site.pages.select { |page| page.data['redirect'] }
redirect_pages.each do |page|
target = redirect_map[normalize_url(page.data['redirect']['to'], site)]
copy_metadata(from: target, to: page) if target
end
end
private
def build_redirect_map(site)
pages_with_redirect_from = (site.pages + site.documents).select { |p| p.data['redirect_from'] }
pages_with_redirect_from.flat_map do |page|
[page.url, page.data['permalink']].compact.map { |u| [u.chomp('/'), page] }
end.to_h
end
def normalize_url(url, site)
url.sub(site.config['url'] || '', '')
.sub(site.config['baseurl'] || '', '')
.chomp('/')
end
def copy_metadata(from:, to:)
from.data.each do |key, value|
to.data[key] = value unless %w[layout redirect redirect_from permalink].include?(key)
end
end
end
end |
こんな事も出来るんですね 今後もし、何か書き方で困ることがあった時に |
インストール済みの redirect_from プラグインを使うと Front Matter でリダイレクトでき、関心の対象も1ファイルにまとめられるので、対応してみました! 🛠💨✨ cf. https://github.com/jekyll/jekyll-redirect-from