我的目标是编写易于阅读的文档文件,以纯文本格式和html格式显示。
问题在于使用 Markdown 表。例如,我的表带有长字符串:
| Name | Description |
|-----------|------------------------------------------------------------------------------------------------------------------------------|
| some_name | Very very very long description for some_name property that should be easy to read even in plain text form even in html form |
这个markdown会生成普通的HTML,但是由于行很长,因此无法以纯文本格式读取它。
如果我写这样的话:
| Name | Description |
|-----------|--------------------------------|
| some_name | Very very very long description|
| | for some_name property that |
| | should be easy to read even in |
| | plain text form even in html |
| | form |
|-----------|--------------------------------|
然后,它很容易阅读,但是会生成难看的html演示文稿。
有什么办法可以将减价表中的长行换行吗?
最佳答案
这取决于您使用的Markdown实现。
例如,Pandoc的multiline_tables和grid_tables都支持多行单元格,但是,更流行的pipe_tables不支持。请注意,默认情况下这些功能均未启用,但必须通过选项明确启用,或作为Markdown变体的一部分启用。让我们依次看一下:
multiline_tables
这些表使用空白行分隔每一行。
------------------------------------------
Name Description
---------- -------------------------------
some_name Very very very long description
for some_name property that
should be easy to read even in
plain text form even in html
form
other_name A second row
------------------------------------------
我知道(至少)multiline_tables有两个问题:
grib_tables
这些本质上是Emacs table mode的克隆,也是restructuredtext's grid tables的更严格实现。
+------------+--------------------------------+
| Name | Description |
+============+================================+
| some_name | Very very very long description|
| | for some_name property that |
| | should be easy to read even in |
| | plain text form even in html |
| | form |
+------------+--------------------------------+
| other_name | A second row |
+------------+--------------------------------+
每行由
-
和+
字符的组合分隔。grid_tables的已知问题是:
pipe_tables
除了Pandoc之外,它们还受到许多实现的支持,包括PHP Markdown Extra,MultiMarkdown,GitHub Flavored Markdown,Python-Markdown *和Kramdown等。
| Name | Description |
| ---------- | ---------------------------------------------------------------------------------------------------------------------------- |
| some_name | Very very very long description for some_name property that should be easy to read even in plain text form even in html form |
| other_name | A second row |
请注意,该语法没有提供任何方法来定义一行的结束时间和另一行的开始时间(与其他类型的表不同)。由于您无法定义行之间的分隔,因此它唯一可行的方法是每一行都是自己的行。因此,一行不能包含多行文本。
最后,如Pandoc的文档所述:
Since the pipes indicate column boundaries, columns need not be vertically aligned.
在这种情况下,可以在文本编辑器中像这样更轻松地格式化表格:
| Name | Description |
| ---------- | ------------ |
| some_name | Very very very long description for some_name property that should be easy to read even in plain text form even in html form |
| other_name | A second row |
这肯定会有所帮助,并消除了手动换行的需要。长线不理想。但是,由于跨实现的广泛支持,对于大多数用户而言,它是最佳选择。
MultiMarkdown的表格文档提供了一个有趣的发现(原始内容为重点):
MultiMarkdown table support is designed to handle most tables for most people; it doesn’t cover all tables for all people. If you need complex tables you will need to create them by hand or with a tool specifically designed for your output format. At some point, however, you should consider whether a table is really the best approach if you find MultiMarkdown tables too limiting.
*全面披露:我是Python-Markdown的维护者
关于markdown - 在减价表中换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57165371/