From d84b51dc70bbc269c9d62c4f98a20f120e7d22f3 Mon Sep 17 00:00:00 2001 From: Steven Date: Mon, 12 Aug 2024 21:03:24 -0400 Subject: [PATCH] dynamically populate and correct page hyperlinks before copying (#1531) #1513 --- OneMoreCalendar/CalendarPage.cs | 13 ++++++++++- OneMoreCalendar/MonthView.cs | 38 ++++++++++++++++++++++++--------- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/OneMoreCalendar/CalendarPage.cs b/OneMoreCalendar/CalendarPage.cs index ee00d42202..ab64a7603d 100644 --- a/OneMoreCalendar/CalendarPage.cs +++ b/OneMoreCalendar/CalendarPage.cs @@ -12,7 +12,18 @@ namespace OneMoreCalendar /// /// /// - internal class CalendarPages : List { } + internal class CalendarPages : List + { + public CalendarPages() + : base() + { + } + + public CalendarPages(IEnumerable pages) + : base(pages) + { + } + } /// diff --git a/OneMoreCalendar/MonthView.cs b/OneMoreCalendar/MonthView.cs index d94ef4df40..1d79e887ee 100644 --- a/OneMoreCalendar/MonthView.cs +++ b/OneMoreCalendar/MonthView.cs @@ -644,26 +644,44 @@ private async void ClickCopyPageButton(object sender, EventArgs e) { if (((MoreButton)sender).Tag is CalendarDay day) { - // if we have at least one Hyperlink then we've been here before! - if (!day.Pages.Exists(p => p.Hyperlink is not null)) + // fill and correct hyperlinks... + + var candidates = day.Pages + .Where(p => p.Hyperlink is null) + .Select(p => p); + + if (candidates.Any()) { + var empties = new CalendarPages(candidates); + var one = new OneNoteProvider(); - await one.GetPageLinks(day.Pages); + await one.GetPageLinks(empties); - // hyperlinks are returned from the OneNote API backwards from the expected - // format so this matches the two parts that needs to be swapped - var regex = new Regex(@"onenote:(#Boxing&.+?&end)&base-path=(https:.+)"); + // hyperlinks may be returned from the OneNote API backwards from the + // expected format so this matches the two parts that needs to be swapped + var regex = new Regex(@"onenote:(#.+?&end)&base-path=(https:.+)"); - foreach (var page in day.Pages) + foreach (var page in empties) { - var match = regex.Match(page.Hyperlink); - if (match.Success) + if (page.Hyperlink.StartsWith("onenote:https:")) { - page.Hyperlink = $"onenote:{match.Groups[2].Value}{match.Groups[1].Value}"; + // hyperlink is correct, just strip onenote: part + page.Hyperlink = page.Hyperlink.Substring(8); + } + else + { + var match = regex.Match(page.Hyperlink); + if (match.Success) + { + // hyperlink is reversed, so correct it + page.Hyperlink = $"onenote:{match.Groups[2].Value}{match.Groups[1].Value}"; + } } } } + // copy... + var pages = day.Pages.Where(p => p.Hyperlink is not null).ToList(); if (pages.Any()) {