Copy/paste that ensures correct relative links
See original GitHub issueWhat is the problem?
Copy/paste markdown between files will break relative links (e.g. to images or local files).
While it is easy to capture images and paste them into markdown (e.g. using https://marketplace.visualstudio.com/items?itemName=mushan.vscode-paste-image), there is no easy way to move markdown content between files if both files are in different folders.
This is a serious limitation, as it makes it very difficult to rearrange content, a process that is trivial in any word processor.
How can I reproduce it?
Create a file first.md
in folder path/to/file/a/
containing a link to an image.
### This is file path/to/file/a/first.md
![Some Image]("2020-08-09-09-20-21.png")
Then copy and paste the markdown into a file second.md
in folder path/to/file/b
.
The link in the 2nd file will be broken, as the relative path now points to the wrong folder.
Is there any error message in the console?
No
Solution
We need modified copy/paste commands that either updates relative paths, or copy linked files as well (setting copyLinkedFiles
).
Copy should convert every path referenced in the file to an absolute path, i.e. ![Some Image]("path/to/file/a/first.md/2020-08-09-09-20-21.png")
.
Paste should convert any absolute path to a path relative to the 2nd file. In the case above, this would be ![Some Image]("../a/first.md/2020-08-09-09-20-21.png")
.
If copyLinkedFiles=true
, paste should copy all referenced files under folder a to folder b (potentially preserving subfolders, e.g. if the file is in a folder “images” on a
it should be copied to a folder “images” on b
as well.
If the first path and the second path reside on different drives, the linked files should be copied by default.
Note: Always using absolute paths is not a solution, as in this case the markdown files can’t be moved around freely (e.g. onto a USB or network drive).
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
Sounds good. Maybe someone with more experience than me with extensions will comment, I am still learning. Thank you!
It could be possible to check the existence of relative links and show warnings and code actions. Though it’s not a direct solution, I still think this can help a lot.
Please also note that there are many corner cases in URIs.
BTW, markdown-it provides a
MarkdownIt.parse()
method. Then, it might be feasible to build a language server on it to gain more knowledge of a document.