MarkdownMonster icon indicating copy to clipboard operation
MarkdownMonster copied to clipboard

Feature Request: Improve command `Insert link` (Ctrl+K)

Open jbridgy opened this issue 2 years ago • 2 comments

The result of inserting for example c:\Users\jb\Documents\subdir\file name with spaces.md into c:\Users\jb\Documents\test.md with command Insert link (Ctrl+K) of MM 2.9.10 is by default:

[](c:\Users\jb\Documents\subdir\file%20name%20with%20spaces.md)

This link has multiple issues:

  1. The Link Text is empty if nothing was selected before pressing Ctrl+K.
  2. The Link contains backslashes (\) instead of slashes (/).
  3. The Link is absolute although it could be relative to the current file. Usually both files belong to the same working tree. So the absolute link gets invalid when the working tree is checked out at a different place. The absolute link also does not work in web-UI of the remote repo.

I suggest to change the default result as follows:

["file name with spaces.md"](subdir/file%20name%20with%20spaces.md)

The following rules have been applied:

  1. Use the original file name as default Link Text if it is empty. Additionally enclose it in double quotes if it contains spaces.
  2. Replace each \ in the link with a /.
  3. Make the link relative to the current file if the linked file and the current file reside on the same volume.

jbridgy avatar Jun 04 '23 23:06 jbridgy

If you embed a file that is more than a couple of levels removed from the current one, the link will be embedded as a fixed path. Arguably this should be a file://xxx/ path, but frankly that's not going to work any better unless the file exists in that path, so really that's a non-issue with a fully qualified physical path.

As to relative path embedding:

If you want relative paths you can add configuration into your root folder or the YAML header of the document that specifies a root path. More info here:

When this is set MM searches up the hierarchy for files and uses that path as the / root folder for creating relative paths.

Otherwise, anything further back than 2 parent paths up from the current path is considered an absolute path.

Ultimately if you're working on a project that has 'root' you should add one of these marker files into the root folder of the project - that's the most reliable way to get clean project relative paths.

RickStrahl avatar Jul 29 '23 17:07 RickStrahl

If you embed a file that is more than a couple of levels removed from the current one, the link will be embedded as a fixed path. Arguably this should be a file://xxx/ path, but frankly that's not going to work any better unless the file exists in that path, so really that's a non-issue with a fully qualified physical path.

The main issue is that command Insert link (Ctrl+K) of MM does not convert an absolute Windows file path to a relative URL whenever possible and probably most users strongly dislike doing this tedious conversion manually. In the original post I forgot to point out that the behavior of Insert link depends on how the local file path is entered into the Link: field. There are two cases:

  1. The local file path is entered directly by typing or pasting.
  2. The local file path is entered indirectly by the ... button.

The example in the original post refers to case 1. In this case MM never converts an absolute local path to a relative one. After all it converts the spaces to %20 when you press the Paste Link button. image

In case 2 the behavior of MM is better. It converts an absolute local path to a relative one if the directory of the current file contains the linked file. image image

So reconciling these two cases should actually be the first improvement.

When this is set MM searches up the hierarchy for files and uses that path as the / root folder for creating relative paths.

Otherwise, anything further back than 2 parent paths up from the current path is considered an absolute path.

Why do you restrict the conversion? I suggest to make a file path relative whenever possible, that is as already mentioned above, relative to the current file if the linked file and the current file formally reside on the same volume. By "formally" I mean, that the linked file is not required to be existent when you create the link. Maybe Insert link (Ctrl+K) should have an option like use absolute path to suppress the conversion in rare special cases.

Ultimately if you're working on a project that has 'root' you should add one of these marker files into the root folder of the project - that's the most reliable way to get clean project relative paths.

This route is MM-specific. Therefore it is impractical for my environment and presumably for most others.

jbridgy avatar Jul 31 '23 16:07 jbridgy

  1. is correct. Whatever you type in manually is what you get. The space encoding is not fixing up paths - it's required in order for links to render correctly.

  2. I took a stab at this and I think this now does more or less what you're looking for:

  • Relative paths will now always be used
  • EXCEPT: when the relative path walks back all the way to the Root path (ie. c:\ or \\ ) and then back out.

So say you're in ~\Notes\test.md and you reference c:\temp\test.md - that will generate an absolute file path url. However, if you reference ~\Documentation\test.md that will produce relative path.

RickStrahl avatar Apr 24 '24 07:04 RickStrahl

Closing due to late response and feature creep. If there are still concerns lets start a new issue with the specific issues.

RickStrahl avatar Apr 24 '24 08:04 RickStrahl