gpt4 book ai didi

jekyll - 带有 Jekyll 的预邮器

转载 作者:行者123 更新时间:2023-12-04 02:21:11 24 4
gpt4 key购买 nike

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

您可能知道,HTML 电子邮件需要内联所有 CSS,这非常难以管理。大多数人使用 Premailer 自动处理此问题 - https://github.com/premailer/premailer

对于特定布局,我将如何使用带有 Jekyll 的 premailer?

是否可以通过插件使用 premailer?

最佳答案

是的,你可以!

安装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 不可用。

关于jekyll - 带有 Jekyll 的预邮器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29246991/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com