0

我在我的 Rails 博客应用程序中使用 Redcarpet 进行语法高亮。

在我的帖子/index.html.erb 中,我想截断博客帖子以预览前几句(或段落)。用户应该能够点击截断帖子末尾的“阅读更多”来阅读整篇博文。不幸的是,“阅读更多”链接不适用于 Redcarpet(当我不使用我的降价方法时(见下文),链接工作正常)。我该如何解决?我必须在 Redcarpet 中使用其他选项吗?

我在 /helpers/application_helper.rb 中使用 Redcarpet 的降价方法:

def markdown(content)
  renderer = HTMLwithPygments.new(hard_wrap: true, filter_html: true)
  options = {
    autolink: true,
    no_intra_emphasis: true,
    disable_indented_code_blocks: true,
    fenced_code_blocks: true,
    lax_html_blocks: true,
    strikethrough: true,
    superscript: true
  }

  Redcarpet::Markdown.new(renderer, options).render(content).html_safe
end

/views/posts/index.html.erb

<%=  markdown (truncate(post.content, 
                        length: 600, 
                        separator: ' ',
                        omission: '... ') {
                        link_to "read more", post 
               }) %>

顺便说一句:我正在循环通过@posts 变量,所以“post.content”给了我一篇帖子的内容,而“post”给了我帖子的路径。

“阅读更多”文本正在显示,但您无法单击它。当我离开“降价”方法时,“阅读更多”链接工作正常。

如何使用“markdown”方法创建链接?

4

1 回答 1

1

该链接虽然不是 Markdown,但它是 HTML。也许将其更改为 Markdown?

<%= markdown(truncate(post.content, length: 600,
                      separator: ' ', omission: '... ') {
             "[read more](#{post_path(post)})"
    }) %>

post_path如果不正确,请更改为适当的内容。

于 2015-09-29T17:55:52.597 回答