forked from CommunityToolkit/Labs-Windows
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtensions.cs
736 lines (692 loc) · 24.9 KB
/
Extensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//using ColorCode;
//using ColorCode.Common;
//using ColorCode.Styling;
using Markdig.Syntax;
using Markdig.Syntax.Inlines;
using CommunityToolkit.Labs.WinUI.MarkdownTextBlock.TextElements;
using System.Xml.Linq;
using System.Globalization;
using Windows.UI.ViewManagement;
using HtmlAgilityPack;
namespace CommunityToolkit.Labs.WinUI.MarkdownTextBlock;
public static class Extensions
{
private const string OneDarkBackground = "#282c34";
private const string OneDarkPlainText = "#abb2bf";
private const string OneDarkXMLDelimiter = "#e06c75";
private const string OneDarkXMLName = "#d19a66";
private const string OneDarkXMLAttribute = "#61afef";
private const string OneDarkXAMLCData = "#98c379";
private const string OneDarkXMLComment = "#5c6370";
private const string OneDarkComment = "#5c6370";
private const string OneDarkKeyword = "#c678dd";
private const string OneDarkGray = "#9b9b9b";
private const string OneDarkNumber = "#d19a66";
private const string OneDarkClass = "#e5c07b";
private const string OneDarkString = "#98c379";
public const string Blue = "#FF0000FF";
public const string White = "#FFFFFFFF";
public const string Black = "#FF000000";
public const string DullRed = "#FFA31515";
public const string Yellow = "#FFFFFF00";
public const string Green = "#FF008000";
public const string PowderBlue = "#FFB0E0E6";
public const string Teal = "#FF008080";
public const string Gray = "#FF808080";
public const string Navy = "#FF000080";
public const string OrangeRed = "#FFFF4500";
public const string Purple = "#FF800080";
public const string Red = "#FFFF0000";
public const string MediumTurqoise = "FF48D1CC";
public const string Magenta = "FFFF00FF";
public const string OliveDrab = "#FF6B8E23";
public const string DarkOliveGreen = "#FF556B2F";
public const string DarkCyan = "#FF008B8B";
//public static ILanguage ToLanguage(this FencedCodeBlock fencedCodeBlock)
//{
// switch (fencedCodeBlock.Info?.ToLower())
// {
// case "aspx":
// return Languages.Aspx;
// case "aspx - vb":
// return Languages.AspxVb;
// case "asax":
// return Languages.Asax;
// case "ascx":
// return Languages.AspxCs;
// case "ashx":
// case "asmx":
// case "axd":
// return Languages.Ashx;
// case "cs":
// case "csharp":
// case "c#":
// return Languages.CSharp;
// case "xhtml":
// case "html":
// case "hta":
// case "htm":
// case "html.hl":
// case "inc":
// case "xht":
// return Languages.Html;
// case "java":
// case "jav":
// case "jsh":
// return Languages.Java;
// case "js":
// case "node":
// case "_js":
// case "bones":
// case "cjs":
// case "es":
// case "es6":
// case "frag":
// case "gs":
// case "jake":
// case "javascript":
// case "jsb":
// case "jscad":
// case "jsfl":
// case "jslib":
// case "jsm":
// case "jspre":
// case "jss":
// case "jsx":
// case "mjs":
// case "njs":
// case "pac":
// case "sjs":
// case "ssjs":
// case "xsjs":
// case "xsjslib":
// return Languages.JavaScript;
// case "posh":
// case "pwsh":
// case "ps1":
// case "psd1":
// case "psm1":
// return Languages.PowerShell;
// case "sql":
// case "cql":
// case "ddl":
// case "mysql":
// case "prc":
// case "tab":
// case "udf":
// case "viw":
// return Languages.Sql;
// case "vb":
// case "vbhtml":
// case "visual basic":
// case "vbnet":
// case "vb .net":
// case "vb.net":
// return Languages.VbDotNet;
// case "rss":
// case "xsd":
// case "wsdl":
// case "xml":
// case "adml":
// case "admx":
// case "ant":
// case "axaml":
// case "axml":
// case "builds":
// case "ccproj":
// case "ccxml":
// case "clixml":
// case "cproject":
// case "cscfg":
// case "csdef":
// case "csl":
// case "csproj":
// case "ct":
// case "depproj":
// case "dita":
// case "ditamap":
// case "ditaval":
// case "dll.config":
// case "dotsettings":
// case "filters":
// case "fsproj":
// case "fxml":
// case "glade":
// case "gml":
// case "gmx":
// case "grxml":
// case "gst":
// case "hzp":
// case "iml":
// case "ivy":
// case "jelly":
// case "jsproj":
// case "kml":
// case "launch":
// case "mdpolicy":
// case "mjml":
// case "mm":
// case "mod":
// case "mxml":
// case "natvis":
// case "ncl":
// case "ndproj":
// case "nproj":
// case "nuspec":
// case "odd":
// case "osm":
// case "pkgproj":
// case "pluginspec":
// case "proj":
// case "props":
// case "ps1xml":
// case "psc1":
// case "pt":
// case "qhelp":
// case "rdf":
// case "res":
// case "resx":
// case "rs":
// case "sch":
// case "scxml":
// case "sfproj":
// case "shproj":
// case "srdf":
// case "storyboard":
// case "sublime-snippet":
// case "sw":
// case "targets":
// case "tml":
// case "ui":
// case "urdf":
// case "ux":
// case "vbproj":
// case "vcxproj":
// case "vsixmanifest":
// case "vssettings":
// case "vstemplate":
// case "vxml":
// case "wixproj":
// case "workflow":
// case "wsf":
// case "wxi":
// case "wxl":
// case "wxs":
// case "x3d":
// case "xacro":
// case "xaml":
// case "xib":
// case "xlf":
// case "xliff":
// case "xmi":
// case "xml.dist":
// case "xmp":
// case "xproj":
// case "xspec":
// case "xul":
// case "zcml":
// return Languages.Xml;
// case "php":
// case "aw":
// case "ctp":
// case "fcgi":
// case "php3":
// case "php4":
// case "php5":
// case "phps":
// case "phpt":
// return Languages.Php;
// case "css":
// case "scss":
// case "less":
// return Languages.Css;
// case "cpp":
// case "c++":
// case "cc":
// case "cp":
// case "cxx":
// case "h":
// case "h++":
// case "hh":
// case "hpp":
// case "hxx":
// case "inl":
// case "ino":
// case "ipp":
// case "ixx":
// case "re":
// case "tcc":
// case "tpp":
// return Languages.Cpp;
// case "ts":
// case "tsx":
// case "cts":
// case "mts":
// return Languages.Typescript;
// case "fsharp":
// case "fs":
// case "fsi":
// case "fsx":
// return Languages.FSharp;
// case "koka":
// return Languages.Koka;
// case "hs":
// case "hs-boot":
// case "hsc":
// return Languages.Haskell;
// case "pandoc":
// case "md":
// case "livemd":
// case "markdown":
// case "mdown":
// case "mdwn":
// case "mdx":
// case "mkd":
// case "mkdn":
// case "mkdown":
// case "ronn":
// case "scd":
// case "workbook":
// return Languages.Markdown;
// case "fortran":
// case "f":
// case "f77":
// case "for":
// case "fpp":
// return Languages.Fortran;
// case "python":
// case "py":
// case "cgi":
// case "gyp":
// case "gypi":
// case "lmi":
// case "py3":
// case "pyde":
// case "pyi":
// case "pyp":
// case "pyt":
// case "pyw":
// case "rpy":
// case "smk":
// case "spec":
// case "tac":
// case "wsgi":
// case "xpy":
// return Languages.Python;
// case "matlab":
// case "m":
// return Languages.MATLAB;
// default:
// return Languages.JavaScript;
// }
//}
public static string ToAlphabetical(this int index)
{
var alphabetical = "abcdefghijklmnopqrstuvwxyz";
var remainder = index;
var stringBuilder = new StringBuilder();
while (remainder != 0)
{
if (remainder > 26)
{
var newRemainder = remainder % 26;
var i = (remainder - newRemainder) / 26;
stringBuilder.Append(alphabetical[i - 1]);
remainder = newRemainder;
}
else
{
stringBuilder.Append(alphabetical[remainder - 1]);
remainder = 0;
}
}
return stringBuilder.ToString();
}
public static TextPointer? GetNextInsertionPosition(this TextPointer position, LogicalDirection logicalDirection)
{
// Check if the current position is already an insertion position
if (position.IsAtInsertionPosition(logicalDirection))
{
// Return the same position
return position;
}
else
{
// Try to find the next insertion position by moving one symbol forward
TextPointer next = position.GetPositionAtOffset(1, logicalDirection);
// If there is no next position, return null
if (next == null)
{
return null;
}
else
{
// Recursively call this method until an insertion position is found or null is returned
return GetNextInsertionPosition(next, logicalDirection);
}
}
}
public static bool IsAtInsertionPosition(this TextPointer position, LogicalDirection logicalDirection)
{
// Get the character rect of the current position
Rect currentRect = position.GetCharacterRect(logicalDirection);
// Try to get the next position by moving one symbol forward
TextPointer next = position.GetPositionAtOffset(1, logicalDirection);
// If there is no next position, return false
if (next == null)
{
return false;
}
else
{
// Get the character rect of the next position
Rect nextRect = next.GetCharacterRect(logicalDirection);
// Compare the two rects and return true if they are different
return !currentRect.Equals(nextRect);
}
}
public static string RemoveImageSize(string? url)
{
if (string.IsNullOrEmpty(url))
{
throw new ArgumentException("URL must not be null or empty", nameof(url));
}
// Create a regex pattern to match the URL with width and height
var pattern = @"([^)\s]+)\s*=\s*\d+x\d+\s*";
// Replace the matched URL with the URL only
var result = Regex.Replace(url, pattern, "$1");
return result;
}
public static Uri GetUri(string? url, string? @base)
{
var validUrl = RemoveImageSize(url);
Uri result;
#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
if (Uri.TryCreate(validUrl, UriKind.Absolute, out result))
{
//the url is already absolute
return result;
}
else if (!string.IsNullOrWhiteSpace(@base))
{
//the url is relative, so append the base
//trim any trailing "/" from the base and any leading "/" from the url
@base = @base?.TrimEnd('/');
validUrl = validUrl.TrimStart('/');
//return the base and the url separated by a single "/"
return new Uri(@base + "/" + validUrl);
}
else
{
//the url is relative to the file system
//add ms-appx
validUrl = validUrl.TrimStart('/');
return new Uri("ms-appx:///" + validUrl);
}
#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type.
}
//public static StyleDictionary GetOneDarkProStyle()
//{
// return new StyleDictionary
// {
// new ColorCode.Styling.Style(ScopeName.PlainText)
// {
// Foreground = OneDarkPlainText,
// Background = OneDarkBackground,
// ReferenceName = "plainText"
// },
// new ColorCode.Styling.Style(ScopeName.HtmlServerSideScript)
// {
// Background = Yellow,
// ReferenceName = "htmlServerSideScript"
// },
// new ColorCode.Styling.Style(ScopeName.HtmlComment)
// {
// Foreground = OneDarkComment,
// ReferenceName = "htmlComment"
// },
// new ColorCode.Styling.Style(ScopeName.HtmlTagDelimiter)
// {
// Foreground = OneDarkKeyword,
// ReferenceName = "htmlTagDelimiter"
// },
// new ColorCode.Styling.Style(ScopeName.HtmlElementName)
// {
// Foreground = DullRed,
// ReferenceName = "htmlElementName"
// },
// new ColorCode.Styling.Style(ScopeName.HtmlAttributeName)
// {
// Foreground = Red,
// ReferenceName = "htmlAttributeName"
// },
// new ColorCode.Styling.Style(ScopeName.HtmlAttributeValue)
// {
// Foreground = OneDarkKeyword,
// ReferenceName = "htmlAttributeValue"
// },
// new ColorCode.Styling.Style(ScopeName.HtmlOperator)
// {
// Foreground = OneDarkKeyword,
// ReferenceName = "htmlOperator"
// },
// new ColorCode.Styling.Style(ScopeName.Comment)
// {
// Foreground = OneDarkComment,
// ReferenceName = "comment"
// },
// new ColorCode.Styling.Style(ScopeName.XmlDocTag)
// {
// Foreground = OneDarkXMLComment,
// ReferenceName = "xmlDocTag"
// },
// new ColorCode.Styling.Style(ScopeName.XmlDocComment)
// {
// Foreground = OneDarkXMLComment,
// ReferenceName = "xmlDocComment"
// },
// new ColorCode.Styling.Style(ScopeName.String)
// {
// Foreground = OneDarkString,
// ReferenceName = "string"
// },
// new ColorCode.Styling.Style(ScopeName.StringCSharpVerbatim)
// {
// Foreground = OneDarkString,
// ReferenceName = "stringCSharpVerbatim"
// },
// new ColorCode.Styling.Style(ScopeName.Keyword)
// {
// Foreground = OneDarkKeyword,
// ReferenceName = "keyword"
// },
// new ColorCode.Styling.Style(ScopeName.PreprocessorKeyword)
// {
// Foreground = OneDarkKeyword,
// ReferenceName = "preprocessorKeyword"
// },
// new ColorCode.Styling.Style(ScopeName.Number)
// {
// Foreground=OneDarkNumber,
// ReferenceName="number"
// },
// new ColorCode.Styling.Style(ScopeName.CssPropertyName)
// {
// Foreground=OneDarkClass,
// ReferenceName="cssPropertyName"
// },
// new ColorCode.Styling.Style(ScopeName.CssPropertyValue)
// {
// Foreground=OneDarkString,
// ReferenceName="cssPropertyValue"
// },
// new ColorCode.Styling.Style(ScopeName.CssSelector)
// {
// Foreground=OneDarkKeyword,
// ReferenceName="cssSelector"
// },
// new ColorCode.Styling.Style(ScopeName.SqlSystemFunction)
// {
// Foreground=OneDarkClass,
// ReferenceName="sqlSystemFunction"
// },
// new ColorCode.Styling.Style(ScopeName.XmlAttribute)
// {
// Foreground=OneDarkXMLAttribute,
// ReferenceName="xmlAttribute"
// },
// new ColorCode.Styling.Style(ScopeName.XmlAttributeQuotes)
// {
// Foreground=OneDarkXMLDelimiter,
// ReferenceName="xmlAttributeQuotes"
// },
// new ColorCode.Styling.Style(ScopeName.XmlAttributeValue)
// {
// Foreground=OneDarkString,
// ReferenceName="xmlAttributeValue"
// },
// new ColorCode.Styling.Style(ScopeName.XmlCDataSection)
// {
// Foreground=OneDarkXAMLCData,
// ReferenceName="xmlCDataSection"
// },
// new ColorCode.Styling.Style(ScopeName.XmlComment)
// {
// Foreground=OneDarkXMLComment,
// ReferenceName="xmlComment"
// },
// new ColorCode.Styling.Style(ScopeName.XmlDelimiter)
// {
// Foreground=OneDarkXMLDelimiter,
// ReferenceName="xmlDelimiter"
// },
// new ColorCode.Styling.Style(ScopeName.XmlName)
// {
// Foreground=OneDarkXMLName,
// ReferenceName="xmlName"
// }
// };
//}
public static HtmlElementType TagToType(this string tag)
{
switch (tag.ToLower())
{
case "address":
case "article":
case "aside":
case "details":
case "blockquote":
case "canvas":
case "dd":
case "div":
case "dl":
case "dt":
case "fieldset":
case "figcaption":
case "figure":
case "footer":
case "form":
case "h1":
case "h2":
case "h3":
case "h4":
case "h5":
case "h6":
case "header":
case "hr":
case "li":
case "main":
case "nav":
case "noscript":
case "ol":
case "p":
case "pre":
case "section":
case "table":
case "tfoot":
case "ul": return HtmlElementType.Block;
default: return HtmlElementType.Inline;
}
}
public static bool IsHeading(this string tag)
{
var headings = new List<string>() { "h1", "h2", "h3", "h4", "h5", "h6" };
return headings.Contains(tag.ToLower());
}
public static Size GetSvgSize(string svgString)
{
// Parse the SVG string as an XML document
XDocument svgDocument = XDocument.Parse(svgString);
// Get the root element of the document
#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
XElement svgElement = svgDocument.Root;
// Get the height and width attributes of the root element
#pragma warning disable CS8602 // Dereference of a possibly null reference.
XAttribute heightAttribute = svgElement.Attribute("height");
#pragma warning restore CS8602 // Dereference of a possibly null reference.
XAttribute widthAttribute = svgElement.Attribute("width");
#pragma warning restore CS8600 // Converting null literal or possible null value to non-nullable type.
// Convert the attribute values to double
double.TryParse(heightAttribute?.Value, NumberStyles.Number, CultureInfo.InvariantCulture, out double height);
double.TryParse(widthAttribute?.Value, NumberStyles.Number, CultureInfo.InvariantCulture, out double width);
// Return the height and width as a tuple
return new(width, height);
}
public static Size GetMarkdownImageSize(LinkInline link)
{
if (link == null || !link.IsImage)
{
throw new ArgumentException("Link must be an image", nameof(link));
}
var url = link.Url;
if (string.IsNullOrEmpty(url))
{
throw new ArgumentException("Link must have a valid URL", nameof(link));
}
// Try to parse the width and height from the URL
var parts = url?.Split('=');
if (parts?.Length == 2)
{
var dimensions = parts[1].Split('x');
if (dimensions.Length == 2 && int.TryParse(dimensions[0], out int width) && int.TryParse(dimensions[1], out int height))
{
return new(width, height);
}
}
// not using this one as it's seems to be from the HTML renderer
//// Try to parse the width and height from the special attributes
//var attributes = link.GetAttributes();
//if (attributes != null && attributes.Properties != null)
//{
// var width = attributes.Properties.FirstOrDefault(p => p.Key == "width")?.Value;
// var height = attributes.Properties.FirstOrDefault(p => p.Key == "height")?.Value;
// if (!string.IsNullOrEmpty(width) && !string.IsNullOrEmpty(height) && int.TryParse(width, out int w) && int.TryParse(height, out int h))
// {
// return new(w, h);
// }
//}
// Return default values if no width and height are found
return new(0, 0);
}
public static SolidColorBrush GetAccentColorBrush(UIColorType colorType = UIColorType.Accent)
{
// Create a UISettings object to get the accent color
var uiSettings = new UISettings();
// Get the accent color as a Color value
var accentColor = uiSettings.GetColorValue(colorType);
// Create a SolidColorBrush from the accent color
var accentBrush = new SolidColorBrush(accentColor);
return accentBrush;
}
public static string GetAttribute(this HtmlNode node, string attributeName, string defaultValue)
{
if (attributeName == null)
{
throw new ArgumentNullException(nameof(attributeName));
}
return node.Attributes?[attributeName]?.Value ?? defaultValue;
}
}