How to create custom HTML output for an existing Asciidoctor Asciidoc macro?

前端 未结 1 1731
名媛妹妹
名媛妹妹 2021-01-23 10:07

For example, I want to add the loading="lazy" attribute to all my images e.g.:

image::myimage.jpg[]

but the default HTML

1条回答
  •  广开言路
    2021-01-23 10:44

    This is documented at: https://asciidoctor.org/docs/user-manual/#provide-custom-templates but it feels like a minimal example would be beneficial.

    main.adoc

    image::myimage.jpg[]
    

    template_dir/block_image.html.erb

    <%#encoding:UTF-8%> 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 %>>
    
    <% if attr? :link %> <%= attr :alt %><%= (attr? :height) ? %( height="#{attr :height}") : nil %>><% else %> <%= attr :alt %><%= (attr? :height) ? %( height="#{attr :height}") : nil %>><% end %>
    <% if title? %>
    <%= captioned_title %>
    <% end %>

    This is a copy the default template from https://github.com/asciidoctor/asciidoctor-backends/blob/master/erb/html5/block_image.html.erb, but with the HTML modified by adding loading="lazy".

    Gemfile

    gem 'asciidoctor', '2.0.10'
    gem 'concurrent-ruby', '1.1.7'
    gem 'tilt', '2.0.10'
    

    We need to install those extra gems for it to work.

    Compile:

    asciidoctor --template-dir template_dir main.adoc
    

    and that's it, the output HTML now contains loading="lazy".

    Tested in Asciidoctor 2.0.10.

0 讨论(0)
提交回复
热议问题