Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/XmlDocMarkdown.Core/MarkdownGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2166,7 +2166,7 @@ private IEnumerable<string> ToMarkdown(IReadOnlyList<XmlDocBlock> 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 "```";
Expand Down
2 changes: 2 additions & 0 deletions src/XmlDocMarkdown.Core/XmlDocBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
15 changes: 13 additions & 2 deletions src/XmlDocMarkdown.Core/XmlDocMember.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -246,15 +252,20 @@ 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 <code>
if (lines.Count != 0)
lines.RemoveAt(lines.Count - 1); // remove </code>

if (lines.Count != 0 && lines[0].Trim().Length == 0)
lines.RemoveAt(0);
if (lines.Count != 0 && lines[lines.Count - 1].Trim().Length == 0)
lines.RemoveAt(lines.Count - 1);

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;

Expand Down