2015-02-23 72 views
0

我正在研究一個插件來解析所有帖子,並將它們收集到JSON文件中以供搜索機制使用。 我怎樣才能訪問這個帖子的文本,沒有標記?我目前訪問site.posts,然後例如page.content循環。這將返回帖子的內容,但包含換行符標記(\n)和Markdown語法。從Jekyll插件訪問無標記帖子和頁面內容

我看到有人想要get Markdown processed content in a Jekyll tag plugin的另一個問題,但我的情況是不同的:我根本不需要任何標記,只是文章的純文本,沒有格式應用。

以下是我目前實施的關鍵def

def generate(site) 
    target = File.open('js/searchcontent.js', 'w') 
    target.truncate(target.size) 
    target.puts('var tipuesearch = {"pages": [') 

    all_but_last, last = site.posts[0..-2], site.posts.last 

    # Process all posts but the last one 
    all_but_last.each do |page| 
    tp_page = TipuePage.new(
     page.data['title'], 
     "#{page.data['tags']} #{page.data['categories']}", 
     page.url, 
     page.content 
    ) 
    target.puts(tp_page.to_json + ',') 
    end 

    # Do the last post 
    tp_page = TipuePage.new(
    last.data['title'], 
    "#{last.data['tags']} #{last.data['categories']}", 
    last.url, 
    last.content 
) 
    target.puts(tp_page.to_json) 

    target.puts(']};') 
    target.close 
end 

回答

1

也許你可以試試這個:

{{ page.content | strip_html | strip_newlines }} 

編輯很明顯,我誤解你的問題。

但是你可以使用液體過濾器與include Liquid::StandardFilters

然後你可以在你的插件使用strip_htmlstrip_newlines

+0

這些是液體過濾器,無法從插件Ruby腳本訪問。我需要完全在此插件文件中完成此操作。 – Tohuw 2015-02-23 14:32:31