taqueci / redmine_wysiwyg_editor

Redmine WYSIWYG Editor plugin
GNU General Public License v2.0
114 stars 27 forks source link

Feature request — replace spaces with %20 in the link dialog #88

Closed randylang closed 4 years ago

randylang commented 4 years ago

Our installation is on a windows server and we have lots of paths with white space in them.

When we insert a link of the form file:///\\our-server\engineering files\somewhere

We have to manually insert %20 in place of the white space.

If the WYSIWYG editor could do this it would make things much easier.

taqueci commented 4 years ago

Hi @randylang

It may be good feature for IE user. I will consider. Please wait patiently!

taqueci commented 4 years ago

Note to self:

Hex Character URI path UNC path
0x20 SPACE - -
0x21 ! x x
0x22 " - -
0x23 # - x
0x24 $ - x
0x25 % - -
0x26 & - x
0x27 ' x x
0x28 ( x x
0x29 ) x x
0x2a * x -
0x2b + - x
0x2c , - x
0x2d - x x
0x2e . x x
0x2f / - -
0x30 - 0x39 0-9 x x
0x3a : - -
0x3b ; - x
0x3c < - -
0x3d = - x
0x3e > - -
0x3f ? - -
0x40 @ - x
0x41 - 0x5a A-Z x x
0x5b [ - x
0x5c \ - -
0x5d ] - x
0x5e ^ - x
0x5f _ x x
0x60 ` - x
0x61 - 0x7a a-z x x
0x7b { - x
0x7c PIPE - -
0x7d } - x
0x7e ~ x x
taqueci commented 4 years ago

Hi @randylang

I have no environment to test an link to Windows server. The latest browsers may not support it.

Could you please test branch issuue/88?

randylang commented 4 years ago

I will try the branch you provided this weekend.

I don’t think you need a windows server to try this. If someone pastes a file path that contains spaces, only the portion of the link be for the first space is interpreted as a link the rest shows up as text in Redmine. The issue doesn’t occur with links to Redmine because the spaces are automatically converted. For example, if you copy the url to another wiki page and paste it into the new page it pastes with the spaces converted. I don’t know whether the browser or Redmine is doing the conversion, but now that I think about it, I’ll bet it’s the browser. Since the file links are copied from Windows File Explorer, they don’t get converted.

I think the equivalent task on a Unix machine would have the same effect. Copy the file path from a console, not a file browser, then paste to the wiki page, I’ll bet the white space is not converted.

I don’t use Unix/Linux much these days so maybe I’m missing a detail.

randylang commented 4 years ago

It works if you put one more slash after the scheme:

  var linkUri = function(url) {
    if (/^\\\\/.test(url)) {
      // Convert UNC path to URL
      return 'file://' + url.split('\\').map(function(s) {
        return encodeURIComponent(s);
      }).join('/');
    }

I tested it with the following links, all three work:

Input text: \\server-name\Users\Engineering\2 - Instrumentation User Manuals\Oscilloscopes Expected result: "Test Link":file:///\\server-name\Users\Engineering\2 - Instrumentation User Manuals\Oscilloscopes

Input text: https://servername/redmine/documents/45 Expected result: "Second Test Link":https://servername/redmine/documents/45

Input text: /redmine/documents/45 Expected result: "Third Test Link":/redmine/documents/45

randylang commented 4 years ago

The expected results in my last post are not exactly right, it does correctly encode the URI. It’s difficult in my setup to make these posts when I’m connected to the server so I cut corners.

"Test Link":file:///\\server-name\Users\Engineering\2%20-%20Instrumentation%20User Manuals\Oscilloscopes

taqueci commented 4 years ago

Hi @randylang

Sorry for late reply.

I guess you can do it by the following codes.

However, the conversion is not reasonable for me because the result does not comply with UNC link path specification.

I'm afraid I can not accept your request. Please use the following code in your private repository.

diff --git a/assets/javascripts/redmine_wysiwyg_editor.js b/assets/javascripts/redmine_wysiwyg_editor.js
index 6f5d2fd..11c9c3f 100644
--- a/assets/javascripts/redmine_wysiwyg_editor.js
+++ b/assets/javascripts/redmine_wysiwyg_editor.js
@@ -987,6 +987,17 @@ RedmineWysiwygEditor.prototype._toTextTextile = function(content) {
     return (attr.length > 0) ? attr.join('') + '.' : '';
   };

+  var linkUri = function(url) {
+    if (/^\\\\/.test(url)) {
+      // Convert UNC path to URL
+      return 'file:///' + url.split('\\').map(function(s) {
+        return encodeURIComponent(s);
+      }).join('\\');
+    }
+
+    return url;
+  };
+
   var NT = '<notextile></notextile>';

   var converters = [{
@@ -1079,7 +1090,7 @@ RedmineWysiwygEditor.prototype._toTextTextile = function(content) {
         return gluableContent(content, node, ' ');
       } else {
         var titlePart = node.title ? ' (' + node.title + ')' : '';
-        var c = '"' + content +  titlePart + '":' + href;
+        var c = '"' + content +  titlePart + '":' + linkUri(href);

         return gluableContent(c, node, NT);
       }
diff --git a/test/test.js b/test/test.js
index 51a89ea..3fb69b0 100644
--- a/test/test.js
+++ b/test/test.js
@@ -124,6 +124,13 @@ suite('Redmine WYSIWYG Editor', function() {
       assert.equal(x._toTextTextile(content), expected);
     });

+    test('UNC path link', function() {
+      var content = '<a href="\\\\server-name\\Users\\Engineering\\2 - Instrumentation User Manuals\\Oscilloscopes">Test Link</a>';
+      var expected = '"Test Link":file:///\\\\server-name\\Users\\Engineering\\2%20-%20Instrumentation%20User%20Manuals\\Oscilloscopes';
+
+      assert.equal(x._toTextTextile(content), expected);
+    });
+
     test('Abbreviation', function() {
       var content = '<abbr title="Richard Matthew Stallman">RMS</abbr>';
       var expected = 'RMS(Richard Matthew Stallman)';
taqueci commented 4 years ago

redmine_wysiwyg_editor-issue88.zip