Skip to content

Instantly share code, notes, and snippets.

@axeleroy
Created May 29, 2020 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save axeleroy/273e6a6eb0eb68c8dad1f80c4d45303e to your computer and use it in GitHub Desktop.
Save axeleroy/273e6a6eb0eb68c8dad1f80c4d45303e to your computer and use it in GitHub Desktop.
Jekyll plugin that replaces pictures in Markdown documents by responsive images using jekyll-responsive-mage
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(/!\[(.*)\]\((?:\/?([^\)\:\s]+))(?:\s"(.*)")?\)(?:{:([^}]+)})*/, '{% responsive_image path: \2 alt: "\1" title: "\3" %}')
document.content = newContent
end
end
@axeleroy
Copy link
Author

axeleroy commented May 29, 2020

This Jekyll plugin replaces every Markdown pictures in Markdown documents by responsive images using jekyll-responsive-image's template.

For example:

![Philadelphia's Magic Gardens. This place was so cool!](/assets/images/philly-magic-gardens.jpg "Philadelphia's Magic Gardens")

will be replaced by

{% responsive_image path: assets/images/philly-magic-gardens.jpg alt: "Philadelphia's Magic Gardens. This place was so cool!" title: "Philadelphia's Magic Gardens" %}

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