【问题标题】:Access markup-free post and page content from Jekyll plugins从 Jekyll 插件访问无标记的帖子和页面内容
【发布时间】:2015-02-23 03:51:12
【问题描述】:

我正在开发一个插件来解析所有帖子并将它们收集到一个 JSON 文件中,以供搜索机制使用。 我怎样才能只访问帖子的文本而不使用标记?我目前正在访问site.posts,然后例如page.content 在循环中。这将返回帖子的内容,但包括换行符 (\n) 和 Markdown 语法。

我看到另一个问题,有人想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

【问题讨论】:

    标签: jekyll


    【解决方案1】:

    也许你可以试试这个:

    {{ page.content | strip_html | strip_newlines }}
    

    编辑显然我误解了你的问题。

    但是你可以使用带有include Liquid::StandardFilters的液体过滤器

    然后您可以在插件中使用strip_htmlstrip_newlines

    【讨论】:

    • 这些是 Liquid 过滤器,无法从插件 Ruby 脚本访问。我需要在这个插件文件中完全做到这一点。
    猜你喜欢
    • 1970-01-01
    • 2017-08-28
    • 1970-01-01
    • 2015-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-23
    相关资源
    最近更新 更多