Pandoc Lua how to create an id from a string?

Viewed 47

I need to mimic pandoc's way of turning a header's text into an acceptable id string in a Lua filter: remove special characters, turn spaces into dashes.

Is there a command to sanitize a string that way?

Ex: Les élèves d'hier becomes: les-eleves-dhier

1 Answers

There is no such function built-in, but you can hack one by round-tripping a heading through one of the formats that pandoc supports. This will force the creation of a label that you can use:

local function make_id (inlines, via)
  local via = via or 'html'
  local heading = pandoc.Header(1, inlines)
  local temp_doc = pandoc.Pandoc{heading}
  local roundtripped_doc = pandoc.read(pandoc.write(temp_doc, via), via)
  return roundtripped_doc.blocks[1].identifier
end

Example:

print(make_id(pandoc.Inlines "Les élèves d'hier"))

Will print les-élèves-dhier. I believe the output should be the same for all roundtripable formats, but may it isn't.

Related