如何扩展Middleman的Redcarpet Markdown渲染器?

3
我正在使用Middleman构建网站,并使用Redcarpet作为我的markdown引擎,主要是因为它支持GFM
我想要在markdown渲染过程中添加对各种语法选项的支持,或者在其之前进行操作。例如,我希望能够实现以下功能:
[file:/path/to/file]

将被渲染为:

<p class="file">
  <code>/path/to/file</code>
</p>

在任何情况下,我都没有渲染任何可能影响模板中剩余markdown的内容,因此我认为我可以在渲染过程之前进行操作。
另外,如果使用其他渲染器更简单的话,我并不局限于Redcarpet,只是希望能够支持GFM。

如果你决定使用Pandoc,可以看看pandoc scripting,祝你好运! - mb21
1个回答

1

首先,您需要在config.rb文件中创建一个基于redcarpet的新渲染器。类似于以下内容:

   set renderer: myRenderer

接下来,您需要创建一个名为"myRenderer"的新类(您可以在config.rb的顶部执行此操作,但也可以将其放在外部文件中)。

require "middleman-core/renderers/redcarpet"
class myRenderer < Middleman::Renderers::MiddlemanRedcarpetHTML

def preprocess(document)
  # insert ruby code to use a regex to find your tag in the document
  # insert ruby code to generate your HTML and replace your tag with
  #    HTML that you want
  return (document)
end

如果您希望这是最后要做的事情,请使用postprocess(document)而不是preprocess(document)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接