【问题标题】:How to get rid of additional spaces using redcarpet gem (Ruby on Rails)如何使用 redcarpet gem (Ruby on Rails) 去除额外的空间
【发布时间】:2016-07-06 23:21:06
【问题描述】:

我正在尝试使用 markdown 写博客,并决定安装 redcarpet gem。一切看起来都很好,pygments.rb 在语法突出显示方面做得很好,但是问题是,每当我尝试使用 ``` 放置代码块时,我都会缩进所有行(第一行除外)增加 6 个空格。如何摆脱它?

application_helper.rb

module ApplicationHelper
  class HTMLwithPygments < Redcarpet::Render::HTML
    def block_code(code, language)
      Pygments.highlight(code, lexer: language)
    end
  end

  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
end

发布视图 - show.html.haml

.container
  .show.title
    = @post.title
  .show.header
    = @post.header
  .show.created_at
    = @post.created_at
  .show.content
    = markdown @post.content

这就是代码在 sublime 中的样子:

这是复制粘贴相同代码以发布内容时呈现的帖子的样子:

我正在使用带有 2 个空格缩进的 SublimeText3,视图为 html.haml 格式。

这是帖子内容的准确输入:

```ruby
module ApplicationHelper
  class HTMLwithPygments < Redcarpet::Render::HTML
    def block_code(code, language)
      Pygments.highlight(code, lexer: language)
    end
  end

  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
end

【问题讨论】:

    标签: ruby-on-rails haml markdown redcarpet


    【解决方案1】:

    这是由 Haml 缩进块引起的,以便输出 HTML 格式整齐,这通常是人们想要的,但可能会导致此类对空格敏感的代码出现问题。

    有几种方法可以修复它。首先,如果您将 :ugly option 设置为 true(在生产中应该是这种情况)运行,那么额外的空白将不会添加到任何地方,您将获得所需的结果。

    您也可以使用whitespace preservation operator ~ 代替=。这会将块中的所有换行符转换为实体 (&amp;#x000A),因此不会添加额外的空格(因为没有要添加的换行符)。这将更改 HTML 生成的内容,但在浏览器中查看时会显示为您想要的样子。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-04
      • 2011-06-26
      • 1970-01-01
      • 2012-06-10
      • 1970-01-01
      相关资源
      最近更新 更多