文件嵌入
从 docsify 4.6 开始,可以嵌入任何类型的文件。
你可以把这些文件嵌入为视频、音频、iframe 或代码块;如果是 Markdown 文件,甚至可以直接嵌入到当前文档中。
例如,下面嵌入了一个 Markdown 文件。你只需要这样写:
[filename](_media/example.md ':include')example.md 的内容就会直接显示在这里:
This is from the
example.md
你可以查看 example.md 的原始内容。
通常情况下,这样的语法会被编译成一个链接;但在 docsify 中,如果你添加了 :include,它就会被嵌入。引号可以随意使用单引号或双引号。
也可以使用外部链接,只需替换链接目标即可。如果想使用 gist 的 URL,请参阅嵌入 gist 一节。
嵌入的文件类型
目前,文件扩展名会被自动识别,并以不同的方式嵌入。
支持以下类型:
- iframe
.html、.htm - markdown
.markdown、.md - audio
.mp3 - video
.mp4、.ogg - code 其他文件扩展名
当然,你也可以强制指定类型。例如,通过设置 :type=code,可以把 Markdown 文件作为代码块嵌入。
[filename](_media/example.md ':include :type=code')你会看到:
> This is from the `example.md`带 YAML Front Matter 的 Markdown
Front Matter 常见于 Jekyll 等博客系统,用于定义文档的元数据。front-matter.js 包可以从文档中提取元数据(front matter)。
嵌入 Markdown 时,YAML front matter 会从渲染内容中去除,这种情况下无法使用其中的属性。
<script src="//cdn.jsdelivr.net/npm/docsify@5/dist/plugins/front-matter.min.js"></script>[filename](_media/example-with-yaml.md ':include')你只会看到正文内容
This is from the
example-with-yaml.md
嵌入代码片段
有时你并不想嵌入整个文件。也许你只需要其中几行,但又希望在 CI 中编译和测试该文件。
[filename](_media/example.js ':include :type=code :fragment=demo')在代码文件中,你需要用 /// [demo] 行把片段包起来(片段前后各一行)。 你也可以使用 ### [demo]。默认情况下只会省略标识符本身。如果想在片段输出中省略包含标识符的整行,请添加 :omitFragmentLine 选项。当你的代码片段是 HTML 等内容、并且希望 Docsify 片段标识符不出现在 HTML 源码中时,这个选项很有用:在源文件中写 <!-- /// [demo] --> 并配合 :omitFragmentLine,--> 就不会出现在 Docsify 的代码片段中。
示例:源文件 _media/example.js 中已经包含了 /// [demo] 标识符:
[filename](_media/example.js ':include :type=code')import fetch from 'fetch';
const URL = 'https://example.com';
const PORT = 8080;
/// [demo]
const result = fetch(`${URL}:${PORT}`)
.then(response => {
return response.json();
})
.then(myJson => {
console.log(JSON.stringify(myJson));
});
/// [demo]
result.then(console.log).catch(console.error);加上 :fragment=demo 后,结果如下:
[filename](_media/example.js ':include :type=code :fragment=demo')const result = fetch(`${URL}:${PORT}`)
.then(response => {
return response.json();
})
.then(myJson => {
console.log(JSON.stringify(myJson));
});标签属性
如果把文件嵌入为 iframe、audio 或 video,你可能需要给这些标签设置属性。
提示
注意,对于 audio 和 video 类型,docsify 默认会添加 controls 属性。当你想添加更多属性时,如有需要,controls 属性也要手动加上。
[filename](_media/example.mp4 ':include :type=video controls width=100%')[cinwell website](https://cinwell.com ':include :type=iframe width=100% height=400px')看到了吗?你只需要直接写属性就行。这些属性可以查阅 MDN。
代码块高亮
嵌入任何类型的源代码文件时,你可以指定高亮的语言,也可以让它自动识别。
[](_media/example.html ':include :type=code text')⬇️
<style>
html,
body {
margin: 0;
padding: 0;
min-height: 100%;
}
body {
background-color: #4158d0;
background-image: linear-gradient(
43deg,
#4158d0 0%,
#c850c0 46%,
#ffcc70 100%
);
color: #fff;
font-family: sans-serif;
}
main {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
</style>
<main>
<p>Example HTML Page</p>
</main>提示
如何设置高亮?请查看这里。
嵌入 gist
你可以把 gist 作为 Markdown 内容或代码块嵌入。这基于文件嵌入一节开头介绍的方法,只是把目标换成 gist 的 raw URL。
提示
这里不需要任何插件或修改应用配置。事实上,即使你通过插件或配置允许加载外部脚本,从 gist 复制的 "Embed" script 标签也不会加载。
确定 gist 的元数据
先在 gist.github.com 上打开一个 gist。本指南使用下面这个 gist:
从 gist 中找出以下信息:
| 字段 | 示例 | 说明 |
|---|---|---|
| Username | anikethsaha | gist 的所有者。 |
| Gist ID | c2bece08f27c4277001f123898d16a7c | gist 的标识符,在 gist 的整个生命周期内保持不变。 |
| Filename | content.md | gist 中某个文件的名称。即使 gist 只有一个文件,也需要它才能嵌入。 |
你需要用这些信息拼出目标文件的 raw gist URL,格式如下:
https://gist.githubusercontent.com/USERNAME/GIST_ID/raw/FILENAME
下面是基于示例 gist 的两个例子:
- https://gist.githubusercontent.com/anikethsaha/f88893bb563bb7229d6e575db53a8c15/raw/content.md
- https://gist.githubusercontent.com/anikethsaha/f88893bb563bb7229d6e575db53a8c15/raw/script.js
提示
你也可以直接点击 gist 文件上的 Raw 按钮获取 raw URL。但如果采用这种方式,请务必删除 raw/ 和文件名之间的修订号,使 URL 符合上面的格式。否则 gist 更新后,嵌入的 gist 不会显示最新内容。
接下来按照下面任意一节,把 gist 嵌入到 Docsify 页面中。
渲染 gist 中的 Markdown 内容
这是一种把内容无缝嵌入文档的好方法,读者无需跳转到外部链接。它很适合在多个仓库的文档站点之间复用同一个 gist,比如安装说明。无论 gist 属于你自己的账号还是其他用户,这种方法都同样适用。
格式如下:
[LABEL](https://gist.githubusercontent.com/USERNAME/GIST_ID/raw/FILENAME ':include')例如:
[gist: content.md](https://gist.githubusercontent.com/anikethsaha/f88893bb563bb7229d6e575db53a8c15/raw/content.md ':include')渲染结果如下:
LABEL 可以是任意文本。链接失效时,它会作为 备用(fallback) 信息显示,所以在这里重复写上文件名很有用,便于修复失效的链接。它也让嵌入的元素一目了然。
渲染 gist 中的代码块
格式与上一节相同,只是在 alt 文本中加上 :type=code。与嵌入的文件类型一节一样,语法高亮会根据扩展名(例如 .js 或 .py)推断,所以 type 设置为 code 即可。
格式如下:
[LABEL](https://gist.githubusercontent.com/USERNAME/GIST_ID/raw/FILENAME ':include :type=code')例如:
[gist: script.js](https://gist.githubusercontent.com/anikethsaha/f88893bb563bb7229d6e575db53a8c15/raw/script.js ':include :type=code')渲染结果如下: