Skip to content

Instantly share code, notes, and snippets.

@mmistakes
Last active October 14, 2022 14:51
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mmistakes/77c68fbb07731a456805a7b473f47841 to your computer and use it in GitHub Desktop.
Save mmistakes/77c68fbb07731a456805a7b473f47841 to your computer and use it in GitHub Desktop.
Jekyll plugin to replace Markdown images eg. `![description](image.jpg)` with `{% picture %}` tag
# Description: Jekyll plugin to replace Markdown image syntax with {% picture %} tag for crafting responsive images
# place in /_plugins/
Jekyll::Hooks.register :posts, :pre_render do |post, payload|
docExt = post.extname.tr('.', '')
# only process if we deal with a markdown file
if payload['site']['markdown_ext'].include? docExt
newContent = post.content.gsub(/\!\[(.+)\]\((.+)\)/, '{% picture default \2 alt="\1" %}')
post.content = newContent
end
end
@YJPL
Copy link

YJPL commented Jun 10, 2019

This version works for collections in addition to posts (e.g. here made to work with Jekyll Picture Tag):

Jekyll::Hooks.register :documents, :pre_render do |document, payload|
  docExt = document.extname.tr('.', '')
  # only process if we deal with a markdown file
  if payload['site']['markdown_ext'].include? docExt
    newContent = document.content.gsub(/!\[(.*)\]\(([^\)]+)\)(?:{:([^}]+)})*/, '{% picture default \2 --alt \1  %}')
    document.content = newContent
  end
end

@Tymofei
Copy link

Tymofei commented Jul 23, 2020

How can i use it in bettercap ? through proxy

@tadamcz
Copy link

tadamcz commented Oct 13, 2022

I'm using this in the year of our lord 2022. Thanks @mmistakes! 🙌

I changed the regex to

newContent = post.content.gsub(/\!\[(.*)\]\((.+)\)/, '{% picture default \2 alt="\1" %}')

to allow it to work even if the alt text is the empty string.

@tadamcz
Copy link

tadamcz commented Oct 14, 2022

I've done some more work to make this plugin more robust and readable. See: https://gist.github.com/tadamcz/869802c919c72172486f219751904108

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