【问题标题】:Unexpected link behavior after exporting Org-mode file to HTML将 Org 模式文件导出为 HTML 后出现意外的链接行为
【发布时间】:2023-12-27 19:19:01
【问题描述】:

我在 Windows 7 Ultimate 上的 Emacs 24.3.1 中运行 Org-mode 8.2.6,并且遇到了从 Org-mode 导出的 HTML 中链接如何工作的特殊性。我已经广泛使用org-id 为组织模式文件中的标题分配唯一ID(存储在标题的:PROPERTIES: 抽屉中)。

在 Org-mode 8.0 中引入新的导出器框架之前,所有这些链接都可以正常工作。无论标题层次结构的级别如何,导出的基于 HTML ID 的链接都可以正常工作。但是,使用新的导出器框架会产生不同的结果。现在,当目标标题位于导出设置中定义的标题级别以下时,基于 ID 的链接总是会失败,默认为级别 3 (H:3)。注意:这仅适用于导出的 HTML;基于 ID 的链接在 Emacs 中完美运行。

这是一个最小的示例,演示了我将其导出为 HTML 时的这种行为(有关详细信息,请参阅注释):

* Headline Level 1
** Headline Level 2
*** Headline Level 3
:PROPERTIES:
:ID:       307db49e-e001-4a7b-9541-96eee2ae6f06
:END:
**** <<heading-level-4>>Non-headline level
:PROPERTIES:
:ID:       3be9179d-f838-4052-93ca-6c76c9aff12d
:END:
** Headline Level 2
*** Headline Level 3
Now I want to link to information that appears elsewhere in the file. Links work as 
expected within Emacs. When exported to HTML, however, links do not work as they 
did before the new exporter framework was introduced in Org-mode 8.0.
**** ID-based link: [[id:307db49e-e001-4a7b-9541-96eee2ae6f06][Headline Level 3]]
This link /does/ work. Using IDs always works for links to any headline level. By 
"headline level" I mean any Org-mode heading that is defined as a headline 
(default H:3).
**** ID-based link: [[id:3be9179d-f838-4052-93ca-6c76c9aff12d][Non-headline level]]
This link using the ID /doesn't/ work when exported to HTML using the new exporter 
framework. Now, using IDs as the target for links /always/ fails for links to any 
headline lower than the headline level defined in the export settings.
**** Non-ID-based link: [[heading-level-4][Non-headline level]]
Using an internal link works, but I have /many/ existing files that depend on IDs 
for links at heading levels lower than the levels I want treated as (numbered) 
headlines, and I also sometimes link to targets in other files, in which case, 
using ID's creates a much simpler workflow.

如果上面的文件名为demo-links.org,则默认输出文件为demo-links.html。第一个工作链接的目标 HTML 如下所示:

&lt;h4 id="sec-1-1-1"&gt;&lt;a id="ID-307db49e-e001-4a7b-9541-96eee2ae6f06" name="ID-307db49e-e001-4a7b-9541-96eee2ae6f06"&gt;&lt;/a&gt;&lt;span class="section-number-4"&gt;1.1.1&lt;/span&gt; Headline Level 3&lt;/h4&gt;

链接的 HTML 如下所示:

&lt;a href="#sec-1-1-1"&gt;Headline Level 3&lt;/a&gt;

链接 ID 是目标代码的一部分,但不用于链接代码。

无效链接目标的 HTML 如下所示:

&lt;ul class="org-ul"&gt;&lt;li&gt;&lt;a id="heading-level-4" name="heading-level-4"&gt;&lt;/a&gt;Non-headline level&lt;br /&gt;&lt;div class="outline-text-5" id="text-1-1-1-1"&gt;&lt;/div&gt;&lt;/li&gt;&lt;/ul&gt;

请注意,生成的代码不包含 ID (3be9179d-f838-4052-93ca-6c76c9aff12d)。它也不像上一个链接那样包含部分 ID。

指向它的链接的 HTML 如下所示:

&lt;a href="#sec-1-1-1-1"&gt;Non-headline level&lt;/a&gt;

我相信ox-html.el 中的相关代码出现在评论“指向标题的链接”之后,但我是 elisp 的新手(充其量)。

我的问题是:这种行为是设计使然,还是我可以更改某些设置以使导出工作的方式与引入新导出框架之前的方式一样?

【问题讨论】:

  • 我向 Org-mode 邮件列表报告了这个问题,问题已得到解决。

标签: html emacs org-mode


【解决方案1】:

我也遇到了这个问题。更重要的是,“>”之间的文本可以在org-7.9中显示在导出的HTML中,而在org-8.2中不能显示

由于声望有限,我无法投票支持该项目,所以我只是在此处没有正确答案的情况下回答。 (=_=)

【讨论】:

  • 很抱歉您遇到了同样的问题,但我也很高兴知道这不仅仅是我!感谢您的意见。
【解决方案2】:

查看org-html-headline 函数的代码,似乎“标准标题”案例(导出到 hN 的任何内容)正在专门处理自定义 ID:

(let* (...
       (ids (remove nil
                    (list (org-element-property :CUSTOM_ID headline)
                          (concat "sec-" section-number)
                          (org-element-property :ID headline))))
       (preferred-id (car ids)) ;; <- here, id for the header is sec-XXX
       (extra-ids (cdr ids))
       ...)
  (format ...
          (format "outline-container-%s"
                  (or (org-element-property :CUSTOM_ID headline)
                      (concat "sec-" section-number)))
          ...
          (format "\n<h%d id=\"%s\">%s%s</h%d>\n"
                  level1
                  preferred-id
                  (mapconcat
                   (lambda (x)
                     (let ((id (org-export-solidify-link-text
                                (if (org-uuidgen-p x) (concat "ID-" x)
                                  x))))
                       (org-html--anchor id))) ;; <- generate a list of <a id=ID-XXX></a>
                   extra-ids "")
                  full-text
                  level1)
          ...))

这就是那些&lt;a id="ID-XXX" name="ID-XXX"&gt;&lt;/a&gt;sn-ps 的来源(占位符锚,只是为了暴露一个额外的 ID)

不幸的是,在标题转换为列表项的情况下,没有这样的 ID/CUSTOM_ID 处理,坦率地说,这在我看来像是一个错误。 因此,虽然可以重写 org-html-headline 以对列表项进行相同的处理,但不幸的是,它不像修改设置那么容易(而且代码修改会非常脆弱)

我建议打开一个错误报告,毕竟它似乎是一个回归。

【讨论】:

  • 我对代码的解释和你描述的差不多,但我想也许我错过了什么。因此,这对我来说似乎也是一个错误。我将报告它作为一个错误。感谢您的意见。
最近更新 更多