如何在Quarto Lua过滤器中使用YAML元数据实现自定义配置?
解决Quarto扩展Lua过滤器读取YAML元数据的问题
要让你的Quarto扩展支持通过YAML配置自定义水平线的颜色和样式,需要在Lua过滤器中添加元数据捕获逻辑,具体修改如下:
1. 修改Lua过滤器代码
-- 全局变量存储默认配置 local hr_color = "0000FF" local hr_style = "double" -- 从YAML元数据读取自定义配置 function Meta(meta) local hr_config = meta["horizontal-rule"] or {} -- 用元数据值覆盖默认值,未配置则保留默认 hr_color = hr_config.color or hr_color hr_style = hr_config.style or hr_style return meta end function HorizontalRule (elem) if FORMAT == 'docx' then local horizontallinerule = string.format([[<w:p> <w:pPr> <w:pStyle w:val="HorizontalRule"/> <w:ind w:firstLine="0"/> <w:jc w:val="center"/> <w:pBdr> <w:bottom w:val="%s" w:color="%s"/> </w:pBdr> </w:pPr> <w:r> <w:t></w:t> </w:r> </w:p>]], hr_style, hr_color) return pandoc.RawBlock('openxml', horizontallinerule) end end
2. 在Quarto文档中配置YAML选项
在你的Quarto文档开头的YAML区域添加自定义配置,比如:
--- title: "你的文档标题" horizontal-rule: color: "FF0000" # 自定义颜色(无#的十六进制值) style: "single" # 自定义线条样式,可选值包括single、double、dotted、dashed等 ---
关键说明
- 原来的代码未添加元数据读取逻辑,所以无法获取YAML配置。Lua过滤器需要通过
Meta函数捕获文档元数据,将配置值存入全局变量后,才能在HorizontalRule处理函数中使用。 - 样式值必须符合OpenXML的
<w:bottom>标签w:val属性规范,颜色值需为不带#的6位十六进制字符串。
内容的提问来源于stack exchange,提问作者ttalVlatt
相关产品推荐
相关产品推荐

