2017-08-26 81 views
0

我正在解决这个问题。如何在Jekyll中执行base64编码

反正有没有把字符串编码到base64在Jekyll?

我想在将代码推送到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 }}

回答

2

你可以创建自己的液体标签并提供您的字符串作为参数。然后使用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) 

希望这会有所帮助,我会尝试做一个简单的base64液体标签的化身,如果我得到它会更新加工。