DCsunset / pandoc-include

A pandoc filter to allow file and header inclusion
MIT License
63 stars 14 forks source link

Implicit header references do not work, when file with header is included after the reference #21

Open dmitryperets opened 3 years ago

dmitryperets commented 3 years ago

Pandoc supports implicit header references, as described here.

But when using pandoc-include AND when the file with the referenced header is included after the file with the implicit reference, the reference is not rendered properly. Note that explicit references work fine in the same situation.

For example, consider the following markdown source:

---
title: Testing Implicit References
---

!include 10-chapter1.md

!include 20-chapter2.md

Here is 10-chapter1.md:

[This is Header]

[Link][This is Header]

[Explicit Link](#this-is-header)

Here is 20-chapter2.md:

# This is Header

If I convert it to HTML, I get this:

<p>[This is Header]</p>
<p>[Link][This is Header]</p>
<p><a href="#this-is-header">Explicit Link</a></p>
<h1 data-number="1" id="this-is-header"><span class="header-section-number">1</span> This is Header</h1>

Note that only the explicit reference has been properly created. If I swap the MD files (so that 20-chapter2.md comes first), there will be no such issue.

DCsunset commented 3 years ago

Hello, can you show your command to compile this file? It could help me to reproduce this problem.

dmitryperets commented 3 years ago

Hi,

Suppose you have the three files that I've shown in the description (10-chapter1.md, 20-chapter2.md and the actual source markdown file called include-test.md), you can reproduce the problem as follows:

% cat include-test.md 
---
title: Testing Implicit References
---

!include 10-chapter1.md

!include 20-chapter2.md

% pandoc --filter pandoc-include -o include-test.html include-test.md

% cat include-test.html 
<p>[This is Header]</p>
<p>[Link][This is Header]</p>
<p><a href="#this-is-header">Explicit Link</a></p>
<h1 id="this-is-header">This is Header</h1>
DCsunset commented 3 years ago

Hi, I tried to swap the two files but the issue still exists. Is it the same on your PC?

dmitryperets commented 3 years ago

Hmm yes, you are right... something confused me before, I thought I saw it working when the referenced item was included before the reference. Maybe I tested on a different environment, not sure.

So now it looks like pandoc-include doesn't allow implicit references at all.

This is how it works without pandoc-include:

% cat include-test.md     
---
title: Testing Implicit References
---

# This is Header

[This is Header]

[Link][This is Header]

[Explicit Link](#this-is-header)

% pandoc --filter pandoc-include -o include-test.html include-test.md

% cat include-test.html                                              
<h1 id="this-is-header">This is Header</h1>
<p><a href="#this-is-header">This is Header</a></p>
<p><a href="#this-is-header">Link</a></p>
<p><a href="#this-is-header">Explicit Link</a></p>
DCsunset commented 3 years ago

This feature is not supported in pandoc-include because pandoc-include processes every file independently before merging them into one file.

But pandoc renders the implicit reference only when it sees any corresponding title in the same file. So the feature is difficult to implement in this filter.

If you have any ideas to solve this problem, feel free to comment here.