blob: 041d7156caa2a7528dfac0b91fa71d725db44365 (
plain) (
blame)
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
|
namespace Spacebar.Sdk.Core;
public class MarkdownEnumerator {
public IEnumerable<BaseMarkdownNode> EnumerateMarkdownComponents(string text) {
if (text.StartsWith("-#")) {
var line = text.Split('\n')[0];
text = text.Replace(line + "\n", "");
yield return new ContainerMarkdownNode() {
ComponentType = "sub",
Contents = new MarkdownEnumerator().EnumerateMarkdownComponents(line[2..].TrimStart()).ToList()
};
}
else if (text.StartsWith("#")) {
var hdrLevel = text.TakeWhile(x => x == '#').Count();
var line = text.Split('\n')[0];
text = text.Replace(line + "\n", "");
yield return new ContainerMarkdownNode() {
ComponentType = "h" + hdrLevel,
Contents = new MarkdownEnumerator().EnumerateMarkdownComponents(line[hdrLevel..].TrimStart()).ToList()
};
}
yield return new InnerTextMarkdownNode(text);
}
}
public class BaseMarkdownNode {
}
public class ContainerMarkdownNode : BaseMarkdownNode {
public string ComponentType { get; set; }
public List<BaseMarkdownNode> Contents { get; set; }
}
public class InnerTextMarkdownNode(string Text) : BaseMarkdownNode{
public string Text { get; set; }
}
|