asciidoc - 如何为现有的 Asciidoctor Asciidoc 宏创建自定义 HTML 输出?
问题描述
例如,我想将loading="lazy"
属性添加到我的所有图像中,例如:
image::myimage.jpg[]
但默认的 HTML<img>
元素输出没有该属性。
也许有人问过这个问题:使用 asciidoctor 创建自定义 HTML,但问题不够清楚,我无法确定。
解决方案
这记录在:https ://asciidoctor.org/docs/user-manual/#provide-custom-templates但感觉像一个最小的例子会是有益的。
主文件
image::myimage.jpg[]
模板目录/block_image.html.erb
<%#encoding:UTF-8%><div<%= @id && %( id="#{@id}") %> class="<%= ['imageblock',@style,role].compact * ' ' %>"<%
if (attr? :align) || (attr? :float)
%> style="<%= [("text-align: #{attr :align};" if attr? :align),("float: #{attr :float};" if attr? :float)].compact * ' ' %>"<%
end %>>
<div class="content"><%
if attr? :link %>
<a class="image" href="<%= attr :link %>"><img src="<%= image_uri(attr :target) %>" loading="lazy" alt="<%= attr :alt %>"<%= (attr? :width) ? %( width="#{attr :width}") : nil %><%= (attr? :height) ? %( height="#{attr :height}") : nil %>></a><%
else %>
<img src="<%= image_uri(attr :target) %>" loading="lazy" alt="<%= attr :alt %>"<%= (attr? :width) ? %( width="#{attr :width}") : nil %><%= (attr? :height) ? %( height="#{attr :height}") : nil %>><%
end %>
</div><%
if title? %>
<div class="title"><%= captioned_title %></div><%
end %>
</div>
这是来自https://github.com/asciidoctor/asciidoctor-backends/blob/master/erb/html5/block_image.html.erb的默认模板的副本,但 HTML 通过添加loading="lazy"
.
宝石文件
gem 'asciidoctor', '2.0.10'
gem 'concurrent-ruby', '1.1.7'
gem 'tilt', '2.0.10'
我们需要安装这些额外的 gem 才能让它工作。
编译:
asciidoctor --template-dir template_dir main.adoc
就是这样,输出 HTML 现在包含loading="lazy"
.
在 Asciidoctor 2.0.10 中测试。
推荐阅读
- javascript - 保持手风琴的特定面板打开
- r - rpart 的多类分类非常慢(只有 100 行)
- javascript - 如何在页面加载后显示图像弹出窗口
- svelte - Svelte - 更新值时防止重新渲染
- java - 使用 Spring Boot 上传文件失败
- django - Django类别在渲染到模板时选择所有类别数据?
- swift - 从 SwiftUI 的异步块内部改变 @State 变量是否安全?
- c++ - 子类化 NullFederateAmbassador 时出现链接错误
- python - 如何将 functools.cache 包装在另一个装饰器中?
- php - 如何在 laravel 中使用缓存?