如何在 Jekyll 中进行 base64 编码
How to do base64 encoding in Jekyll
这个问题一直困扰着我。
在 Jekyll 中是否有将字符串编码为 base64 的方法?
我想在将代码推送到 Github 页之前或之后为博客中的每篇文章创建 json 个文件。
- 2017-08-26-post1.md
- 2017-08-26-post1.md
- 2017-08-26-post1.json
- 2017-08-26-post1.json
可能是 {{ some_string | base64_encoded }}
您可以创建自己的液体标签并提供您的字符串作为参数。然后使用 ruby https://ruby-doc.org/stdlib-2.1.3/libdoc/base64/rdoc/Base64.html 将该字符串转换为 base64 并将其输出到您的页面。
这样的事情可能会有所帮助,这是我在这里找到的一个例子 - https://blog.sverrirs.com/2016/04/custom-jekyll-tags.html:
class AdsInlineTag < Liquid::Tag
def initialize(tag_name, input, tokens)
super
@input = input
end
def render(context)
# Split the input variable (omitting error checking)
input_split = split_params(@input)
adclient = input_split[0].strip
adslot = input_split[1].strip
# Write the output HTML string
output = "<div style=\"margin: 0 auto; padding: .8em 0;\"><script async "
output += "src=\"//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js\">"
output += "</script><ins class=\"adsbygoogle\" style=\"display:block\" data-ad-client=\"#{adclient}\""
output += "data-ad-slot=\"#{adslot}\" data-ad-format=\"auto\"></ins><script>(adsbygoogle ="
output += "window.adsbygoogle || []).push({});</script></div>"
# Render it on the page by returning it
return output;
end
def split_params(params)
params.split("|")
end
end
Liquid::Template.register_tag('ads', AdsInlineTag)
希望这对您有所帮助,我会尝试为 jekyll 制作一个简单的 base64 液体标签,如果我让它工作,我会更新。
这是一个使用 Ruby 编写的有效 base64 过滤器的示例:
require "base64"
module Base64Filter
def base64_encode (input)
Base64.encode64(input)
end
end
Liquid::Template.register_filter(Base64Filter) # register filter globally
在 jekyll 中创建一个新文件 base64_filter.rb
并将其放入根目录的 _plugins
文件夹中。如果您处于 serve
模式,则需要重新启动服务器。
然后您可以将它用于例如:
{{ site.email | base64_encode }}
注意:Github页面不支持插件!
这个问题一直困扰着我。
在 Jekyll 中是否有将字符串编码为 base64 的方法?
我想在将代码推送到 Github 页之前或之后为博客中的每篇文章创建 json 个文件。
- 2017-08-26-post1.md
- 2017-08-26-post1.md
- 2017-08-26-post1.json
- 2017-08-26-post1.json
可能是 {{ some_string | base64_encoded }}
您可以创建自己的液体标签并提供您的字符串作为参数。然后使用 ruby https://ruby-doc.org/stdlib-2.1.3/libdoc/base64/rdoc/Base64.html 将该字符串转换为 base64 并将其输出到您的页面。
这样的事情可能会有所帮助,这是我在这里找到的一个例子 - https://blog.sverrirs.com/2016/04/custom-jekyll-tags.html:
class AdsInlineTag < Liquid::Tag
def initialize(tag_name, input, tokens)
super
@input = input
end
def render(context)
# Split the input variable (omitting error checking)
input_split = split_params(@input)
adclient = input_split[0].strip
adslot = input_split[1].strip
# Write the output HTML string
output = "<div style=\"margin: 0 auto; padding: .8em 0;\"><script async "
output += "src=\"//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js\">"
output += "</script><ins class=\"adsbygoogle\" style=\"display:block\" data-ad-client=\"#{adclient}\""
output += "data-ad-slot=\"#{adslot}\" data-ad-format=\"auto\"></ins><script>(adsbygoogle ="
output += "window.adsbygoogle || []).push({});</script></div>"
# Render it on the page by returning it
return output;
end
def split_params(params)
params.split("|")
end
end
Liquid::Template.register_tag('ads', AdsInlineTag)
希望这对您有所帮助,我会尝试为 jekyll 制作一个简单的 base64 液体标签,如果我让它工作,我会更新。
这是一个使用 Ruby 编写的有效 base64 过滤器的示例:
require "base64"
module Base64Filter
def base64_encode (input)
Base64.encode64(input)
end
end
Liquid::Template.register_filter(Base64Filter) # register filter globally
在 jekyll 中创建一个新文件 base64_filter.rb
并将其放入根目录的 _plugins
文件夹中。如果您处于 serve
模式,则需要重新启动服务器。
然后您可以将它用于例如:
{{ site.email | base64_encode }}
注意:Github页面不支持插件!