Preserve line breaks in title using pandoc

后端 未结 3 924
情话喂你
情话喂你 2021-01-04 04:18

Consider the following title block in pandoc-flavored Markdown:

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

相关标签:
3条回答
  • 2021-01-04 04:55
    % 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.

    0 讨论(0)
  • 2021-01-04 05:01

    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.

    0 讨论(0)
  • 2021-01-04 05:08

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

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

    Found the idea in this thread.

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