使用 python markdown 库在标题上生成永久链接

发布于 2024-10-31 22:56:24 字数 1004 浏览 1 评论 0原文

我想知道如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

霊感 2024-11-07 22:56:24

Markdown in Python has an extension that does this.
It also lets you specify an id you like for the header, like this:

A header            {#a-header}
========
孤君无依 2024-11-07 22:56:24

您可以使用 Python-Markdown 的 目录 扩展生成永久链接。 Python-Markdown 文档 指出,如果可能,最好传递一个实例扩展名而不是字符串。

import markdown
from markdown.extensions.toc import TocExtension

markup = """
A header
========

A paragraph
"""

html = markdown.markdown(markup, extensions=[TocExtension(permalink=True)])
print(html)


configs = {'toc': {'permalink': True}}
html = markdown.markdown(markup, extensions=['toc'], extension_configs=configs)
print(html)

另一个问题的答案展示了如何通过将永久链接选项设置为字符串来更改段落符号。

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.

import markdown
from markdown.extensions.toc import TocExtension

markup = """
A header
========

A paragraph
"""

html = markdown.markdown(markup, extensions=[TocExtension(permalink=True)])
print(html)


configs = {'toc': {'permalink': True}}
html = markdown.markdown(markup, extensions=['toc'], extension_configs=configs)
print(html)

An answer to a different question shows how you can change the paragraph symbol by setting the permalink option to a string.

撑一把青伞 2024-11-07 22:56:24

Pandoc 根据您想象的规则将唯一标识符与每个标头关联起来:id 是小写标题,空格替换为连字符。这用于生成 HTML 和 LaTeX 以及其他输出格式的可选目录。在 HTML 中,它会自动生成可链接的 id,特别是可用于内部交叉引用; Markdown 语法是:

 See the section on [header identifiers](#header-identifiers-in-html).

正如我们在 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:

 See the section on [header identifiers](#header-identifiers-in-html).

as we read in the user's guide at http://johnmacfarlane.net/pandoc/README.html#header-identifiers-in-html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文