我正在做一个既有静态网页又有HTML电子邮件模板的项目。
您可能知道,HTML电子邮件要求所有CSS内联,这是一个巨大的痛苦管理。大多数人使用Premailer来自动处理这个问题- https://github.com/premailer/premailer
我将如何使用premailer与Jekyll在一个特定的布局?
是否可以通过插件使用预邮件程序?
发布于 2015-03-25 14:19:53
不,你可以!
安装了premailer之后,您可以制作一个类似于此的jekyll插件:
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也不可用。
https://stackoverflow.com/questions/29246991
复制相似问题