使用 python markdown 库在标题上生成永久链接
我想知道如何使用 python markdown 库从以下标记生成永久链接:
A header
========
A paragraph
所需的输出类似于
<span id="a-header"></span>
<h1>
A header
<a class="headerlink" title="Permalink to this headline" href="#a-header">¶</a>
</h1>
<p>A paragraph</p>
Answer:
谢谢 @BlaXpirit (查看答案)
使用 headerid python markdown 扩展并输入以下内容:
# A header [¶](#a-header) {#a-header}
A paragraph
这会生成以下输出:
<h1 id="a-header">
A header
<a href="#a-header">¶</a>
</h1>
然后使用一些 css 样式来获取常见输出,例如:
h1 a{visibility:hidden;}
h1:hover a{visibility:visible;}
I was wondering how to generate permalinks from the following markup, using python markdown library:
A header
========
A paragraph
The desired output would be something like
<span id="a-header"></span>
<h1>
A header
<a class="headerlink" title="Permalink to this headline" href="#a-header">¶</a>
</h1>
<p>A paragraph</p>
Answer:
Thanks @BlaXpirit (see answer)
Use headerid python markdown extension and input the following:
# A header [¶](#a-header) {#a-header}
A paragraph
This generates the following output:
<h1 id="a-header">
A header
<a href="#a-header">¶</a>
</h1>
Then use some css styling to get the common output, something like :
h1 a{visibility:hidden;}
h1:hover a{visibility:visible;}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Python 中的 Markdown 有一个扩展< /a> 就是这样做的。
它还允许您为标头指定您喜欢的 id,如下所示:
Markdown in Python has an extension that does this.
It also lets you specify an id you like for the header, like this:
您可以使用 Python-Markdown 的 目录 扩展生成永久链接。 Python-Markdown 文档 指出,如果可能,最好传递一个实例扩展名而不是字符串。
另一个问题的答案展示了如何通过将永久链接选项设置为字符串来更改段落符号。
You can generate permalinks with the Table of Contents extension for Python-Markdown. The Python-Markdown documentation notes that when possible it is preferable to pass an instance of an extension rather than a string.
An answer to a different question shows how you can change the paragraph symbol by setting the permalink option to a string.
Pandoc 根据您想象的规则将唯一标识符与每个标头关联起来:id 是小写标题,空格替换为连字符。这用于生成 HTML 和 LaTeX 以及其他输出格式的可选目录。在 HTML 中,它会自动生成可链接的 id,特别是可用于内部交叉引用; Markdown 语法是:
正如我们在 http:// johnmacfarlane.net/pandoc/README.html#header-identifiers-in-html
Pandoc associates a unique identifier to each header based on the rule you imagined: the id is the downcase heading, spaces replace by hyphens. This is used to generate optional tables of contents for HTML and LaTeX and other output formats. In HTML it automatically makes linkable ids and in particular can be used for internal cross references; the markdown syntax is:
as we read in the user's guide at http://johnmacfarlane.net/pandoc/README.html#header-identifiers-in-html