Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape heading titles #1752

Merged
merged 1 commit into from
Jan 3, 2025
Merged
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
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