171

I have a local markdown file containing several links and I want that links head to local file like pdf.

I use the following syntax:

[my link](file:///C:/my_file.pdf)

But when I open my markdown file into a Firefox page and click on the link, nothing happens.

What exactly have I missed? Is it possible to open local file?

3

9 Answers 9

186

None of the answers worked for me. But inspired in BarryPye's answer I found out it works when using relative paths!

# Contents from the '/media/user/README_1.md' markdown file:

Read more [here](./README_2.md) # It works!
Read more [here](file:///media/user/README_2.md) # Doesn't work
Read more [here](/media/user/README_2.md) # Doesn't work
9
  • 2
    Starting off a url path with "./" implies look in local dir, while starting with "/" implies "look in base dir." This is a widespread Linux convention. I suspect maybe you didn't have the full base dir down? Jul 4, 2018 at 20:48
  • 5
    How is this different from @BarryPye's answer? Nov 29, 2018 at 7:33
  • 5
    @javadba The exclamation point (!) in BarryPye's answer renders the file as an image. The question refers to a link to another file. (I know, slow, slow, response to your question but I thought others might want to know the answer.)
    – RichTBreak
    Jun 1, 2020 at 0:36
  • 2
    It's important not to have a space between the ] and ( characters. Aug 26, 2020 at 8:04
  • 3
    @nekovolta Go back in the tree directory and then navigate to wherever the PDF is. Example: [Book](../../docs/mybook.pdf). So i go back with ../../ (assuming the PDF folder is two folders down from the local MD file) and then enter its folder with /docs/ Jan 20, 2022 at 17:06
42

After messing around with @BringBackCommodore64 answer I figured it out

[link](file:///d:/absolute.md)    # absolute filesystem path
[link](./relative1.md)            # relative to opened file
[link](/relativeToProject.md)     # relative to opened project

All of them tested in Visual Studio Code and working,

Note: The absolute and relative to opened project path work in editor but don't work in markdown preview mode!

6
  • 5
    your Note is very important!
    – Thinh NV
    Oct 27, 2020 at 3:48
  • As of today relative link to opened project become broken in VSCode. Just now updated to 1.61.1 and it is broken. Oct 17, 2021 at 12:20
  • @user1325696, yeah, it still works in editor though.
    – Ali80
    Oct 17, 2021 at 14:06
  • What do you mean by "opened project"? Looks like a file. Oct 20, 2022 at 4:01
  • @gargoylebident the root path of the folder which vscode is opened in
    – Ali80
    Oct 20, 2022 at 5:17
41

How are you opening the rendered Markdown?

If you host it over HTTP, i.e. you access it via http:// or https://, most modern browsers will refuse to open local links, e.g. with file://. This is a security feature:

For security purposes, Mozilla applications block links to local files (and directories) from remote files. This includes linking to files on your hard drive, on mapped network drives, and accessible via Uniform Naming Convention (UNC) paths. This prevents a number of unpleasant possibilities, including:

  • Allowing sites to detect your operating system by checking default installation paths
  • Allowing sites to exploit system vulnerabilities (e.g., C:\con\con in Windows 95/98)
  • Allowing sites to detect browser preferences or read sensitive data

There are some workarounds listed on that page, but my recommendation is to avoid doing this if you can.

2
  • 1
    As I said, I open my markdown file with Mozilla Firefox 40.0.3 with the plug-in 'Markdown Viewer'. Sep 15, 2015 at 13:08
  • Ah, you didn't mention that plug-in before ;-) I tried it with the HTML export (what I assumed "Firefox page" meant) of MarkdownPad. Please check the plug-in's help/support channels. Sep 15, 2015 at 13:13
35

You link to a local file the same way you link to local images. Here is an example to link to file start_caQtDM_7id.sh in the same directory as the markdown source:

![start_caQtDM_7id.sh](./start_caQtDM_7id.sh) 
2
22

If you have spaces in the filename, try these:

[file](./file%20with%20spaces.md)
[file](<./file with spaces.md>)

First one seems more reliable

3
  • It is not clear what is your problem and what you are asking. Is the question: which of the file name is more reliable?
    – Seymour
    Apr 17, 2020 at 14:09
  • 1
    Perfect! [file](<./file with spaces.md>) was what i was looking for Jan 21, 2022 at 18:27
  • The original question referenced links to .pdf documents but all the answers reference .md documents. For anyone skimming through the answers - this answer works with .pdf documents. Your document should open in your chosen .pdf viewer if you ctrl-click the link in your .md document.
    – Cam Cairns
    May 1 at 23:33
6

This is a old question, but to me it still doesn't seem to have a complete answer to the OP's question. The chosen answer about security being the possible issue is actually often not the problem when using the Firefox 'Markdown Viewer' plug-in in my experience. Also, the OP seems to be using MS-Windows, so there is the added issue of specifying different drives.

So, here is a little more complete yet simple answer for the 'Markdown Viewer' plug-in on Windows (and other Markdown renderers I've seen): just enter the local path as you would normally, and if it is an absolute path make sure to start it with a slash. So:

[a relative link](../../some/dir/filename.md)
[Link to file in another dir on same drive](/another/dir/filename.md)
[Link to file in another dir on a different drive](/D:/dir/filename.md)

That last one was probably what the OP was looking for given their example. Note this can also be used to display directories rather than files.

Though late, I hope this helps!

1
  • 1
    Yes, for absolute path, putting a slash before the drive name works for me [link](/J:/dir/filename.md)
    – John Jesus
    Jun 9, 2021 at 20:58
5

Thank you drifty0pine!

The first solution, it´s works!

[a relative link](../../some/dir/filename.md)
[Link to file in another dir on same drive](/another/dir/filename.md)
[Link to file in another dir on a different drive](/D:/dir/filename.md)

but I had need put more ../ until the folder where was my file, like this:

[FileToOpen](../../../../folderW/folderX/folderY/folderZ/FileToOpen.txt)
1

If the file is in the same directory as the one where the .md is, then just putting [Click here](MY-FILE.md) should work.

Otherwise, can create a path from the root directory of the project. So if the entire project/git-repo root directory is called 'my-app', and one wants to point to my-app/client/read-me.md, then try [My hyperlink](/client/read-me.md).

At least works from Chrome.

1
  • Thanks! No need for ! when in same directory
    – boardtc
    Apr 28, 2021 at 13:57
1

It seems most Markdown viewers won't support non http(s) URLs. Since markdown does support some HTML tags, why not use that

<a href="file:///path/to/local/file.pdf">link</a>

This redirects fine.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.