homebrewery
homebrewery copied to clipboard
Possible fix for multiple inline divs in a block
This runs a split on the html input on inline divs and then parses each singly.
This solves the issue of
<div>1</div><div>2</div><div>3</div><div>4</div><div>5</div>
producing
<div>
<p>1</p>
</div>
<div>
2
</div>
<div>
3
</div>
<div>
4
</div>
<div>
5
</div>
However the results instead are
<div>
<p>
1
</p>
</div>
<div>
<p>
2
</p>
</div>
<div>
<p>
3
</p>
</div>
<div>
<p>
4
</p>
</div>
<div>
<p>
5
</p>
</div>
If the paragraph wrappers are undesired, we either need to not run parse on the div contents or strip the paragraph wrapper.
The tests are failing because the final </div>
is being duplicated, at least some of the time:
I haven't had a chance to look at the actual code yet to see what might be causing this duplication.
This does not fix nested divs
Our current behavior of parsing markdown inside block-level divs already deviates from the Commonmark standard to allow one specific case. This PR deviates even more (and suggests we should also accommodate <span>
and other tags)? I could see this ballooning into a bigger and bigger thing just fighting against the commonMark standard with more and more edge cases. (This doesn't handle nested Divs, nor <div> *hello* </div> *hello*
, nor <div></div> *hello* <div></div>
)
At that point, it would make more sense to just overwrite the whole Markdown HTML tokenizer instead and make it into a complete extension.
Does this have an issue it's linked to?
If we have no specific high-demand user case or issue that needs this behavior, I might suggest it's better to leave this alone. We should be encouraging users to avoid raw HTML in V3 anyway.
If this is something we want to support, I would suggest a simpler approach (just split the string on <div>
's and parse everything else as markdown :
//Processes the markdown within an HTML block if it's just a class-wrapper
renderer.html = function (html) {
if (html.match(/<\/?div.*?>/g)) {
// Split the input HTML into `<div>` and not div
const divBlocks = html.split(/(<\/?div.*?>)/g);
let result = '';
for (const block of divBlocks) {
if(block[0] !== '<')
result += Marked.parse(block);
else
result += block;
}
return result;
}
return html;
};
Closing pending discussion.