从 Jekyll 插件访问无标记 post 和页面内容
Access markup-free post and page content from Jekyll plugins
我正在开发一个插件来解析所有 post 并将它们收集到一个 JSON 文件中以供搜索机制使用。 如何只访问 post 的文本而不带任何标记? 我目前正在访问 site.posts
,例如page.content
循环。 returns 是 post 的内容,但包括换行符(\n
)和 Markdown 语法。
我看到另一个问题有人想 get Markdown processed content in a Jekyll tag plugin,但我的情况不同:我根本不需要任何标记,只是 post 的纯文本,没有已应用格式。
下面是我当前实现的关键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
也许你可以试试这个:
{{ page.content | strip_html | strip_newlines }}
编辑 显然我误解了你的问题。
但是您可以将 Liquid 过滤器与 include Liquid::StandardFilters
然后您可以在您的插件中使用 strip_html
和 strip_newlines
。