我正在开发一个既有静态网页也有HTML电子邮件模板的项目。
正如您可能知道的那样,HTML电子邮件需要内联所有CSS,这是一个很难管理的问题。大多数人使用Premailer自动处理 - https://github.com/premailer/premailer
我如何使用Jekyll的预制器进行特定布局?
是否可以通过插件使用预编程器?
答案 0 :(得分:3)
是的,你可以!
安装premailer
后,您可以像这样jekyll plugin:
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集成的一般概念。
代码当然可以改进。
注意:我决定不使用Generator plugin因为当调用生成器时,sass和scss文件仍然没有被解析并且生成的css不可用。