mkdocs:如何附加可下载的文件

huangapple go评论86阅读模式
英文:

mkdocs: how to attach a downloadable file

问题

我正在尝试找到一种方法来在构建中“附加”file1.ext,例如作为chapter1.md中的链接。有关如何实现这一点的任何建议吗?详细信息:我希望在单击时能够下载该文件。

英文:

I have a mkdocs project that resembles the following:

project
├─mkdocs.yml
├─docs
│ ├─home.md
│ ├─chapter1.md
│
├─static
  ├─file.ext
  ├─image.png

I am trying to find a way to "attach" file1.ext to the build, for instance as a link in chapter1.md.

Any suggestions how to achieve that? Detail: I want the file to be downloadable on click.

答案1

得分: 1

mkdocs中,要通过markdown使文件在点击时可下载,首先需要将以下内容添加到你的mkdocs.yml文件中:

markdown_extensions:
  - attr_list

然后在你的chapter1.md中,你可以为链接添加download属性,如下所示:

[file.ext](../static/file.ext){:download}

你甚至可以指定要下载的文件的新文件名:

[file.ext](../static/file.ext){:download="awesome-file"}

解释

MkDocs使用Python-Markdown将Markdown转换为HTML,它提供了灵活的扩展机制,使得可以在不必编辑实际源文件的情况下更改或扩展解析器的行为。

mkdocs.yml中的markdown_extensions设置指定了MkDocs的Python-Markdown扩展。
添加attr_list入口点启用了Python-Markdown属性列表扩展,它通过使用花括号{}和类似CSS的语法来支持HTML样式的属性。

示例:

假设我们想要在新标签中打开一个链接,我们可以这样实现:

[Google](https://www.google.com){:target="_blank"}
英文:

In mkdocs, to get the file to be downloadable on click using markdown, first you need to add this to your mkdocs.yml file :

markdown_extensions:
  - attr_list

and then in your chapter1.md you can add download attribute to your link ... like so :

[file.ext](../static/file.ext){:download}

Heck you can even specifies the new filename for the downloaded file:

[file.ext](../static/file.ext){:download="awesome-file"}

Explanation

MkDocs converts Markdown to HTML using Python-Markdown which offers a flexible extension mechanism, which makes it possible to change and/or extend the behavior of the parser without having to edit the actual source files.

The markdown_extensions in mkdocs.yml setting specifies the Python-Markdown extensions for MkDocs.
adding attr_list entry point enables the Python-Markdown attribute lists extension, which adds support for HTML-style attributes using curly brackets {} and a CSS-like syntax.

example:

let's say we want to open a link in new tab, we can achieve this like so :

[Google](https://www.google.com){:target="_blank"}

答案2

得分: 0

以下是已翻译的内容:

Edit:
通过 @WaLid LamRaoui 检查答案

Original:
使用以下代码修复它:

// 在 chapter1.md 中
<a href="../static/file.ext" download>file.ext</a>

Markdown 样式似乎不起作用:

[file.ext](file::///../static/file.ext)
英文:

Edit:
Check the answer by @WaLid LamRaoui

Original:
Fixed it with:

// in chapter1.md
<a href="../static/file.ext" download>file.ext</a>

the mardownish like did not work:

[file.ext](file::///../static/file.ext)

答案3

得分: -1

在你的 chapter1.md 文件中,你应该链接到你的 file1.ext 文件,这个文件应该位于 static 文件夹中。
你可以这样链接它:
[链接到 file1](../static/file1.ext)
之后,你可以构建你的项目。

英文:

In your chapter1.md file you should link your file1.ext, this file should be located in the static folder.
You can link it like:
[Link to file1](../static/file1.ext)
After this you can build your project.

答案4

得分: -2

将文件放入静态目录中:

  1. 将文件.ext移动或复制到项目结构中的静态目录中。

更新您的Markdown文件(chapter1.md)以包括一个指向文件的链接:
在chapter1.md文件中,您可以添加一个指向要附加的文件的Markdown链接。例如:
下载文件

根据您的项目结构和文件在静态目录内的实际位置,调整文件路径../static/file.ext。

英文:

Place the file in the static directory:

  1. Move or copy the file.ext to the static directory in your project structure.
Project
  ├─mkdocs.yml
  ├─docs
  | ├─home.md
  | ├─chapter1.md
  |
  ├─static
    ├─file.ext
    ├─image.png

2.Update your markdown file (chapter1.md) to include a link to the file:

In the chapter1.md file, you can add a Markdown link that points to the file you want to attach. For example:
[Download File](../static/file.ext)

3.Adjust the file path ../static/file.ext according to your project structure and the actual location of the file within the static directory

huangapple
  • 本文由 发表于 2023年5月18日 03:35:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76275641.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定