Alexis Le Provost
Alexis Le Provost

Reputation: 2073

How link to any local file with markdown syntax?

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?

Upvotes: 198

Views: 321334

Answers (11)

Yves Guillo
Yves Guillo

Reputation: 11

With Markdown Reader on Chrome [file Name](//C:/Path/to/file/file.md) worked.
So, replacing file:///C:/Path/to/file/file.md with //C:/Path/to/file/file.md worked for me.

Upvotes: 1

Alex T
Alex T

Reputation: 415

You can in fact do almost anything behind an MD hyperlink by utilising Windows shortcuts.

To test this for yourselves, create a link like *[Typora](.\Typora.exe.lnk)* and place an actual Windows shortcut into the folder alongside the markdown file (TestMe.MD) called Typora.exe.lnk.

Then open the MD file in Typora and Ctrl-Click the link, and it should execute whatever you linked in that shortcut, opened in any folder you specified in that shortcut (or locally if you left it blank). In this case, it would be another instance of Typora of course. :)

I use this for instruction documents that include links to executables and scripts to follow certain documented procedures that are complicated enough to forget. The especially great thing about this is of course that you can have parameters in the link files, and you can even write scripts to interact with the human reading the document.

The world becomes your oyster. :)

Upvotes: 1

lineage
lineage

Reputation: 895

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.

Upvotes: 2

Ali80
Ali80

Reputation: 8686

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!

Upvotes: 57

Oubi&#241;a
Oubi&#241;a

Reputation: 59

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)

Upvotes: 5

drifty0pine
drifty0pine

Reputation: 346

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!

Upvotes: 8

Zikoat
Zikoat

Reputation: 655

If you have spaces in the filename, try these:

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

First one seems more reliable

Upvotes: 28

Akul Aggarwal
Akul Aggarwal

Reputation: 71

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.

Upvotes: 1

BringBackCommodore64
BringBackCommodore64

Reputation: 5470

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

Upvotes: 214

BarryPye
BarryPye

Reputation: 2082

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) 

Upvotes: 37

Chris
Chris

Reputation: 137183

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.

Upvotes: 42

Related Questions