khomutovspace As mentioned in my previous answer, you are creating invalid HTML. The browser can't fix that therefore puts these closing </p>
tags. Please note that Markdown wraps stand-alone lines in paragraphs.
Your Markdown looks like this:
{frame-start}
# Heading
Text here ...
{frame-end}
The Markdown is rendered according to its specs to the following:
<p>{frame-start}</p>
<h1>Heading</h1>
<p>Text here ....
{frame-end}</p>
Now in your template you replace the frame divs:
<p><div></p> <-- INVALID!!!
<h1>Heading</h1>
<p>Text here ...
</div></p> <-- INVALID!!!
The browser tries to clean up the mess then:
<p></p><div><p></p> <-- NOW VALID!!!
<h1>Heading</h1>
<p>Text here ...
</p></div><p></p> <-- NOW VALID!!!
More readable it will look like:
<p></p>
<div>
<p></p>
<h1>Heading</h1>
<p>Text here ...</p>
</div>
<p></p>
The result you get is therefore expected and totally following the standards. However in order to fix yout template simply add one more replacement to clean up:
@{ text |
markdown |
replace ( ... ) |
replace ( ... ) |
replace ('/\\<p\\>\\<\\/p\\>/s', '')
}