Jekyll,修改一些html标签的呈现方式

时间:2014-07-15 00:55:49

标签: ruby plugins markdown jekyll redcarpet

我想修改一些html标签在jekyll上呈现的方式。我需要的是自动添加一些css类(在本例中为“.table”类到表html标记)。

我正在使用redcarpet降价处理器。我想我需要编写一个扩展渲染器的插件,但我找不到任何好的例子......

我想出了这个,但它只是一个复制/粘贴工作,它不起作用......

require 'redcarpet'

class BootstrapTables < Redcarpet::Render::HTML
  def table(header, body)
    "\n<table class='table'><thead>\n#{ header }</thead><tbody>\n#{ body }</tbody></table>\n"
  end
end

有人可以帮忙吗?

3 个答案:

答案 0 :(得分:9)

我已经测试过你可以通过 kramdown

为一个降价元素提供课程
{:.foo}
| First Header  | Second Header |
| ------------- | ------------- |
| Content Cell  | Content Cell  |
| Content Cell  | Content Cell  |

您的表格类为 foo

注意:this answer was given one month ago on SO

答案 1 :(得分:2)

我找到了方法并更正了代码,我需要一个正确配置的自定义渲染器。在 _config.yaml 中使用RedcarpetExt作为降价变量将激活它。

# Create a custom renderer that extend Redcarpet to customize its behavior.
require 'jekyll'
require 'redcarpet'

class RedcarpetExtender < Redcarpet::Render::HTML
  # Extend the table to be compatible with the Bootstrap css.
  def table(header, body)
    "\n<table class='table-bordered table-hover'><thead>\n#{ header }</thead><tbody>\n#{ body }</tbody></table>\n"
  end
end

class Jekyll::Converters::Markdown::RedcarpetExt
  def initialize(config)
    @site_config = config
  end

  def extensions
    Hash[ *@site_config['redcarpet']['extensions'].map {|e| [e.to_sym, true]}.flatten ]
  end

  def markdown
    @markdown ||= Redcarpet::Markdown.new(RedcarpetExtender, extensions)
  end

  def convert(content)
    return super unless @site_config['markdown'] == 'RedcarpetExt'
    markdown.render(content)
  end
end

答案 2 :(得分:1)

另一种解决方案是使用sass Bootstrap version

此版本与twbs / bootstrap同步,并且Jekyll本身支持sass / scss。

然后,一旦你有sass工作(需要五分钟),你只需要在自定义style.scss文件中创建一个规则:

table{
  @extend .table;
}

然后每个表都将使用.table引导规则。

相关问题