Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ as a script:
>>> markdown2.markdown("*boo!*", extras=["footnotes"])
'<p><em>boo!</em></p>\n'
```
The `footnotes` extra keeps the same number when a footnote is referenced more
than once.

There are a number of currently implemented extras for tables, footnotes,
syntax coloring of `<pre>`-blocks, auto-linking patterns, table of contents,
Smarty Pants (for fancy quotes, dashes, etc.) and more. See the [Extras
Expand Down
8 changes: 6 additions & 2 deletions lib/markdown2.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ def _setup_extras(self):
# https://docs.python.org/3/whatsnew/3.7.html#summary-release-highlights
self.footnotes = OrderedDict()
self.footnote_ids = []
self._footnote_numbers = {}
self._footnote_marker = _hash_text('<<footnote>>')
if "header-ids" in self.extras:
if not hasattr(self, '_count_from_header_id') or self.extras['header-ids'].get('reset-count', False):
Expand Down Expand Up @@ -586,9 +587,12 @@ def toc_sort(entry):
def _do_footnote_marker(self, text):
def footnote_sub(match):
normed_id = match.group(1)
if normed_id not in self.footnote_ids:
if normed_id not in self._footnote_numbers:
self.footnote_ids.append(normed_id)
return str(len(self.footnote_ids))
self._footnote_numbers[normed_id] = (
len(self._footnote_numbers) + 1
)
return str(self._footnote_numbers[normed_id])

return re.sub(r'%s-(.*?)(?=</a></sup>)' % self._footnote_marker, footnote_sub, text)

Expand Down
8 changes: 8 additions & 0 deletions test/test_markdown2.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,14 @@ def test_header_ids_from_file_vars(self):
self.assertIn('<h1 id="{}">Heading</h1>'.format(heading_id), html)
self.assertEqual(md.convert("# Heading\n"), "<h1>Heading</h1>\n")

def test_footnote_numbers_reset_between_conversions(self):
md = markdown2.Markdown(extras=["footnotes"])
md.convert("First[^first], second[^second].\n\n"
"[^first]: First note.\n[^second]: Second note.")
html = md.convert("Second[^second].\n\n[^second]: Second note.")
self.assertIn('<a href="#fn-second">1</a>', html)
self.assertIn('<li id="fn-second">', html)

def test_many_distinct_code_spans(self):
source = '\n\n'.join('`value_%s`' % i for i in range(1000))
expected = '\n\n'.join('<p><code>value_%s</code></p>' % i for i in range(1000)) + '\n'
Expand Down
24 changes: 24 additions & 0 deletions test/tm-cases/repeated_footnote_labels.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<p>First<sup class="footnote-ref" id="fnref-first"><a href="#fn-first">1</a></sup>, second<sup class="footnote-ref" id="fnref-second"><a href="#fn-second">2</a></sup>, and first again<sup class="footnote-ref" id="fnref-first"><a href="#fn-first">1</a></sup>.</p>

<ul>
<li>Second again<sup class="footnote-ref" id="fnref-second"><a href="#fn-second">2</a></sup>.</li>
<li>Third<sup class="footnote-ref" id="fnref-third"><a href="#fn-third">3</a></sup>.</li>
<li>First again<sup class="footnote-ref" id="fnref-first"><a href="#fn-first">1</a></sup>.</li>
</ul>

<div class="footnotes">
<hr />
<ol>
<li id="fn-second">
<p>Second note referencing first<sup class="footnote-ref" id="fnref-first"><a href="#fn-first">1</a></sup>.&#160;<a href="#fnref-second" class="footnoteBackLink" title="Jump back to footnote 1 in the text.">&#8617;</a></p>
</li>

<li id="fn-first">
<p>First note.&#160;<a href="#fnref-first" class="footnoteBackLink" title="Jump back to footnote 2 in the text.">&#8617;</a></p>
</li>

<li id="fn-third">
<p>Third note referencing second<sup class="footnote-ref" id="fnref-second"><a href="#fn-second">2</a></sup>.&#160;<a href="#fnref-third" class="footnoteBackLink" title="Jump back to footnote 3 in the text.">&#8617;</a></p>
</li>
</ol>
</div>
1 change: 1 addition & 0 deletions test/tm-cases/repeated_footnote_labels.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"extras": ["footnotes"]}
9 changes: 9 additions & 0 deletions test/tm-cases/repeated_footnote_labels.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
First[^first], second[^second], and first again[^first].

- Second again[^second].
- Third[^third].
- First again[^first].

[^second]: Second note referencing first[^first].
[^first]: First note.
[^third]: Third note referencing second[^second].
Loading