在我当前的 Jekyll 设置中,我正在使用 asciidoc (
.adoc
) 文件格式编写所有帖子,特别是我正在使用这些插件
plugins:
- jekyll-asciidoc
- jekyll-redirect-from
- jekyll-feed
我需要创建一个自定义 HTML 组件,我已将其添加到
component.html
中的 _includes
文件夹下,遵循 Jekyll 的 includes 功能。
我现在可以在 Markdown 文件中使用 Liquid 来渲染 HTML 组件
{% include component.html %}
在我的帖子中的任何时候,但我不能对 asciidoc 文件做同样的事情。我查看了他们的文档,但找不到合适的方法来使用此 Jekyll 功能。
有没有办法在 asciidoc 中使用 Liquid?
经过一些研究,我发现了一些可以在 asciidoc 文件中注入
_includes
HTML 组件的东西。
jekyll-asciidoc
有一个特殊的页面属性来启用 Liquid 预处理,您可以在 :page-liquid: 找到文档
所以,为了启用 Liquid 预处理,您只需将其添加到 asciidoc post
:page-liquid:
有了这个,Jekyll 将在将文件实际传递给 asciidoc 生成器之前解析文件中的每个 Liquid 语句。
帖子在这一点上还需要一点改动,引用文档:
如果您使用 Liquid include 标签将 HTML 包含到 AsciiDoc 文档中,请将其包含在 passthrough 块中。
++++ {% include file.html %} ++++
默认情况下
:page-liquid:
将转义 Liquid 生成的内容,这就是为什么您需要在 asciidoc Passthrough构造中包含
{% include ... %}
语句的原因 ++++
将把原始 HTML 注入页面本身。
总而言之,这是一个示例帖子的样子:
_includes/component.html
<p>Some sample text</p>
_posts/liquid-in-asciidoc.adoc
---
title: "HTML component in adoc
---
:page-liquid:
++++
{% include component.html %}
++++
也可以通过创建 AsciiDoc 扩展来实现这一点,例如我可以创建一个识别此语法的块扩展
[example_component]
--
Some text here
--
红宝石扩展将是
require 'asciidoctor/extensions'
include Asciidoctor
Asciidoctor::Extensions.register do
block :example_component do
process do |parent, reader, attributes|
content = reader.lines.join(' ')
html = %(<p>#{content}</p>)
create_pass_block parent, html, {}, :content_model => :raw
end
end
end
这将产生与上面相同的结果