/// <summary> /// Extracts id and summary string from the given HeaderBlock /// </summary> /// <param name="header">markdown header block</param> /// <returns>Tuple of id and summary string</returns> static public (string id, string summary) DecomposeHeading(HeaderBlock header) { var text = header.ToString().Trim(); var prefix = Regex.Match(text, "\\[[-A-Z0-9]{1,}\\]").Value; var id = prefix.Substring(1, prefix.Length - 2); var summay = text?.Replace(prefix, string.Empty); return(string.IsNullOrEmpty(summay) ? (id, string.Empty) : (id, summay)); }
/// <summary> /// Markdown blocks processing /// </summary> /// <param name="b"></param> /// <param name="recursionLevel"></param> protected List <string> BlockProcessing(MarkdownBlock b, int recursionLevel) { string forecolor = string.Empty; string backcolor = string.Empty; string underline = string.Empty; int maxWidth = client.screenWidth - recursionLevel * 2; List <string> Text = new List <string>(); switch (b.Type) { case MarkdownBlockType.Root: break; case MarkdownBlockType.Paragraph: string para = ""; foreach (var i in ((ParagraphBlock)b).Inlines) { para += ProcessInline(i); } Text.AddRange(TextHelper.WordWrap(para, maxWidth)); Text.Add(string.Empty); break; case MarkdownBlockType.Quote: break; case MarkdownBlockType.Code: forecolor = ANSI.GetStyleForeColor("Code_Color", mdStyles); backcolor = ANSI.GetStyleBackColor("Code_Back", mdStyles); List <string> rows = TextHelper.SplitString(((CodeBlock)b).Text); foreach (string s in rows) { Text.Add(backcolor + forecolor + ANSI.ClearCurrentLine + s + ANSI.WriteMode()); } Text.Add(ANSI.WriteMode()); break; case MarkdownBlockType.Header: HeaderBlock hb = (HeaderBlock)b; Text.AddRange(ANSI.Header(hb.ToString(), hb.HeaderLevel, maxWidth)); break; case MarkdownBlockType.List: ListBlock lb = (ListBlock)b; string indent = new string(' ', recursionLevel * 2); foreach (var li in lb.Items) { List <string> lir = new List <string>(); bool isFirst = true; foreach (var lib in li.Blocks) { lir.AddRange(BlockProcessing(lib, recursionLevel + 1)); } foreach (string s in lir) { Text.Add((isFirst ? "- " : " ") + s); isFirst = false; } } break; case MarkdownBlockType.ListItemBuilder: break; case MarkdownBlockType.HorizontalRule: Text.Add(TextHelper.HR('-', maxWidth)); break; case MarkdownBlockType.Table: TableBlock tb = (TableBlock)b; int colWidth = maxWidth / tb.ColumnDefinitions.Count; foreach (var tr in tb.Rows) { string trs = string.Empty; foreach (var tc in tr.Cells) { string tcs = string.Empty; foreach (var i in tc.Inlines) { tcs += ProcessInline(i); } trs += TextHelper.Truncate(tcs, colWidth - 1).PadRight(colWidth); } Text.Add(trs); } break; case MarkdownBlockType.LinkReference: break; case MarkdownBlockType.YamlHeader: break; default: break; } return(Text); }