Preserve line breaks in title using pandoc

吃可爱长大的小学妹 提交于 2019-12-04 01:02:31
% Higgelty Pigglety Pop! \
  or \
  There Must Be More to Life
% Maurice Sendak

Pandoc Markdown enables the escaped_line_breaks extension by default:

A backslash followed by a newline is also a hard line break. Note: in multiline and grid table cells, this is the only way to create a hard line break, since trailing spaces in the cells are ignored.

When using YAML metadata blocks, the following works, too:

---
title: |
    | First line
    | Second line
---

Found the idea in this thread.

tarleb

A very general, but less simple method is to use raw HTML to indicate line breaks, and to convert them into proper line breaks using a pandoc filter. Below is a Lua filter which translates any <br> (or <br />) in the source into a hard line break.

--- Transform a raw HTML element which contains only a `<br>`
-- into a format-indepentent line break.
function RawInline (el)
  if el.format:match '^html' and el.text:match '%<br ?/?%>' then
    return pandoc.LineBreak()
  end
end

Save the file as linebreaks.lua.

The title could then be written as

% Higgelty Pigglety Pop!<br>or<br>There Must Be More to Life
% Maurice Sendak

The above script must be passed to pandoc via the --lua-filter option:

$ pandoc -o test.pdf --lua-filter ./linebreaks.lua test.md

The advantage of this method is that it is more universal and also works in other locations that one might want to add line breaks. E.g., a line break in a header can be added using the same syntax: # first line<br>second line.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!