mohzy83 / NppMarkdownPanel

Lightweight Notepad++ plugin to preview Markdown files
MIT License
224 stars 36 forks source link

Display images with a space character in the filename — %20 #39

Closed AlttiRi closed 1 year ago

AlttiRi commented 3 years ago

It should display a local image that has a space character ` in the filename if the image is included with escaping (%20`).

For example, I have an image 123 4.png.

I include it to .md file with: ![](123%203.png "").

It works fine, for example, both in GitHub and in IntelliJ IDEA. But this plugin does not work with this case.

AlttiRi commented 3 years ago

UPD. Not only a space character, but other chars too (which should be encoded). For example, if I have [image] example 1—1.png then I should include it as %5Bimage%5D%20example%201%E2%80%941.png.

andrzejQ commented 1 year ago

This is a bug under IE/Edge with local file links containing non US-ASCII chars, with escape chars in URL with %..

Can this be inserted in MarkdownPreviewForm.cs in line 128?

            //Unescape local file links containing non US - ASCII chars because of IE/Edge bug
            //But unescape all is probably too much...
            resultForBrowser = Uri.UnescapeDataString(resultForBrowser);

Tests in Zip: test nonAscii path.zip

andrzejQ commented 1 year ago
        public static MarkdownPipelineBuilder UseNonAsciiNoEscape(this MarkdownPipelineBuilder pipeline);
        //     Uses this extension to disable URI escape with % characters for non-US-ASCII
        //     characters in order to workaround a bug under IE/Edge with local file links containing
        //     non US-ASCII chars. DO NOT USE OTHERWISE.

but inserting it in MarkdigMarkdownGenerator.cs doesn't work:

        public string ConvertToHtml(string markDownText, string filepath)
        {
...
            var pipeline = new MarkdownPipelineBuilder()

                .UseNonAsciiNoEscape()
mohzy83 commented 1 year ago

This is a bug under IE/Edge with local file links containing non US-ASCII chars, with escape chars in URL with %..

Can this be inserted in MarkdownPreviewForm.cs in line 128?

            //Unescape local file links containing non US - ASCII chars because of IE/Edge bug
            //But unescape all is probably too much...
            resultForBrowser = Uri.UnescapeDataString(resultForBrowser);

I think its no good idea to unescape the complete document. This can lead to undesired side effects. It would be better to unescape only src attribute of img tags.

mohzy83 commented 1 year ago
        public static MarkdownPipelineBuilder UseNonAsciiNoEscape(this MarkdownPipelineBuilder pipeline);
        //     Uses this extension to disable URI escape with % characters for non-US-ASCII
        //     characters in order to workaround a bug under IE/Edge with local file links containing
        //     non US-ASCII chars. DO NOT USE OTHERWISE.

but inserting it in MarkdigMarkdownGenerator.cs doesn't work:

        public string ConvertToHtml(string markDownText, string filepath)
        {
...
            var pipeline = new MarkdownPipelineBuilder()

                .UseNonAsciiNoEscape()

Doesn't work for me neither. Actually this setting should produce the desired result.

andrzejQ commented 1 year ago

This can be inserted in MarkdownPreviewForm.cs in line 128 (desperate solution). (I have created pull request).

            //using System.Text.RegularExpressions;
            //string inp = " * %25%20x : `<img src=\"file:///C:/tmp/test%20nonAscii%20path/A%C4%85C%C4%87E/A%C4%84%2520(2).png\" />`";
            //outp:          * %25%20x : `<img src="file:///C:/tmp/test nonAscii path/AąCćE/AĄ%20(2).png" />`
            Regex regex = new Regex("src=\"file:///[^\"]+");
            resultForBrowser = regex.Replace(resultForBrowser, m => Uri.UnescapeDataString(m.Value));

nppMdP.tests.zip

mohzy83 commented 1 year ago

fixed in release 0.7.2