You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Asciidoctor转DOCX时表格/图片自动编号标题消失问题求助

解决Asciidoctor转DOCX时表格/图片标题自动编号丢失的问题

方法1:自定义DOCX模板配置自动编号

Asciidoctor生成DOCX时依赖内置模板,默认模板未启用标题自动编号。你可以通过自定义模板解决:

  • 新建空白DOCX文档,创建Table Caption和Figure Caption两个段落样式
  • 为这两个样式设置对应格式的自动编号(例如Table 1.、Picture 1.)
  • 将模板保存为custom-template.docx
  • 转换时指定模板参数:
    asciidoctor-docx -a docx-template=custom-template.docx your-document.adoc
    

方法2:用Ruby扩展注入自动编号

如果不想修改模板,可编写简单扩展在转换过程中补全编号:

  1. 创建caption-numbering.rb脚本:
    Asciidoctor::Extensions.register do
      postprocessor do
        process do |document, output|
          table_idx = 1
          figure_idx = 1
          # 补全表格编号
          output = output.gsub(/<w:p[^>]*class="table-caption"[^>]*>(.*?)<\/w:p>/) do
            "<w:p class=\"table-caption\">Table #{table_idx}. #{$1}</w:p>".tap { table_idx += 1 }
          end
          # 补全图片编号
          output = output.gsub(/<w:p[^>]*class="figure-caption"[^>]*>(.*?)<\/w:p>/) do
            "<w:p class=\"figure-caption\">Picture #{figure_idx}. #{$1}</w:p>".tap { figure_idx += 1 }
          end
          output
        end
      end
    end
    
  2. 转换时加载扩展:
    asciidoctor-docx -r ./caption-numbering.rb your-document.adoc
    

方法3:文档内手动维护编号变量

利用Asciidoctor的属性自增功能,在文档内手动管理编号:

  • 文档开头定义初始变量:
    :table-number: 1
    :figure-number: 1
    
  • 表格标题写法:
    [caption="Table {table-number}. Test name table"]
    |===
    | Column 1 | Column 2
    | Data 1   | Data 2
    |===
    :table-number: {table-number} + 1
    
  • 图片标题写法:
    image::test.png[caption="Picture {figure-number}. Test figure"]
    :figure-number: {figure-number} + 1
    

这种方式无需外部依赖,适合小型文档场景。

内容的提问来源于stack exchange,提问作者LirikaLirikall

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 13:24:51