Skip to content

Commit c32ef78

Browse files
authored
Merge pull request #307 from github/footnote-backref-labels
fix: Links with unique targets should have unique labels
2 parents 7b4ed79 + 388ddec commit c32ef78

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

src/html.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,16 @@ static bool S_put_footnote_backref(cmark_html_renderer *renderer, cmark_strbuf *
6363
if (renderer->written_footnote_ix >= renderer->footnote_ix)
6464
return false;
6565
renderer->written_footnote_ix = renderer->footnote_ix;
66+
char m[32];
67+
snprintf(m, sizeof(m), "%d", renderer->written_footnote_ix);
6668

6769
cmark_strbuf_puts(html, "<a href=\"#fnref-");
6870
houdini_escape_href(html, node->as.literal.data, node->as.literal.len);
69-
cmark_strbuf_puts(html, "\" class=\"footnote-backref\" data-footnote-backref aria-label=\"Back to content\">↩</a>");
71+
cmark_strbuf_puts(html, "\" class=\"footnote-backref\" data-footnote-backref data-footnote-backref-idx=\"");
72+
cmark_strbuf_puts(html, m);
73+
cmark_strbuf_puts(html, "\" aria-label=\"Back to reference ");
74+
cmark_strbuf_puts(html, m);
75+
cmark_strbuf_puts(html, "\">↩</a>");
7076

7177
if (node->footnote.def_count > 1)
7278
{
@@ -78,7 +84,15 @@ static bool S_put_footnote_backref(cmark_html_renderer *renderer, cmark_strbuf *
7884
houdini_escape_href(html, node->as.literal.data, node->as.literal.len);
7985
cmark_strbuf_puts(html, "-");
8086
cmark_strbuf_puts(html, n);
81-
cmark_strbuf_puts(html, "\" class=\"footnote-backref\" data-footnote-backref aria-label=\"Back to content\">↩<sup class=\"footnote-ref\">");
87+
cmark_strbuf_puts(html, "\" class=\"footnote-backref\" data-footnote-backref data-footnote-backref-idx=\"");
88+
cmark_strbuf_puts(html, m);
89+
cmark_strbuf_puts(html, "-");
90+
cmark_strbuf_puts(html, n);
91+
cmark_strbuf_puts(html, "\" aria-label=\"Back to reference ");
92+
cmark_strbuf_puts(html, m);
93+
cmark_strbuf_puts(html, "-");
94+
cmark_strbuf_puts(html, n);
95+
cmark_strbuf_puts(html, "\">↩<sup class=\"footnote-ref\">");
8296
cmark_strbuf_puts(html, n);
8397
cmark_strbuf_puts(html, "</sup></a>");
8498
}

test/extensions.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -737,23 +737,23 @@ Hi!
737737
<section class="footnotes" data-footnotes>
738738
<ol>
739739
<li id="fn-1">
740-
<p>Some <em>bolded</em> footnote definition. <a href="#fnref-1" class="footnote-backref" data-footnote-backref aria-label="Back to content">↩</a></p>
740+
<p>Some <em>bolded</em> footnote definition. <a href="#fnref-1" class="footnote-backref" data-footnote-backref data-footnote-backref-idx="1" aria-label="Back to reference 1">↩</a></p>
741741
</li>
742742
<li id="fn-footnote">
743743
<blockquote>
744744
<p>Blockquotes can be in a footnote.</p>
745745
</blockquote>
746746
<pre><code>as well as code blocks
747747
</code></pre>
748-
<p>or, naturally, simple paragraphs. <a href="#fnref-footnote" class="footnote-backref" data-footnote-backref aria-label="Back to content">↩</a></p>
748+
<p>or, naturally, simple paragraphs. <a href="#fnref-footnote" class="footnote-backref" data-footnote-backref data-footnote-backref-idx="2" aria-label="Back to reference 2">↩</a></p>
749749
</li>
750750
<li id="fn-other-note">
751-
<p>no code block here (spaces are stripped away) <a href="#fnref-other-note" class="footnote-backref" data-footnote-backref aria-label="Back to content">↩</a></p>
751+
<p>no code block here (spaces are stripped away) <a href="#fnref-other-note" class="footnote-backref" data-footnote-backref data-footnote-backref-idx="3" aria-label="Back to reference 3">↩</a></p>
752752
</li>
753753
<li id="fn-codeblock-note">
754754
<pre><code>this is now a code block (8 spaces indentation)
755755
</code></pre>
756-
<a href="#fnref-codeblock-note" class="footnote-backref" data-footnote-backref aria-label="Back to content">↩</a>
756+
<a href="#fnref-codeblock-note" class="footnote-backref" data-footnote-backref data-footnote-backref-idx="4" aria-label="Back to reference 4">↩</a>
757757
</li>
758758
</ol>
759759
</section>
@@ -773,7 +773,7 @@ This footnote is referenced[^a-footnote] multiple times, in lots of different pl
773773
<section class="footnotes" data-footnotes>
774774
<ol>
775775
<li id="fn-a-footnote">
776-
<p>This footnote definition should have three backrefs. <a href="#fnref-a-footnote" class="footnote-backref" data-footnote-backref aria-label="Back to content">↩</a> <a href="#fnref-a-footnote-2" class="footnote-backref" data-footnote-backref aria-label="Back to content">↩<sup class="footnote-ref">2</sup></a> <a href="#fnref-a-footnote-3" class="footnote-backref" data-footnote-backref aria-label="Back to content">↩<sup class="footnote-ref">3</sup></a></p>
776+
<p>This footnote definition should have three backrefs. <a href="#fnref-a-footnote" class="footnote-backref" data-footnote-backref data-footnote-backref-idx="1" aria-label="Back to reference 1">↩</a> <a href="#fnref-a-footnote-2" class="footnote-backref" data-footnote-backref data-footnote-backref-idx="1-2" aria-label="Back to reference 1-2">↩<sup class="footnote-ref">2</sup></a> <a href="#fnref-a-footnote-3" class="footnote-backref" data-footnote-backref data-footnote-backref-idx="1-3" aria-label="Back to reference 1-3">↩<sup class="footnote-ref">3</sup></a></p>
777777
</li>
778778
</ol>
779779
</section>
@@ -790,7 +790,7 @@ Hello[^"><script>alert(1)</script>]
790790
<section class="footnotes" data-footnotes>
791791
<ol>
792792
<li id="fn-%22%3E%3Cscript%3Ealert(1)%3C/script%3E">
793-
<p>pwned <a href="#fnref-%22%3E%3Cscript%3Ealert(1)%3C/script%3E" class="footnote-backref" data-footnote-backref aria-label="Back to content">↩</a></p>
793+
<p>pwned <a href="#fnref-%22%3E%3Cscript%3Ealert(1)%3C/script%3E" class="footnote-backref" data-footnote-backref data-footnote-backref-idx="1" aria-label="Back to reference 1">↩</a></p>
794794
</li>
795795
</ol>
796796
</section>

test/regression.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ A footnote in a paragraph[^1]
194194
<section class="footnotes" data-footnotes>
195195
<ol>
196196
<li id="fn-1">
197-
<p>a footnote <a href="#fnref-1" class="footnote-backref" data-footnote-backref aria-label="Back to content">↩</a> <a href="#fnref-1-2" class="footnote-backref" data-footnote-backref aria-label="Back to content">↩<sup class="footnote-ref">2</sup></a></p>
197+
<p>a footnote <a href="#fnref-1" class="footnote-backref" data-footnote-backref data-footnote-backref-idx="1" aria-label="Back to reference 1">↩</a> <a href="#fnref-1-2" class="footnote-backref" data-footnote-backref data-footnote-backref-idx="1-2" aria-label="Back to reference 1-2">↩<sup class="footnote-ref">2</sup></a></p>
198198
</li>
199199
</ol>
200200
</section>
@@ -284,10 +284,10 @@ This is some text. It has a citation.[^citation]
284284
<section class="footnotes" data-footnotes>
285285
<ol>
286286
<li id="fn-citation">
287-
<p>This is a long winded parapgraph that also has another citation.<sup class="footnote-ref"><a href="#fn-another-citation" id="fnref-another-citation" data-footnote-ref>2</a></sup> <a href="#fnref-citation" class="footnote-backref" data-footnote-backref aria-label="Back to content">↩</a></p>
287+
<p>This is a long winded parapgraph that also has another citation.<sup class="footnote-ref"><a href="#fn-another-citation" id="fnref-another-citation" data-footnote-ref>2</a></sup> <a href="#fnref-citation" class="footnote-backref" data-footnote-backref data-footnote-backref-idx="1" aria-label="Back to reference 1">↩</a></p>
288288
</li>
289289
<li id="fn-another-citation">
290-
<p>My second citation. <a href="#fnref-another-citation" class="footnote-backref" data-footnote-backref aria-label="Back to content">↩</a></p>
290+
<p>My second citation. <a href="#fnref-another-citation" class="footnote-backref" data-footnote-backref data-footnote-backref-idx="2" aria-label="Back to reference 2">↩</a></p>
291291
</li>
292292
</ol>
293293
</section>
@@ -306,10 +306,10 @@ This is some text. It has two footnotes references, side-by-side without any spa
306306
<section class="footnotes" data-footnotes>
307307
<ol>
308308
<li id="fn-footnote1">
309-
<p>Hello. <a href="#fnref-footnote1" class="footnote-backref" data-footnote-backref aria-label="Back to content">↩</a></p>
309+
<p>Hello. <a href="#fnref-footnote1" class="footnote-backref" data-footnote-backref data-footnote-backref-idx="1" aria-label="Back to reference 1">↩</a></p>
310310
</li>
311311
<li id="fn-footnote2">
312-
<p>Goodbye. <a href="#fnref-footnote2" class="footnote-backref" data-footnote-backref aria-label="Back to content">↩</a></p>
312+
<p>Goodbye. <a href="#fnref-footnote2" class="footnote-backref" data-footnote-backref data-footnote-backref-idx="2" aria-label="Back to reference 2">↩</a></p>
313313
</li>
314314
</ol>
315315
</section>
@@ -331,10 +331,10 @@ It has another footnote that contains many different characters (the autolinker
331331
<section class="footnotes" data-footnotes>
332332
<ol>
333333
<li id="fn-widely-cited">
334-
<p>this renders properly. <a href="#fnref-widely-cited" class="footnote-backref" data-footnote-backref aria-label="Back to content">↩</a></p>
334+
<p>this renders properly. <a href="#fnref-widely-cited" class="footnote-backref" data-footnote-backref data-footnote-backref-idx="1" aria-label="Back to reference 1">↩</a></p>
335335
</li>
336336
<li id="fn-sphinx-of-black-quartz_judge-my-vow-0123456789">
337-
<p>so does this. <a href="#fnref-sphinx-of-black-quartz_judge-my-vow-0123456789" class="footnote-backref" data-footnote-backref aria-label="Back to content">↩</a></p>
337+
<p>so does this. <a href="#fnref-sphinx-of-black-quartz_judge-my-vow-0123456789" class="footnote-backref" data-footnote-backref data-footnote-backref-idx="2" aria-label="Back to reference 2">↩</a></p>
338338
</li>
339339
</ol>
340340
</section>

0 commit comments

Comments
 (0)