Search code examples
luapandocquarto

Removing data attributes in HTML file with pandoc lua filter


When compiling a document from Quarto, there are some divs with data- attribute values that I'd like to remove. These have specific names like data-output_encrypt:

<div class="cell" data-output_encrypt="key1">
...
</div>

Based on examples I've seen in the docs, etc, I'm imagining the lua filter will look something like this:

if FORMAT:match 'html' then
  function Div (elem)
    elem.attributes.data-output_encrypt = nil
    return elem
  end
end

...but this leads to a syntax error:

ERROR: remove_data.lua:3: syntax error near '-'

I'm guessing because attribute names can't contain hyphens (because they are table keys?). I know I'm on the right track because I can set arbitrary attributes without hyphens (e.g., "data"); I'm just not sure how to deal with the hyphen.

How can I modify/delete the data-output_encrypt attribute?


Solution

  • There are two things to note here.

    Firstly, attributes name in pandoc Div are prefixed with data- when converted into HTML div tag attributes. So to access them with lua filter you do the table indexing by using only the part after data- as a key.

    Secondly, elem.attributes is actually a lua table with attribute names as keys and attribute values as values corresponding to the table keys. And to access a value, you can either use table[key] syntax, where key could be numeric, string or any value except nil or NaN, or if the key is a string constant, you can use it as table. Key where this is only valid if the string consists of underscores, letters, and numbers, but shouldn't start with a number. (See here for details with example)

    So either of the below should work,

    elem.attributes.output_encrypt = nil
    -- or
    elem.attributes["output_encrypt"] = nil
    

    However, I prefer the second approach. Because conventionally HTML div attributes are written in kebab-case (separated with dash) most of the time, in which case you must need to follow the second approach.