diff --git a/src/XmlDocMarkdown.Core/MarkdownGenerator.cs b/src/XmlDocMarkdown.Core/MarkdownGenerator.cs index 65605166..5b30c33b 100644 --- a/src/XmlDocMarkdown.Core/MarkdownGenerator.cs +++ b/src/XmlDocMarkdown.Core/MarkdownGenerator.cs @@ -2166,7 +2166,7 @@ private IEnumerable ToMarkdown(IReadOnlyList blocks, Markdo { if (block.IsCode) { - yield return "```csharp"; + yield return "``` " + block.CodeLanguage; foreach (var inline in block.Inlines) yield return inline.Text!.Replace(Environment.NewLine, ActualNewLine); yield return "```"; diff --git a/src/XmlDocMarkdown.Core/XmlDocBlock.cs b/src/XmlDocMarkdown.Core/XmlDocBlock.cs index b7b91115..3a66018f 100644 --- a/src/XmlDocMarkdown.Core/XmlDocBlock.cs +++ b/src/XmlDocMarkdown.Core/XmlDocBlock.cs @@ -8,6 +8,8 @@ internal sealed class XmlDocBlock public bool IsCode { get; set; } + public string CodeLanguage { get; set; } = "csharp"; + public XmlDocListKind? ListKind { get; set; } public int ListDepth { get; set; } diff --git a/src/XmlDocMarkdown.Core/XmlDocMember.cs b/src/XmlDocMarkdown.Core/XmlDocMember.cs index 6bfbbe07..00b38030 100644 --- a/src/XmlDocMarkdown.Core/XmlDocMember.cs +++ b/src/XmlDocMarkdown.Core/XmlDocMember.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; +using System.Security.Cryptography; using System.Xml.Linq; namespace XmlDocMarkdown.Core @@ -151,7 +152,12 @@ private void AddElement(XElement xElement) case "code": NextBlock(); m_block!.IsCode = true; - m_block.Inlines.Add(new XmlDocInline { Text = TrimCode(xElement.Value) }); + XAttribute lang = xElement.Attribute("lang"); + if (lang == null) + lang = xElement.Attribute("language"); + if (lang != null) + m_block.CodeLanguage = lang.Value; + m_block.Inlines.Add(new XmlDocInline { Text = TrimCode(xElement.ToString()) }); NextBlock(); break; @@ -246,6 +252,11 @@ private static string TrimCode(string text) // trimming logic adapted from https://github.com/kzu/NuDoq var lines = text.Split(new[] { Environment.NewLine, "\n" }, StringSplitOptions.None).ToList(); + if (lines.Count != 0) + lines.RemoveAt(0); // remove + if (lines.Count != 0) + lines.RemoveAt(lines.Count - 1); // remove + if (lines.Count != 0 && lines[0].Trim().Length == 0) lines.RemoveAt(0); if (lines.Count != 0 && lines[lines.Count - 1].Trim().Length == 0) @@ -253,8 +264,8 @@ private static string TrimCode(string text) if (lines.Count == 0) return ""; + var indentLength = lines.Min( l => !string.IsNullOrWhiteSpace(l) ? l.Length - l.TrimStart().Length : int.MaxValue); - var indentLength = lines[0].Length - lines[0].TrimStart().Length; if (indentLength <= 4 && lines[0].Length != 0 && lines[0][0] != '\t') indentLength = 0;