How link to any local file with markdown syntax?

183,521

Solution 1

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.

Solution 2

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

Solution 3

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) 

Solution 4

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!

Solution 5

If you have spaces in the filename, try these:

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

First one seems more reliable

Share:
183,521
Alexis Le Provost
Author by

Alexis Le Provost

Updated on October 18, 2021

Comments

  • Alexis Le Provost
    Alexis Le Provost about 1 year

    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?

  • Alexis Le Provost
    Alexis Le Provost over 7 years
    As I said, I open my markdown file with Mozilla Firefox 40.0.3 with the plug-in 'Markdown Viewer'.
  • Katrin Leinweber
    Katrin Leinweber over 7 years
    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.
  • HoldOffHunger
    HoldOffHunger over 4 years
    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?
  • WestCoastProjects
    WestCoastProjects about 4 years
    How is this different from @BarryPye's answer?
  • Yuriy Kulikov
    Yuriy Kulikov over 3 years
    This "!" is important! Thanks!
  • Seymour
    Seymour over 2 years
    It is not clear what is your problem and what you are asking. Is the question: which of the file name is more reliable?
  • RichTBreak
    RichTBreak over 2 years
    @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.)
  • BarryPye
    BarryPye over 2 years
    It's part of the Github flavored markdown syntax to render an image, see "Images" under guides.github.com/features/mastering-markdown
  • jbustamovej
    jbustamovej over 2 years
    It's important not to have a space between the ] and ( characters.
  • Thinh NV
    Thinh NV about 2 years
    your Note is very important!
  • boardtc
    boardtc over 1 year
    Thanks! No need for ! when in same directory
  • gigabot over 1 year
    @jbustamovej If there are spaces in the filename, see this answer by Zikoat
  • John Jesus
    John Jesus over 1 year
    Yes, for absolute path, putting a slash before the drive name works for me [link](/J:/dir/filename.md)
  • user1325696
    user1325696 about 1 year
    As of today relative link to opened project become broken in VSCode. Just now updated to 1.61.1 and it is broken.
  • Ali80
    Ali80 about 1 year
    @user1325696, yeah, it still works in editor though.
  • nekovolta
    nekovolta 11 months
    @BringBackCommodore64 how do you open a PDF file that is in another path (another local folder in your computer)?
  • BringBackCommodore64
    BringBackCommodore64 11 months
    @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/
  • Partha Mandal 11 months
    Perfect! [file](<./file with spaces.md>) was what i was looking for
  • Mikko Rantalainen
    Mikko Rantalainen 10 months
    @HoldOffHunger: the syntax ./ or ../ is not Linux specific but defined in URL parsing specification: url.spec.whatwg.org/#path-state