Skip to content

Commit

Permalink
Escape heading titles (#1752)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevencohn authored Jan 3, 2025
1 parent f829e30 commit 79c71d6
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions OneMore/Commands/Snippets/TocGenerators/PageTocGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace River.OneMoreAddIn.Commands.Snippets.TocGenerators
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Security;
using System.Threading.Tasks;
using System.Xml.Linq;
using Resx = Properties.Resources;
Expand Down Expand Up @@ -324,11 +325,12 @@ private XElement CreateOverlayContainer()
private void BuildHeadings(
XElement container, List<Heading> headings, ref int index, int level, bool dark)
{
static string RemoveHyperlinks(string text)
static string CleanTitle(string text)
{
// Escape URI to handle special chars like '&'
// removes hyperlinks from the text of a heading so the TOC hyperlink can be applied
// clean up illegal directives; can be caused by using "Clip to OneNote" from Edge
var wrapper = new XCData(text).GetWrapper();
var wrapper = new XCData(SecurityElement.Escape(text)).GetWrapper();
var links = wrapper.Elements("a").ToList();
foreach (var link in links)
{
Expand Down Expand Up @@ -360,7 +362,7 @@ static string RemoveHyperlinks(string text)
if (!string.IsNullOrEmpty(heading.Link))
{
var linkColor = dark ? " style='color:#5B9BD5'" : string.Empty;
var clean = RemoveHyperlinks(heading.Text);
var clean = CleanTitle(heading.Text);
text = $"<a href=\"{heading.Link}\"{linkColor}>{clean}</a>";
}

Expand Down

0 comments on commit 79c71d6

Please sign in to comment.