Search code examples
luayamlmarkdownpandoc

Printing the value of the keywords field of the YAML header in a Markdown file as an inline list


This question is a spin-off from Writing thanks and keywords from Markdown to docx via Pandoc.

In my specific setting I am converting the contents of a Markdown file to a docx document via Pandoc. The Markdown file opens with a YAML header for certain metadata among which is a list of keywords as required by the publisher for the target docx document. This list is entered as the value of the keywords YAML field as per the MWE below.

My problem is that the list is printed in the target document as a paragraph list; i.e. it starts a new paragraph and each item is printed in its own paragraph. The wished for output is instead an inline list; i.e., a single (logical, not necessarily typographic) line of items (the keywords) separated by some constant delimiter (in my case, a comma, but this particular is unsubstantial).

How can this be achieved?

---
title: The Title
author: The Author
thanks: |
  The author wishes to thank certain support.
abstract: |
  This is the abstract.
keywords: [one, two, three, four]
---
   
# A Heading

Text body. 

I have considered a Lua filter along the lines of

function Meta (meta)
  meta.abstract = meta.abstract .. {pandoc.Emph("Keywords: ")} .. meta.keywords
  return meta
end

but this returns precisely the list of keywords as a paragraph list rather than an inline list.


Solution

  • Pandoc tries to guess what we want, but in this case we should be a bit more explicit: we intersperse comma and space separators between the keywords, then use that to construct a separate paragraph for keywords:

    function Meta (meta)
      -- insert comma and space between keywords
      local keywords, sep = pandoc.Inlines{}, pandoc.Inlines ', '
      for i, kw in ipairs(meta.keywords or {}) do
        keywords:extend(kw)
        if i ~= #meta.keywords then
          keywords:extend(sep)
        end
      end
    
      -- Append keywords paragraph to abstract
      meta.abstract = meta.abstract ..
        {pandoc.Para({pandoc.Emph 'Keywords: '} .. keywords)}
      return meta
    end