Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump Markdig from 0.22.1 to 0.27.0 #7424

Closed
wants to merge 14 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Markdig" Version="0.22.1" />
<PackageReference Include="Markdig" Version="0.27.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="System.Collections.Immutable" Version="6.0.0" />
<PackageReference Include="Nerdbank.GitVersioning" Version="3.4.255" PrivateAssets="all" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class XrefInlineExtension : IMarkdownExtension
{
public void Setup(MarkdownPipelineBuilder pipeline)
{
pipeline.InlineParsers.InsertBefore<AutolineInlineParser>(new XrefInlineParser());
pipeline.InlineParsers.InsertBefore<AutolinkInlineParser>(new XrefInlineParser());
pipeline.InlineParsers.AddIfNotAlready(new XrefInlineShortParser());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private static bool MatchXrefShortcutWithQuote(InlineProcessor processor, ref St

var c = slice.NextChar();

while (c != startChar && !c.IsNewLine() && !c.IsZero())
while (c != startChar && !c.IsNewLineOrLineFeed() && !c.IsZero())
{
href.Append(c);
c = slice.NextChar();
Expand Down
2 changes: 1 addition & 1 deletion src/docfx/lib/markdown/ExtractTitleExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static MarkdownPipelineBuilder UseExtractTitle(
switch (obj)
{
case HeadingBlock heading when heading.Level == 1 || heading.Level == 2 || heading.Level == 3:
if (conceptual.Title.Value is null && heading.Inline.Any())
if (conceptual.Title.Value is null && heading.Inline != null && heading.Inline.Any())
{
conceptual.Title = new SourceInfo<string?>(markdownEngine.ToPlainText(heading), heading.GetSourceInfo());
}
Expand Down
5 changes: 4 additions & 1 deletion src/docfx/lib/markdown/FencedCodeExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ public static MarkdownPipelineBuilder UseFencedCodeLangPrefix(this MarkdownPipel
{
return builder.Use(pipeline =>
{
pipeline.BlockParsers.FindExact<FencedCodeBlockParser>().InfoPrefix = "lang-";
if (pipeline.BlockParsers.FindExact<FencedCodeBlockParser>() is FencedCodeBlockParser parser)
{
parser.InfoPrefix = "lang-";
}
});
}
}
2 changes: 1 addition & 1 deletion src/docfx/lib/markdown/HtmlExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static MarkdownPipelineBuilder UseHtml(
case TabTitleBlock:
return true;
case HtmlBlock block:
block.Lines = new StringLineGroup(ProcessHtml(block.Lines.ToString(), block, errors, elementCount));
block.Lines = new StringLineGroup(ProcessHtml(block.Lines.ToString().Replace("\r", ""), block, errors, elementCount));
return false;
case HtmlInline inline:
inline.Tag = ProcessHtml(inline.Tag, inline, errors, elementCount);
Expand Down
9 changes: 4 additions & 5 deletions src/docfx/lib/markdown/MarkdigUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public static MarkdownObject Replace(this MarkdownObject obj, Func<MarkdownObjec
}
return inline;

case LeafBlock leaf:
case LeafBlock leaf when leaf.Inline != null:
var leafInline = ReplaceCore(leaf.Inline, action) as ContainerInline;
if (leafInline != leaf.Inline)
{
Expand Down Expand Up @@ -268,9 +268,9 @@ private static bool IsInlineImage(this Inline node)
case LinkInline linkInline when linkInline.IsImage:
case TripleColonInline tripleColonInline when tripleColonInline.Extension is ImageExtension:
case HtmlInline htmlInline when htmlInline.Tag.StartsWith("<img", StringComparison.InvariantCultureIgnoreCase):
for (MarkdownObject current = node, parent = node.Parent; current != null;)
for (var current = node; current != null;)
{
if (parent is ContainerInline containerInline)
if (current.Parent is ContainerInline containerInline)
{
foreach (var child in containerInline)
{
Expand All @@ -279,8 +279,7 @@ private static bool IsInlineImage(this Inline node)
return true;
}
}
current = parent;
parent = containerInline.Parent;
current = current.Parent;
}
else
{
Expand Down
5 changes: 4 additions & 1 deletion src/docfx/lib/markdown/MarkdownEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,10 @@ parser is HeadingBlockParser || parser is ParagraphBlockParser ||

builder.InlineParsers.RemoveAll(parser => !(parser is LinkInlineParser || parser is EscapeInlineParser));

builder.BlockParsers.Find<HeadingBlockParser>().MaxLeadingCount = int.MaxValue;
if (builder.BlockParsers.Find<HeadingBlockParser>() is HeadingBlockParser headingParser)
{
headingParser.MaxLeadingCount = int.MaxValue;
}

builder.UseFilePath()
.UseYamlFrontMatter()
Expand Down
27 changes: 12 additions & 15 deletions src/docfx/lib/markdown/MonikerZoneExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,21 @@ internal static class MonikerZoneExtension
{
public static MarkdownPipelineBuilder UseMonikerZone(this MarkdownPipelineBuilder builder, Func<SourceInfo<string?>, MonikerList> parseMonikerRange)
{
return builder.Use(document =>
return builder.Use(document => document.Replace(node =>
{
document.Replace(node =>
if (node is MonikerRangeBlock monikerRangeBlock)
{
if (node is MonikerRangeBlock monikerRangeBlock)
var monikers = parseMonikerRange(new(monikerRangeBlock.MonikerRange, monikerRangeBlock.GetSourceInfo()));
if (!monikers.HasMonikers)
{
var monikers = parseMonikerRange(new SourceInfo<string?>(monikerRangeBlock.MonikerRange, monikerRangeBlock.GetSourceInfo()));
if (!monikers.HasMonikers)
{
return null;
}

monikerRangeBlock.ParsedMonikers = monikers;
monikerRangeBlock.GetAttributes().Properties.Remove(new KeyValuePair<string, string>("range", monikerRangeBlock.MonikerRange));
monikerRangeBlock.GetAttributes().AddPropertyIfNotExist("data-moniker", string.Join(" ", monikers));
return null;
}
return node;
});
});

monikerRangeBlock.ParsedMonikers = monikers;
monikerRangeBlock.GetAttributes().Properties?.Remove(new("range", monikerRangeBlock.MonikerRange));
monikerRangeBlock.GetAttributes().AddPropertyIfNotExist("data-moniker", string.Join(" ", monikers));
}
return node;
}));
}
}
4 changes: 2 additions & 2 deletions src/docfx/lib/markdown/XrefExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static MarkdownPipelineBuilder UseXref(
// @uid
if (node is XrefInline xref)
{
var raw = xref.GetAttributes().Properties.First(p => p.Key == "data-raw-source").Value;
var raw = xref.GetAttributes().Properties?.First(p => p.Key == "data-raw-source").Value ?? "";
var suppressXrefNotFound = raw.StartsWith("@");
var source = new SourceInfo<string>(xref.Href, xref.GetSourceInfo());
var xrefLink = resolveXref(source, null, suppressXrefNotFound);
Expand All @@ -30,7 +30,7 @@ public static MarkdownPipelineBuilder UseXref(
return new NolocInline() { Text = raw };
}

var linkInline = new LinkInline(xrefLink.Href, null);
var linkInline = new LinkInline(xrefLink.Href, "");
if (!xrefLink.Localizable)
{
var attributes = linkInline.GetAttributes();
Expand Down
6 changes: 3 additions & 3 deletions src/docfx/lib/markdown/validation/DocsValidationExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -292,14 +292,14 @@ private static TableNode TryParseTable(Table table)
private static TableCellNode ParseCell(TableCell cell)
{
var block = cell.LastChild;
var isVisible = MarkdigUtility.IsVisible(block);
var isVisible = block != null && MarkdigUtility.IsVisible(block);
var isEmphasis = false;
MarkdigUtility.Visit(block, node =>
{
var innerEmphasis = false;
if (node is ParagraphBlock paragraphBlock)
{
innerEmphasis = paragraphBlock.Inline.FindDescendants<EmphasisInline>().Any(x => x.DelimiterCount >= 2);
innerEmphasis = paragraphBlock.Inline != null && paragraphBlock.Inline.FindDescendants<EmphasisInline>().Any(x => x.DelimiterCount >= 2);
}
return isEmphasis = isEmphasis || innerEmphasis;
});
Expand All @@ -313,7 +313,7 @@ private static TableCellNode ParseCell(TableCell cell)

private static bool TryDetectTable(ParagraphBlock paragraph)
{
if (!paragraph.Inline.FindDescendants<LineBreakInline>().Any())
if (paragraph.Inline is null || !paragraph.Inline.FindDescendants<LineBreakInline>().Any())
{
return false;
}
Expand Down
6 changes: 3 additions & 3 deletions test/docfx.Test/lib/MarkdownElementTelemetryExtensionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ public static void GetElementTypeTest(MarkdownObject node, string expectedElemen
new object[] { new LiteralInline() { IsFirstCharacterEscaped = true }, "BackslashEscape" },
new object[] { new LiteralInline() { IsFirstCharacterEscaped = false }, "TextualContent" },
new object[] { new HtmlEntityInline(), "HTMLEntity" },
new object[] { new CodeInline(), "CodeSpan" },
new object[] { new CodeInline(""), "CodeSpan" },
new object[] { new EmphasisInline() { DelimiterCount = 1 }, "Emphasis" },
new object[] { new EmphasisInline() { DelimiterCount = 2 }, "StrongEmphasis" },
new object[] { new LinkInline() { IsImage = false, IsAutoLink = false }, "Link" },
new object[] { new LinkInline() { IsImage = true, IsAutoLink = false }, "Image" },
new object[] { new LinkInline() { IsImage = false, IsAutoLink = true }, "Autolink" },
new object[] { new AutolinkInline(), "Autolink" },
new object[] { new HtmlInline(), "RawHTML" },
new object[] { new AutolinkInline(""), "Autolink" },
new object[] { new HtmlInline(""), "RawHTML" },
new object[] { new LineBreakInline() { IsHard = true }, "HardLineBreak" },
new object[] { new LineBreakInline() { IsHard = false }, "SoftLineBreak" },
new object[] { new XrefInline(), "Xref" },
Expand Down