首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Premailer和Jekyll

Premailer和Jekyll
EN

Stack Overflow用户
提问于 2015-03-25 03:38:34
回答 1查看 200关注 0票数 3

我正在做一个既有静态网页又有HTML电子邮件模板的项目。

您可能知道,HTML电子邮件要求所有CSS内联,这是一个巨大的痛苦管理。大多数人使用Premailer来自动处理这个问题- https://github.com/premailer/premailer

我将如何使用premailer与Jekyll在一个特定的布局?

是否可以通过插件使用预邮件程序?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-25 14:19:53

不,你可以!

安装了premailer之后,您可以制作一个类似于此的jekyll插件

代码语言:javascript
复制
require 'premailer'

module Jekyll
  class Site

    # create an alias for the overriden site::write method
    alias orig_write write

    # we override the site::write method
    def write

      # first call the overriden write method,
      # in order to be sure to find generated css
      orig_write

      # layout name we are looking for in pages/post front matter
      # this can come from site config
      @layout   = 'post'

      # this the css that will be inlined
      # this can come from site config
      @css_path = '/css/main.css'

      # look for generated css file content
      @cssPages = pages.select{ |page|
        page.destination(dest).include? @css_path
      }

      # look again in pages and posts
      # to generate newsletters with premailer
      newsletters = [posts, pages].flatten.select{ |page_or_post|
        page_or_post.data['layout'] == @layout
      }

      newsletters.each do |newsletter|

        newsletter.output = Premailer.new(
            newsletter.output,
            {
                # declare that we pass page html as a string not an url
                :with_html_string => true,
                # also pass css content as a string
                :css_string       => @cssPages.join,
            }
        ).to_inline_css;

        # rewrite the newsletter with inlined css
        newsletter.write(dest)

      end
    end
  end
end

这是关于如何将premailer与Jekyll集成的一般想法。代码当然可以改进。

注意:我决定不使用发电机插件,因为当调用生成器时,sass和scss文件仍然没有解析,生成的css也不可用。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29246991

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档