jekyll-postfiles icon indicating copy to clipboard operation
jekyll-postfiles copied to clipboard

support for img: tag in post header

Open ciapecki opened this issue 4 years ago • 1 comments

---
layout: post
img: picture.png
tags: [sometag] # add tag
---

Is it possible to use pictures saved in certain post folder in this header section of a post? Currently it generates:

<article class="post">
  
    <a class="post-thumbnail" style="background-image: url(/assets/img/picture.png)" href="/2020-10-13-test/"></a>
  
  <div class="post-content">
    <h2 class="post-title"><a href="/2020-10-13-test/">Test</a></h2>
    <p>Some body
</p>
    <span class="post-date">2020, Oct 13&nbsp;&nbsp;&nbsp;—&nbsp;</span>
    <span class="post-words">1 minute read</span>
  </div>
</article>

it tries to find the picture in main folder: /assets/img/picture.png

ciapecki avatar Oct 13 '20 10:10 ciapecki

I wrote a li'l plugin to do this – it could be implemented into jekyll-postfiles with a config property that allows you to set an array of front matter properties to process.

# jekyll-postfiles is lovely, *but* it doesn't handle front matter – which is
# reasonable, I suppose, since asset path front matter properties
# don't have set keys. This little band-aid makes it possible for front matter
# asset paths to work the same way jekyll-postfiles does.

# Could be put in _config.yaml, but I don't see the need at the moment.
KEYS_TO_PROCESS = [
    "img",
    "image",
    "cover-image"
]

Jekyll::Hooks.register [:posts], :pre_render do |post|
  KEYS_TO_PROCESS.each do |key|
    next unless post.data.key? key
    value = post.data[key]
    unless value.start_with?('/') || value.downcase.start_with?('http://') || value.downcase.start_with?('https://')
      post.data[key] = post.url + value
    end
  end
end

obskyr avatar Feb 11 '24 20:02 obskyr