是否可以在 GitHub gist Markdown 的中心放置一张桌子? [英] Is it possible to have a table in the center in a GitHub gist Markdown?
问题描述
是否可以在 GitHub gist Markdown 的中心放置一张表格?如果可以,怎么做?
我使用以下语法在 Markdown 文件上创建了一个表格:
不知何故,桌子总是向左冲!!!|列 1|列 1|列 1||:----|:----:|----:||列 1|列 1|列 1|
但表被冲左,见
简而言之,这是不可能的.GitHub 不允许您定义自己的样式.
首先,请注意在 GitHub Flavored Markdown 规范(参见表格部分).正如您的示例所示,您知道可以将表格单元格中的文本居中,但这仅适用于单元格,对父表格没有影响(这是 HTML 和 CSS 的工作方式,并非特定于 Markdown 或 GitHub).
有几种方法可以为 HTML(由 Markdown 生成)定义自定义样式,但 GitHub 不允许.
一种方法是定义 CSS 规则.然而,在 spec 中,GitHub 明确声明他们这样做不允许 标签.
另一种方法是在 Markdown 文档中直接包含原始 HTML(使用内联样式).但是,出于安全原因,GitHub 对其允许的内容非常挑剔.在 Markup 项目中,他们定义了应用于他们支持的所有标记语言的过滤器(包括,但不限于 Markdown).在相关部分,文档解释(强调):
<块引用>- HTML 经过清理,积极删除可能伤害您和您的亲属的内容,例如
script
标签、inline-styles 和class
或id
属性.请参阅消毒过滤器了解完整信息白名单.
鉴于上述情况,根本不可能为 GitHub 上托管的文档定义自己的样式.也就是说,有些人希望能够在 Markdown 语法本身中定义样式.但是,原始 Markdown 规则 解释(强调):
<块引用>Markdown 的语法有一个目的:用作网络写作的格式.
Markdown 不是 HTML 的替代品,甚至不是 HTML 的替代品.它的语法非常小,仅对应于 HTML 标签的一个非常小的子集.这个想法不是创建一种语法,使插入 HTML 标签变得更容易.在我看来,HTML 标签已经很容易插入了.Markdown 的想法是让阅读、编写和编辑散文变得容易.HTML 是一种发布格式;Markdown 是一种书写格式.因此,Markdown 的格式语法只解决了可以用纯文本传达的问题.
对于 Markdown 语法未涵盖的任何标记,您只需使用 HTML 本身即可.
由于它不是发布格式",因此提供一种设置文档样式的方法超出了 Markdown 的范围.这给我们留下了 GitHub 明确禁止的方式.因此,无法在 GitHub 上将表格居中(或应用任何其他自定义样式).
<小时>顺便说一句,虽然 GitHub 使用 CommonMark 规范(带扩展)而不是原始 Markdown 规则,但我引用了原始规则,因为我引用的部分讨论了在创建 Markdown 时做出的各种设计决策背后的哲学.Markdown(和 CommonMark)的行为与该理念直接相关.虽然 CommonMark 规范没有进入设计决策(预计当它与 Markdown 不同时),但它确实 参考 我在上面引用的段落中讨论的一些要点.它在任何地方都与这种哲学相矛盾.因此,我认为它与我们应该对哪些是 CommonMark 以及哪些不属于 CommonMark 以及 GitHub Flavored Markdown 的部分抱有期望有关.
<小时>为了完整起见,让我们检查一下 OP 提供的每个示例.
第一个示例只是一个带有中间列对齐中心".如果我们查看源代码"(或使用浏览器的检查"工具),我们会看到生成了以下 HTML:
<头><tr><th align="left">Column1</th><th align="center">Column1</th><th align="right">Column1</th></tr></thead><tr><td align="left">Column1</td><td align="center">Column1</td><td align="right">Column1</td></tr></tbody>
请注意,
align="center"
仅定义在每行的中间单元格上.由于此类样式仅由子元素继承,而不是由父元素继承,因此这不会应用于整个表格.顺便说一句,HTML5 规范中甚至没有提到align
属性a>(我能找到);但是,在 HTML 4.01 规范中,您可以在table
元素或其任何子元素上定义align
属性,然后仅由该元素的子元素继承.当然,如上所述,Markdown 不提供一种机制来定义除单元格之外的任何东西的对齐方式.但即使您可以在table
元素上定义align
,规范解释说[t]his 属性指定数据的对齐方式和单元格中文本的对齐方式."因此,如果仍然不会影响表格在其父元素中的定位.第二个例子是一个包裹在
元素.查看源 HTML 会发现中的表格;
标记已被删除.其实看看GitHub的列入白名单的元素 表明center
元素不被允许并被删除.第三个示例尝试用
将表格包装在一个段落中align="center"
在段落上定义.但是,请注意(解释的)HTML:<p align="center"></p><表格><头><tr><th align="left">Column1</th><th align="center">Column1</th><th align="right">Column1</th></tr></thead><tr><td align="left">Column1</td><td align="center">Column1</td><td align="right">Column1</td></tr></tbody><p></p>
根据 HTML5 规范:
<块引用>p
元素的结束标记可以省略,如果p
元素后紧跟一个...table
... 元素.因此,段落实际上并没有包裹
table
,而是被表格的开始标记隐式关闭.但这让我很好奇.如果您使用
div
而不是段落会怎样.但是没有区别.正如我们之前确定的,align
属性只影响单元格文本.您需要指定一个style
来更改页面上表格的位置,Github 明确禁止定义您自己的样式.Is it possible to have a table in the center in a GitHub gist Markdown? If so, how?
I've used the following syntax to create a table on a Markdown file:
Somehow the table is always flushed to the left!!! |Column1|Column1|Column1| |:----|:----:|----:| |Column1|Column1|Column1|
But the table is flushed left, see https://gist.github.com/alvations/5095d3bcc0eec357c78f1c21a49e334f
Is it possible to have the table at the center of the page when viewing?
I've tried the suggestion from Is it possible to center tables in a markdown file? to use:
Somehow the table is always flushed to the left!!! <center> |Column1|Column1|Column1| |:----|:----:|----:| |Column1|Column1|Column1| </center>
And the table disappears when viewing, see https://gist.github.com/alvations/cd3495e7107b7701cf1cf1da2a839534
I've also tried How do I center an image in the README.md on GitHub?:
Still on the left!!! <p align="center"> |Column1|Column1|Column1| |:----|:----:|----:| |Column1|Column1|Column1| </p>
But it's still on the left, see https://gist.github.com/alvations/23c18681df7a6bbf175d0e8c2cfccba3
Images for all three versions above:
解决方案In short, it's not possible. GitHub does not allow you to define your own styling.
First, note that there is no mention of the ability to apply any styling to any block level types in the GitHub Flavored Markdown spec (see the tables section). As your examples show, you are aware that you can center text within table cells, but that only applies to the cells and has no effect on the parent table (which is how HTML and CSS work and is not specific to Markdown or GitHub).
There are a few ways to define custom styles for HTML (which Markdown generates), but GitHub does not permit them.
One such way is to define CSS rules. However, right in the spec, GitHub explicitly states that they do not allow
<style>
tags.Another way is to include raw HTML right in the Markdown document (with inline styles). However, for security reasons, GitHub is very selective about what they allow. In the Markup project they define the filters they apply to all markup languages they support (including, but not limited to Markdown). In pertinent part, the docs explain (emphasis added):
- The HTML is sanitized, aggressively removing things that could harm you and your kin—such as
script
tags, inline-styles, andclass
orid
attributes. See the sanitization filter for the full whitelist.
Given the above, it is simply not possible to define your own styling for documents hosted on GitHub. That said, some expect to be able to define styling within the Markdown syntax itself. However, the original Markdown rules explain (emphasis added):
Markdown’s syntax is intended for one purpose: to be used as a format for writing for the web.
Markdown is not a replacement for HTML, or even close to it. Its syntax is very small, corresponding only to a very small subset of HTML tags. The idea is not to create a syntax that makes it easier to insert HTML tags. In my opinion, HTML tags are already easy to insert. The idea for Markdown is to make it easy to read, write, and edit prose. HTML is a publishing format; Markdown is a writing format. Thus, Markdown’s formatting syntax only addresses issues that can be conveyed in plain text.
For any markup that is not covered by Markdown’s syntax, you simply use HTML itself.
As it is not a "publishing format," providing a way to style your document is out-of-scope for Markdown. Which leaves us with the ways which GitHub explicitly disallows. Therefore it is not possible to center a table (or apply any other custom styling) on GitHub.
As an aside, while GitHub uses the CommonMark spec (with extensions) rather than the original Markdown Rules, I make reference to the original rules as the section I quote from discusses the philosophy behind various design decisions made when creating Markdown. Markdown's (and CommonMark's) behaviors are directly related to that philosophy. While the CommonMark spec does not get into the design decisions (expect when it differs from Markdown), it does make reference to some of the points discussed in the very paragraph I quoted above. And nowhere does it contradict that philosophy. Therefore, I consider it relevant to the expectations we should have about what is and what is not part of CommonMark, and by extension, GitHub Flavored Markdown.
For completness, let's examine each of the examples provided by the OP.
The first example is simply a table with the middle column aligned "center". If we "view source" (or use the browser's "inspect" tool), we see the following HTML was generated:
<table> <thead> <tr> <th align="left">Column1</th> <th align="center">Column1</th> <th align="right">Column1</th> </tr> </thead> <tbody> <tr> <td align="left">Column1</td> <td align="center">Column1</td> <td align="right">Column1</td> </tr> </tbody> </table>
Note that
align="center"
is only defined on the middle cell of each row. As such styling is only inherited by children elements, not parent elements, this does not get applied to the the table as a whole. As an aside, thealign
attribute is not even mentioned in the HTML5 spec (that I could find); however, in the HTML 4.01 spec, you can define analign
attribute on atable
element or any of its children which is then inherited by the children of that element only. Of course as established above, Markdown does not provide a mechanism to define alignment on anything except the cells. But even if you could definealign
on thetable
element, the spec explains that "[t]his attribute specifies the alignment of data and the justification of text in a cell." Therefore, if would still have no effect on how the table is positioned in its parent element.The second example is a table wrapped in a
<center>
element. A look at the source HTML reveals that the<center>
tag was stripped out. In fact, a look at GitHub's whitelisted elements reveals thatcenter
elements are not allowed and stripped out.The third example attempts to wrap the table in a paragraph with
align="center"
defined on the paragraph. However, note the (interpreted) HTML:<p align="center"></p> <table> <thead> <tr> <th align="left">Column1</th> <th align="center">Column1</th> <th align="right">Column1</th> </tr> </thead> <tbody> <tr> <td align="left">Column1</td> <td align="center">Column1</td> <td align="right">Column1</td> </tr> </tbody> </table> <p></p>
According to the HTML5 spec:
A
p
element's end tag may be omitted if thep
element is immediately followed by an...table
... element.Therefore, the paragraph does not actually wrap the
table
, but is implicitly closed by the table's opening tag.But that got me curious. What if you used a
div
instead of a paragraph. But that makes no difference. As we've established earlier, thealign
attribute only effects cell text. You need to assign astyle
to change the position of a table on the page and Github explicitly disallows defining your own styles.
这篇关于是否可以在 GitHub gist Markdown 的中心放置一张桌子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文登录 关闭
扫码关注1秒登录发送“验证码”获取 | 15天全站免登陆- The HTML is sanitized, aggressively removing things that could harm you and your kin—such as