Skip to content

Conversation

@lightrabbit
Copy link

This issue #384 is caused by the fact that cheerio automatically adds closing tags for unclosed opening tags. However, when processing HTML fragments with html_block, the input HTML may not always be a complete document.

This leads to situations where:

<div><img src="...">
# some title
some text
</div>

is processed into:

<div><img src="..."></div>
# some title
some text
// Here, the </div> is automatically removed by cheerio.

The issue arises because cheerio assumes that any unclosed tags should be properly closed, which can cause unexpected behavior when working with incomplete HTML fragments.

@Nat-Faeeria
Copy link

Hey,
Noticed the same problem, didn't think about checking the PR.
Since the goal is only to format the src of images, a simpler solution could be

if (type !== 'html') {
  md.renderer.rules.html_block = function (tokens, idx) {
    var html = tokens[idx].content;
    html = html.replace(/<img\s+([^>]*?)src=["']([^"']+)["']([^>]*)>/gi, function (match, before, src, after) {
        var href = convertImgPath(src, filename);
        return `<img ${before}src="${href}"${after}>`;
    });
    return html;
  };
}

(full disclosure : this code is ai generated)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants