-
Hi, Finishing and trying to add an option to a Lua filter, I stumbled upon a problem that I reduced to some sort of MWE. Maybe it’s me doing wrong things (I’m not an expert at all) but this seems quite strange to me. Consider this markdown file (note the foo class): ---
title: "Untitled"
---
## Some title
![Some caption](teaching.jpg){.foo}
## Without caption
![](teaching.jpg)
One more paragraph. And then this filter: local classes = {"aclass", "anotherclass"}
local function bblocks (blocks)
local newClasses = {}
local originalClasses = {}
-- Go from end to start to avoid problems with shifting indices.
for i = #blocks-1, 1, -1 do
originalClasses = {}
newClasses = classes
if blocks[i].t == "Figure" then
originalClasses = blocks[i].content[1].content[1].classes
for k,v in pairs(originalClasses) do table.insert(newClasses, v) end
blocks[i].content[1].content[1].classes = newClasses
print("For my Figure:")
for k,v in pairs(blocks[i].content[1].content[1].classes) do print(v) end
elseif blocks[i].t == "Para" then
originalClasses = blocks[i].content[1].classes
for k,v in pairs(originalClasses) do table.insert(newClasses, v) end
blocks[i].content[1].classes = newClasses
print("For my Para:")
for k,v in pairs(blocks[i].content[1].classes) do print(v) end
end
end
return blocks
end
return {{Blocks = bblocks}} Then, when I invoke:
I get the expected output in the console:
But the generated AST contains also a foo class in the Para block:
Since the console output is correct and Blocks are evaluated last to first (so the one with the wanted foo class is the last evaluated), I can’t figure what I’m doing wrong… |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Just from a quick glance: for k,v in pairs(originalClasses) do table.insert(newClasses, v) end
|
Beta Was this translation helpful? Give feedback.
-
Well, to sum up, this is the way they are different (in my original code, it’s ok in the new one):
I now understand that in the console, it just worked by chance, because my image without the class |
Beta Was this translation helpful? Give feedback.
You are not actually initializing
newClasses
. You're re-assigning it by reference toclasses
, but when you mutate it after that the original table forclasses
gets updated.