wildlyinaccurate / jekyll-responsive-image

An unopinionated Jekyll plugin for generating and using responsive images
MIT License
332 stars 48 forks source link

Would it be possible to use the standard Markdown syntax for images? #15

Closed nhoizey closed 7 years ago

nhoizey commented 8 years ago

I have put the question on StackOverflow first, so this is almost a copy/paste:

I would like to use the Jekyll Responsive Image plugin to generate appropriate responsive images with srcset/sizes attributes for my posts' images. That's why I'm here obviously… ;-)

But I would also like to be able to edit my posts in a software providing a live preview like MacDown, which only understands the standard Markdown syntax for images.

That's why I would like to know if there is a way —in this plugin or another one— to tell Jekyll to transform the standard Markdown syntax for images, which I would put in my Markdown files…

![alt text](path/to/image.jpg)

…into this syntax specific to the Jekyll Responsive Image plugin:

{% responsive_image path: path/to/image.jpg alt: "alt text" %}

And THEN, Jekyll could continue and use Kramdown to generate the HTML…

I understand I could only manage one template this way, but it should be enough for my blog.

wildlyinaccurate commented 8 years ago

You should be able to achieve this by writing a simple converter plugin. This should do the trick, though I don't know how performant it will be with larger sites:

module Jekyll
  class UpcaseConverter < Converter
    safe true
    priority :low

    def matches(ext)
      ext =~ /^\.md$/i
    end

    def output_ext(ext)
      ".html"
    end

    def convert(content)
      content.gsub(/\!\[(.+)\]\((.+)\)/, '{% responsive_image path: \2 alt: \1  %}')
    end
  end
end
nhoizey commented 8 years ago

Shouldn't the output be .md?

How does this ensure it is run before Kramdown?

wildlyinaccurate commented 8 years ago

output_ext is used to determine the extension of the generated file (i.e. the file in _site/). So unless you're building your site in a format other than HTML, it should probably be ".html".

If you're seeing it run after the Markdown processor, then you can bump the priority -- there's a list of priorities in the docs I linked to.

nhoizey commented 8 years ago

Thanks for the explanation.

It seems I could also build my converter so that it runs Kramdown after it has done its job: http://stackoverflow.com/a/26075361/717195

nhoizey commented 8 years ago

I think I will use @parkr 's solution: http://stackoverflow.com/a/35636071/717195

But then I still need #13 before migrating from my current use of https://github.com/nhoizey/jekyll-picture-tag