Pandoc div parser help

I’m attempting to write a pandoc div parser/renderer, however, I’m running into one small issue that’s preventing me from finishing it.

When the div closing mark, :::, immediately follows a paragraph, i.e. no blank lines before it, the composite node ends up finishing before the mark, even though the mark ends up within the composite node. By adding a blank line before the mark, the node finishes at the correct spot.

In the example code, this should be visible by adding or removing a blank line before the mark, and the highlight should extend either all the way, or end on the line before. The goal is that it should always end after the mark.

I believe this is because of an interaction between the composite and endLeaf functions, but I cannot seem to figure it out! I would appreciate any insight others have into this problem.

Here is the playground example

Pandoc div example

You have a bug in your pandocDivComposite function—it should be calling line.addMarker instead of ctx.addElement. But you’ll also need the patch below—the library’s composite block parsing wasn’t really designed for this kind of closing-token style blocks (CommonMark’s own composite blocks all use leading line markup to continue the block). But with that patch and the change to line.addMarker, your code seems to work.

With your suggested change to useline.addMarker and the provided patch, the div parsing seems to work really well, including nested divs. Thank you sm, this is delightful!