-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelpers.cs
More file actions
50 lines (44 loc) · 1.57 KB
/
Helpers.cs
File metadata and controls
50 lines (44 loc) · 1.57 KB
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
namespace Markdig.Renderer.Docx;
internal static class Helpers
{
// Some of the Learn content has invalid characters such as '\v' in the pages. This works in the Markdown
// but fails the strict parsing required by XML and docx.
public static string CleanText(string rawText) => new(rawText.Where(XmlConvert.IsXmlChar).ToArray());
/// <summary>
/// Returns the text of all literals following the given inline element until a non-literal is found.
/// </summary>
/// <param name="renderer"></param>
/// <param name="item"></param>
/// <returns></returns>
public static string ReadLiteralTextAfterTag(IDocxRenderer renderer, Inline item)
{
StringBuilder sb = new();
if (!item.IsClosed)
{
while (item.NextSibling is LiteralInline li)
{
sb.Append(CleanText(li.Content.ToString()));
renderer.OutOfPlaceRendered.Add(li);
item = item.NextSibling;
}
}
return sb.ToString();
}
/// <summary>
/// Returns the HTML tag associated with a string of HTML.
/// </summary>
/// <param name="htmlTag"></param>
/// <returns></returns>
public static string GetTag(string htmlTag)
{
if (string.IsNullOrEmpty(htmlTag))
return null;
int startPos = 1;
if (htmlTag.StartsWith("</"))
startPos = 2;
int endPos = startPos;
while (char.IsLetter(htmlTag[endPos]))
endPos++;
return htmlTag.Substring(startPos, endPos - startPos).ToLower();
}
}