Skip to content

Commit

Permalink
Fix for links that include ampersands
Browse files Browse the repository at this point in the history
I think this is happening because of the markdown conversion, with
values in complex tables, because they are not getting converted in
the same way. Hopefully we don't see too many more of these.
  • Loading branch information
pcraig3 committed Sep 13, 2024
1 parent b1b82a5 commit 8bc2f59
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ The format is based on Keep a Changelog, and this project adheres to Semantic Ve
- Specify exact Python version in Dockerfile
- Fix 2 broken cover images
- preserve_heading_links function now accounts for multiple links preceding headings
- Fix for heading links where the HTML id includes an ampersand

## [1.25.0] - 2023-08-27

Expand Down
7 changes: 7 additions & 0 deletions bloom_nofos/nofos/nofo.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ def add_headings_to_nofo(nofo):

if subsection.html_id:
new_ids.append({"old_id": subsection.html_id, "new_id": subsection_id})
if "&" in subsection.html_id:
new_ids.append(
{
"old_id": subsection.html_id.replace("&", "&"),
"new_id": section_id,
}
)

subsection.html_id = subsection_id
subsection.save()
Expand Down
53 changes: 53 additions & 0 deletions bloom_nofos/nofos/test_nofo.py
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,33 @@ def setUp(self):
}
]

self.sections_with_ampersand_links = [
{
"name": "Program description",
"order": 1,
"html_id": "",
"has_section_page": True,
"subsections": [
{
"name": "Budget & Budget",
"order": 1,
"tag": "h3",
"html_id": "_budget_&_budget",
"body": [
'<p>Subsection 1 body to <a href="#_budget_&_budget">Budget & Budget</a>.</p>'
],
},
{
"order": 2,
"html_id": "",
"body": [
'<p>Subsection 2 body to <a href="#_budget_&amp;_budget">Budget & Budget</a>.</p>'
],
},
],
}
]

def test_add_headings_success(self):
nofo = create_nofo("Test Nofo", self.sections)
self.assertEqual(nofo.title, "Test Nofo")
Expand Down Expand Up @@ -1461,6 +1488,32 @@ def test_add_headings_with_really_long_title_replace_link(self):
subsection_3.body,
)

def test_add_headings_with_ampersand_links(self):
nofo = create_nofo("Test Nofo 3", self.sections_with_ampersand_links)
self.assertEqual(nofo.title, "Test Nofo 3")

################
# ADD HEADINGS
################
add_headings_to_nofo(nofo)
section = nofo.sections.first()
subsection_1 = nofo.sections.first().subsections.all()[0]
subsection_2 = nofo.sections.first().subsections.all()[1]

# check section heading has new html_id
self.assertEqual(section.html_id, "program-description")

# check new html_ids
self.assertEqual(subsection_1.html_id, "1--program-description--budget-budget")
self.assertEqual(
subsection_1.body,
"Subsection 1 body to [Budget & Budget](#1--program-description--budget-budget).\n\n",
)
self.assertEqual(
subsection_2.body,
"Subsection 2 body to [Budget & Budget](#1--program-description--budget-budget).\n\n",
)


class AddPageBreaksToHeadingsTests(TestCase):
def setUp(self):
Expand Down

0 comments on commit 8bc2f59

Please sign in to comment.