在 Linux 中把多个 Markdown 文件转换成 HTML 或其他格式
Markdown pandoc
![](/data/attachment/album/202104/07/095441bztj6cz68j89568u.jpg)


很多时候我与 Markdown 打交道的方式是，先写完一个文件，然后把它转换成 HTML 或其他格式。也有些时候，需要创建一些新的文件。当我要写多个 Markdown 文件时，通常要把他们全部写完之后才转换它们。


我用 `pandoc` 来转换文件，它可以一次性地转换所有 Markdown 文件。


Markdown 格式的文件可以转换成 .html 文件，有时候我需要把它转换成其他格式，如 epub，这个时候 [pandoc](https://pandoc.org/) 就派上了用场。我更喜欢用命令行，因此本文我会首先介绍它，然而你还可以使用 [VSCodium](https://vscodium.com/) 在非命令行下完成转换。后面我也会介绍它。


### 使用 pandoc 把多个 Markdown 文件转换成其他格式（命令行方式）


你可以在 Ubuntu 及其他 Debian 系发行版本终端输入下面的命令来快速开始：



```
sudo apt-get install pandoc

```

本例中，在名为 `md_test` 目录下我有四个 Markdown 文件需要转换。



```
[email protected]:~/Documents/md_test$ ls -l *.md
-rw-r--r-- 1 bdyer bdyer 3374 Apr  7  2020 file01.md
-rw-r--r-- 1 bdyer bdyer  782 Apr  2 05:23 file02.md
-rw-r--r-- 1 bdyer bdyer 9257 Apr  2 05:21 file03.md
-rw-r--r-- 1 bdyer bdyer 9442 Apr  2 05:21 file04.md
[email protected]:~/Documents/md_test$

```

现在还没有 HTML 文件。现在我要对这些文件使用 `pandoc`。我会运行一行命令来实现：


* 调用 `pandoc`
* 读取 .md 文件并导出为 .html


下面是我要运行的命令：



```
for i in *.md ; do echo "$i" && pandoc -s $i -o $i.html ; done

```

如果你不太理解上面的命令中的 `;`，可以参考 [在 Linux 中一次执行多个命令](https://itsfoss.com/run-multiple-commands-linux/)。


我执行命令后，运行结果如下：



```
[email protected]:~/Documents/md_test$ for i in *.md ; do echo "$i" && pandoc -s $i -o $i.html ; done
file01.md
file02.md
file03.md
file04.md
[email protected]:~/Documents/md_test$

```

让我再使用一次 `ls` 命令来看看是否已经生成了 HTML 文件:



```
[email protected]:~/Documents/md_test$ ls -l *.html
-rw-r--r-- 1 bdyer bdyer  4291 Apr  2 06:08 file01.md.html
-rw-r--r-- 1 bdyer bdyer  1781 Apr  2 06:08 file02.md.html
-rw-r--r-- 1 bdyer bdyer 10272 Apr  2 06:08 file03.md.html
-rw-r--r-- 1 bdyer bdyer 10502 Apr  2 06:08 file04.md.html
[email protected]:~/Documents/md_test$

```

转换很成功，现在你已经有了四个 HTML 文件，它们可以用在 Web 服务器上。


pandoc 功能相当多，你可以通过指定输出文件的扩展名来把 Markdown 文件转换成其他支持的格式。不难理解它为什么会被认为是[最好的写作开源工具](https://itsfoss.com/open-source-tools-writers/)。


### 使用 VSCodium 把 Markdown 文件转换成 HTML（GUI 方式）


就像我们前面说的那样，我通常使用命令行，但是对于批量转换，我不会使用命令行，你也不必。VSCode 或 [VSCodium](https://itsfoss.com/vscodium/) 可以完成批量操作。你只需要安装一个 Markdown-All-in-One 扩展，就可以在一次运行中转换多个 Markdown 文件。


有两种方式安装这个扩展：


* VSCodium 的终端
* VSCodium 的插件管理器


通过 VSCodium 的终端安装该扩展：


1. 点击菜单栏的 `终端`。会打开终端面板
2. 输入，或[复制下面的命令并粘贴到终端](https://itsfoss.com/copy-paste-linux-terminal/)：



```
codium --install-extension yzhang.markdown-all-in-one

```

**注意**：如果你使用的 VSCode 而不是 VSCodium，那么请把上面命令中的 `codium` 替换为 `code`


![](/data/attachment/album/202104/07/095532nmz18hxed6dxldqr.jpg)


第二种安装方式是通过 VSCodium 的插件/扩展管理器：


1. 点击 VSCodium 窗口左侧的块区域。会出现一个扩展列表，列表最上面有一个搜索框。
2. 在搜索框中输入 “Markdown All in One”。在列表最上面会出现该扩展。点击 “安装” 按钮来安装它。如果你已经安装过，在安装按钮的位置会出现一个齿轮图标。


![](/data/attachment/album/202104/07/095533wp55yjpxx322u25h.jpg)


安装完成后，你可以打开含有需要转换的 Markdown 文件的文件夹。


点击 VSCodium 窗口左侧的纸张图标。你可以选择文件夹。打开文件夹后，你需要打开至少一个文件。你也可以打开多个文件，但是最少打开一个。


当打开文件后，按下 `CTRL+SHIFT+P` 唤起命令面板。然后，在出现的搜索框中输入 `Markdown`。当你输入时，会出现一列 Markdown 相关的命令。其中有一个是 `Markdown All in One: Print documents to HTML` 命令。点击它：


![](/data/attachment/album/202104/07/095533wohb4vq7ph1qqq6m.jpg)


你需要选择一个文件夹来存放这些文件。它会自动创建一个 `out` 目录，转换后的 HTML 文件会存放在 `out` 目录下。从下面的图中可以看到，Markdown 文档被转换成了 HTML 文件。在这里，你可以打开、查看、编辑这些 HTML 文件。


![](/data/attachment/album/202104/07/095534g25m4i2ws5554i5w.jpg)


在等待转换 Markdown 文件时，你可以更多地集中精力在写作上。当你准备好时，你就可以把它们转换成 HTML —— 你可以通过两种方式转换它们。




---


via: <https://itsfoss.com/convert-markdown-files/>


作者：[Bill Dyer](https://itsfoss.com/author/bill/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[lxbwolf](https://github.com/lxbwolf) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![Warp Terminal](/assets/images/warp-terminal.webp)


![Warp Terminal](/assets/images/warp-terminal.webp)

Many times, when I use Markdown, I work on one file and when I’m done with it, I convert it to HTML or some other format. Occasionally, I have to create a few files. When I do work with more than one Markdown file, I usually wait until I have finished them before I convert them.

I use pandoc to convert files, and it’s possible convert all the Markdown files in one shot.

Markdown can convert its files to .html, but if there’s a chance that I will have to convert to other formats like epub, [pandoc](https://pandoc.org/?ref=itsfoss.com) is the tool to use. I prefer to use the command line, so I will cover that first, but you can also do this in [VSCodium](https://vscodium.com/?ref=itsfoss.com) without the command line. I’ll cover that too.

## Converting multiple Markdown files to another format with Pandoc [command line method]

To get started quickly, Ubuntu, and other Debian distros can type the following commands in the terminal:

`sudo apt-get install pandoc`


In this example, I have four Markdown files in a directory called md_test.

```
bdyer@bill-pc:~/Documents/md_test$ ls -l *.md
-rw-r--r-- 1 bdyer bdyer 3374 Apr 7 2020 file01.md
-rw-r--r-- 1 bdyer bdyer 782 Apr 2 05:23 file02.md
-rw-r--r-- 1 bdyer bdyer 9257 Apr 2 05:21 file03.md
-rw-r--r-- 1 bdyer bdyer 9442 Apr 2 05:21 file04.md
bdyer@bill-pc:~/Documents/md_test$
```


There are no HTML files yet. Now I’ll use Pandoc to do its magic on the collection of files. To do this, I run a one-line command that:

- calls pandoc
- reads the .md files and exports them as .html

This is the command:

`for i in *.md ; do echo "$i" && pandoc -s $i -o $i.html ; done`


If you are not aware already, `;`

is used for [running multiple commands at once in Linux](https://itsfoss.com/run-multiple-commands-linux/).

Here’s what the display looks like once I have executed the command:

```
bdyer@bill-pc:~/Documents/md_test$ for i in *.md ; do echo "$i" && pandoc -s $i -o $i.html ; done
file01.md
file02.md
file03.md
file04.md
bdyer@bill-pc:~/Documents/md_test$
```


Let me use the `ls`

command once more to see if HTML files were created:

```
bdyer@bill-pc:~/Documents/md_test$ ls -l *.html
-rw-r--r-- 1 bdyer bdyer 4291 Apr 2 06:08 file01.md.html
-rw-r--r-- 1 bdyer bdyer 1781 Apr 2 06:08 file02.md.html
-rw-r--r-- 1 bdyer bdyer 10272 Apr 2 06:08 file03.md.html
-rw-r--r-- 1 bdyer bdyer 10502 Apr 2 06:08 file04.md.html
bdyer@bill-pc:~/Documents/md_test$
```


The conversion was a success, and you have four HTML files ready to go on the Web server.

Pandoc is quite a versatile tool. Similarly, you can [use Pandoc to convert the markdown files to PDF](https://tiiny.host/blog/pandoc-markdown-pdf/) or some other supported format by specifying the extension of the output files. You can understand why it is considered among the [best open source tools for writers](https://itsfoss.com/open-source-tools-writers/).

## Converting Markdown files to HTML using VSCodium [GUI method]

Like I’ve said earlier, I normally use the command line, but I don’t always use it for batch conversions, and you don’t have to either. VSCode or [VSCodium](https://itsfoss.com/vscodium/) can do the job. You just need to add one extension, called: *Markdown-All-in-One* which will allow you to convert more than one Markdown file in one run.

There are two ways to install the extension:

- VSCodium’s terminal
- VSCodium’s plug-in manager

To install the extension through VSCodium’s terminal:

- Click on
`Terminal`

on the menu bar. The terminal panel will open - Type, or
[copy-and-paste, the following command in the terminal](https://itsfoss.com/copy-paste-linux-terminal/):

`codium --install-extension yzhang.markdown-all-in-one`


**Note**: If you’re using VSCode instead of VSCodium, replace the word, `codium`

, in the above command, with `code`


![vscodium terminal](https://itsfoss.com/content/images/wordpress/2021/04/vscodium_terminal-800x564.jpg)

The second way to install is through VSCodium’s plug-in, or extension, manager:

- Click on the blocks on the left side of the VSCodium window. A list of extensions will appear. At the top of the list, there will be a search bar.
- In the search bar, type:
`Markdown All in One`

. The extension will be listed at the top of the list. Click on the`Install`

button to install it. If it is already installed, a gear icon will appear in place of the install button.

![vscodium extension](https://itsfoss.com/content/images/wordpress/2021/04/vscodium_extension_select-800x564.jpg)

Once the extension is installed, you can open the folder that contains the Markdown files you want to convert.

Click on the paper icon located on the left side of the VSCodium window. You’ll be given the opportunity to choose your folder. Once a folder is open, you’ll need to open at least one file. You can open as many files as you want, but one is the minimum.

Once a file is open, bring up the Command Palette by pressing `CTRL+SHIFT+P`

. Then, start typing `Markdown`

in the search bar that will appear. As you do this, a list of Markdown related commands will appear. One of these will be `Markdown All in One: Print documents to HTML`

command. Click on that one.

![vscodium markdown function options](https://itsfoss.com/content/images/wordpress/2021/04/vscodium_markdown_function_options-800x564.jpg)

You’ll be asked to choose a folder containing the files. This is so an output directory (called `out`

) can be made and this is where the HTML files will go. The image below shows that the HTML was made after exporting the Markdown documents. From here, you can open, view, and edit the HTML as you wish.

![vscodium html filelist shown](https://itsfoss.com/content/images/wordpress/2021/04/vscodium_html_filelist_shown-800x564.jpg)

By waiting to convert your Markdown files, you can concentrate more on writing. Conversion to HTML can come when you’re ready – and you have two ways to get that done.