The context
I have written the following pandoc-filter
, which uses HTML details
and summary
elements (together with some CSS also given below) to produce collapsible sections when pandoc converts Markdown to HTML.
Sections begin with a Markdown header line (### Section title
) and it is assumed that after a header with level N the next header never has level N + 2 or deeper.
Javascript
let level = 0;
filter.stdio(function ({ t, c }, format, meta) {
if (t === "Header") {
const content = [
filter.RawBlock("html", `<details open><summary>`),
filter.Header(...c),
filter.RawBlock("html", `</summary>`)
];
for (let i = c[0]; i <= level; i++)
content.unshift(filter.RawBlock("html", `</details>`));
level = c[0];
return content;
}
});
CSS
details:not([open]) > summary {
list-style: disclosure-closed;
}
details[open] > summary {
list-style: disclosure-open;
}
The problem
It works nicely but produces invalid HTML (albeit one that the browser silently corrects): The closing </details>
elements at the end of the document are missing, and I could not find a way to add them because the function passed to filter.stdio
is not invoked again at the end of the Markdown document.
For example, when applied to the Markdown source of this question, my approach produces the following details
and summary
tags (other tags omitted):
summary { margin-left: 1em; }
details:not([open])>summary {
list-style: disclosure-closed;
}
details[open]>summary {
list-style: disclosure-open;
}
<details open>
<summary>
<h1>The context</h1>
</summary>
...
<details open>
<summary>
<h2>Javascript</h2>
</summary>
...
</details>
<details open>
<summary>
<h2>CSS</h2>
</summary>
...
</details>
</details>
<details open>
<summary>
<h1>The problem</h1>
</summary>
...
<!-- The </details> end tag is missing. -->
I could solve the problem by writing a pandoc filter that parses the JSON representation of the entire Markdown document and inserts all details
and summary
elements. But I would like to stick with the approach of filter.stdio
, which presents one JSON node at a time to the handler function.
How could I add the closing </details>
tags with the filter.stdio
approach?
2