-
I try to create a Lua filter to preserve HTML comments (but not any other HTML elements). function RawInline(el)
return nil
end
function RawBlock(el)
if starts_with('<!--', el.text) then
return el
else
return nil
end
end
return {{Inline = RawInline, Block = RawBlock}} It doesn't currently work. What might be the problem?
|
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 11 replies
-
I think you take it from here... |
Beta Was this translation helpful? Give feedback.
-
That's what my point 2 is about. Make sure you're using the right filter. For example, test with the following simple filter:
It should print out all the comments it encounters. /Edit: |
Beta Was this translation helpful? Give feedback.
-
return {{Inline = RawInline}}
This statement is wrong: it means that every Inline will be passed through your function RawInline, which should trigger an error on elements that don't have a text field (such as Emph). If you want to pass every RawInline through the function RawInline, simply remove the return statement: this is the default behaviour.
Returning nil from your function or returning the object without modifications does the same thing, i.e. doesn't change anything on the AST: see the page on Lua filters from “The return value of a filter function must be one of the following”. (By the way, if you want to delete an object, you must return an empty list {}.)
If I understand right, what you want to do is to alter the behaviour of a writer. This can be done with a custom writer: https://pandoc.org/custom-writers.html
|
Beta Was this translation helpful? Give feedback.
-
But if I simply remove it, the filter doesn't seem to do anything at
all...
That's the point 2 of my answer!
|
Beta Was this translation helpful? Give feedback.
-
Once again, you must return something other than the unmodified element or nil, since in both case nothing will be changed in the AST. One solution would be to return the comment as a RawInline pretending to be a raw GFM fragment. (This is more or less equivalent with using a custom writer as I suggested above.)
local function starts_with(start, str)
return str:sub(1, #start) == start
end
function RawInline(el)
if starts_with('<!--', el.text) then
return pandoc.RawInline('gfm', el.text)
end
end
|
Beta Was this translation helpful? Give feedback.
-
Now you know from the responses on Stackoverflow (and from a comment from me above if I remember well) how to suppress an element in the AST, so just do that in an else clause.
|
Beta Was this translation helpful? Give feedback.
-
Let me copy over my solution from StackOverflow. local function starts_with(start, str)
return str:sub(1, #start) == start
end
function RawInline(el)
if not starts_with('<!--', el.text) then
return {}
end
end As noted in the comments there, this is insufficient for your purpose. (I'd like to stress again that, in cases like this, a lot of everybody's time can be saved by including the example and expected output directly in the question). Since you don't want any HTML, it's best to use function RawInline (el)
return starts_with('<!--', el.text)
and pandoc.RawInline('markdown', el.text) -- pretend HTML comment is raw Markdown
or {} -- not an HTML comment, thus drop it
end |
Beta Was this translation helpful? Give feedback.
Let me copy over my solution from StackOverflow.
As noted in the comments there, this is insufficient for your purpose. (I'd like to stress again that, in cases like this, a lot of everybody's time can be saved by including the example and expected output directly in the question). Since you don't want any HTML, it's best to use
-t gfm-raw_html
. This also suppresses all HTML snippets, so we need to pretend that our comments are actually Markdown. Modifying the filter like this will do the trick: