如何使用 markdown 语法 link 到任何本地文件?

How link to any local file with markdown syntax?

我有一个包含多个 links 的本地降价文件,我希望 links 转到本地文件,如 pdf。

我使用以下语法:

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

但是当我在 Firefox 页面中打开 markdown 文件并单击 link 时,没有任何反应。

我到底错过了什么?是否可以打开本地文件?

你如何打开渲染的 Markdown?

如果您通过 HTTP 托管它,即您通过 http://https:// 访问它,大多数现代浏览器将拒绝打开本地链接,例如file://。这是一个 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

该页面上列出了一些解决方法,但我的建议是尽可能避免这样做。

您 link 到本地文件的方式与您 link 到本地图像的方式相同。下面是 link 文件 start_caQtDM_7id.sh 与降价源相同目录中的示例:

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

None 个答案对我有用。但是在 BarryPye 的回答中受到启发,我发现它在使用相对路径时有效!

# 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

如果该文件与 .md 所在的目录位于同一目录中,则只需输入 [Click here](MY-FILE.md) 即可。

否则,可以从项目的根目录创建一个路径。所以如果整个project/git-repo根目录都叫'my-app',想指向my-app/client/read-me.md,那就试试[My hyperlink](/client/read-me.md).

至少从 Chrome 起有效。

在弄乱了@BringBackCommodore64 的回答后我明白了

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

所有这些都在 Visual Studio Code 中进行了测试并且可以正常工作,

注意:打开项目的绝对路径和相对路径在编辑器中有效,但在markdown预览模式下无效!

如果文件名中有空格,请尝试以下操作:

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

第一个比较靠谱

这是一个老问题,但对我来说,它似乎仍然没有完整回答 OP 的问题。根据我的经验,在使用 Firefox 'Markdown Viewer' 插件时,选择的关于安全问题的答案实际上通常 而不是 问题。此外,OP 似乎正在使用 MS-Windows,因此存在指定不同驱动器的附加问题。

因此,这里是 Windows(以及我见过的其他 Markdown 渲染器)上的 'Markdown Viewer' 插件的更完整但更简单的答案:只需输入本地路径即可通常,如果它是绝对路径,请确保以斜杠开头。所以:

[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)

根据他们的示例,最后一个可能是 OP 正在寻找的内容。 请注意,这也可用于显示目录而不是文件。

虽然晚了,希望对你有所帮助!

Thank you drifty0pine!

第一个解决方案,有效!

[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)

但我需要放置更多 ../ 直到我的文件所在的文件夹,如下所示:

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