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?

Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
Alexis Le Provost
  • 1,803
  • 2
  • 11
  • 9

9 Answers9

184

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
BringBackCommodore64
  • 4,910
  • 3
  • 27
  • 30
  • 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? – HoldOffHunger Jul 04 '18 at 20:48
  • 5
    How is this different from @BarryPye's answer? – WestCoastProjects Nov 29 '18 at 07: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 01 '20 at 00:36
  • 2
    It's important not to have a space between the ] and ( characters. – jbustamovej Aug 26 '20 at 08:04
  • @jbustamovej If there are spaces in the filename, see [this answer](https://stackoverflow.com/a/61224440/12131087) by Zikoat – gigabot Apr 29 '21 at 19:43
  • 1
    @BringBackCommodore64 how do you open a PDF file that is in another path (another local folder in your computer)? – nekovolta Jan 19 '22 at 12:32
  • 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/` – BringBackCommodore64 Jan 20 '22 at 17:06
  • 1
    @HoldOffHunger: the syntax `./` or `../` is not Linux specific but defined in URL parsing specification: https://url.spec.whatwg.org/#path-state – Mikko Rantalainen Feb 18 '22 at 12:40
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.

Community
  • 1
  • 1
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
  • 1
    As I said, I open my markdown file with Mozilla Firefox 40.0.3 with the plug-in 'Markdown Viewer'. – Alexis Le Provost Sep 15 '15 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. – Katrin Leinweber Sep 15 '15 at 13:13
41

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!

Ali80
  • 6,333
  • 2
  • 43
  • 33
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) 
BarryPye
  • 1,975
  • 2
  • 18
  • 19
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

Zikoat
  • 373
  • 3
  • 9
  • 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 '20 at 14:09
  • 1
    Perfect! `[file](<./file with spaces.md>)` was what i was looking for – Partha Mandal Jan 21 '22 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 01 '23 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!

drifty0pine
  • 136
  • 1
  • 3
6

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)
Oubiña
  • 61
  • 1
  • 1
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.

Akul Aggarwal
  • 61
  • 2
  • 6
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.

lineage
  • 792
  • 1
  • 8
  • 20