Skip to content
Merged
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
14 changes: 14 additions & 0 deletions src/MarkdownParser/Models/BlockType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace MarkdownParser.Models
{
public enum BlockType
{
RootOrUnknown,
List,
FencedCode,
IndentedCode,
Html,
Quote,
Heading,
Paragraph
}
}
5 changes: 5 additions & 0 deletions src/MarkdownParser/Models/TextBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ public class TextBlock
{
public BaseSegment[] TextSegments { get; }

/// <summary>
/// Ancestors in order of (starting at 0) GrandParent > Parent > Sibling
/// </summary>
public BlockType[] AncestorsTree { get; internal set; }

public TextBlock(BaseSegment[] textSegments)
{
TextSegments = textSegments;
Expand Down
97 changes: 86 additions & 11 deletions src/MarkdownParser/Writer/ViewWriter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -72,7 +73,7 @@ public void StartBlock(BlockTag blockType)
{
Workbench.Push(new ViewWriterCache<T> { ComponentType = blockType });
}

public void FinalizeParagraphBlock()
{
var wbi = GetWorkbenchItem();
Expand All @@ -89,9 +90,16 @@ public void FinalizeParagraphBlock()

foreach (var itemsCacheTuple in itemsCache)
{
var view = itemsCacheTuple.TextBlock != null
? ViewSupplier.CreateTextView(itemsCacheTuple.TextBlock)
: itemsCacheTuple.Value;
T view;
if (itemsCacheTuple.TextBlock != null)
{
itemsCacheTuple.TextBlock.AncestorsTree = GetAncestorsTreeFromWorkbench(BlockType.Paragraph);
view = ViewSupplier.CreateTextView(itemsCacheTuple.TextBlock);
}
else
{
view = itemsCacheTuple.Value;
}

if (view != null)
{
Expand Down Expand Up @@ -139,9 +147,16 @@ public void FinalizeHeaderBlock(int headerLevel)

foreach (var itemsCacheTuple in itemsCache)
{
var view = itemsCacheTuple.TextBlock != null
? ViewSupplier.CreateHeaderView(itemsCacheTuple.TextBlock, headerLevel)
: itemsCacheTuple.Value;
T view;
if (itemsCacheTuple.TextBlock != null)
{
itemsCacheTuple.TextBlock.AncestorsTree = GetAncestorsTreeFromWorkbench(BlockType.Heading);
view = ViewSupplier.CreateHeaderView(itemsCacheTuple.TextBlock, headerLevel);
}
else
{
view = itemsCacheTuple.Value;
}

views.Add(view);
}
Expand Down Expand Up @@ -187,9 +202,16 @@ public void FinalizeListItemBlock(ListData listData)

foreach (var itemsCacheTuple in itemsCache)
{
var view = itemsCacheTuple.TextBlock != null
? ViewSupplier.CreateTextView(itemsCacheTuple.TextBlock)
: itemsCacheTuple.Value;
T view;
if (itemsCacheTuple.TextBlock != null)
{
itemsCacheTuple.TextBlock.AncestorsTree = GetAncestorsTreeFromWorkbench(BlockType.List);
view = ViewSupplier.CreateTextView(itemsCacheTuple.TextBlock);
}
else
{
view = itemsCacheTuple.Value;
}

if (view != null)
{
Expand Down Expand Up @@ -257,6 +279,7 @@ public void StartAndFinalizeImageBlock(string targetUrl, string subscription, st
public void StartAndFinalizeFencedCodeBlock(StringContent content, string blockInfo)
{
var blocks = StringContentToBlocks(content);
blocks.AncestorsTree = GetAncestorsTreeFromWorkbench(BlockType.FencedCode);

var blockView = ViewSupplier.CreateFencedCodeBlock(blocks, blockInfo);
StoreView(blockView);
Expand All @@ -265,6 +288,7 @@ public void StartAndFinalizeFencedCodeBlock(StringContent content, string blockI
public void StartAndFinalizeIndentedCodeBlock(StringContent content)
{
var blocks = StringContentToBlocks(content);
blocks.AncestorsTree = GetAncestorsTreeFromWorkbench(BlockType.IndentedCode);

var blockView = ViewSupplier.CreateIndentedCodeBlock(blocks);
StoreView(blockView);
Expand All @@ -273,6 +297,7 @@ public void StartAndFinalizeIndentedCodeBlock(StringContent content)
public void StartAndFinalizeHtmlBlock(StringContent content)
{
var blocks = StringContentToBlocks(content);
blocks.AncestorsTree = GetAncestorsTreeFromWorkbench(BlockType.Html);

var blockView = ViewSupplier.CreateHtmlBlock(blocks);
StoreView(blockView);
Expand All @@ -290,6 +315,56 @@ public void StartAndFinalizePlaceholderBlock(string placeholderName)
StoreView(placeholderView);
}

private BlockType[] GetAncestorsTreeFromWorkbench(BlockType currentBlockType)
{
var blockTypeTree = new List<BlockType>();
foreach (var workBenchItem in Workbench.Reverse())
{
switch (workBenchItem.ComponentType)
{
case BlockTag.BlockQuote:
blockTypeTree.Add(BlockType.Quote);
break;
case BlockTag.List:
blockTypeTree.Add(BlockType.List);
break;
case BlockTag.FencedCode:
blockTypeTree.Add(BlockType.FencedCode);
break;
case BlockTag.IndentedCode:
blockTypeTree.Add(BlockType.IndentedCode);
break;
case BlockTag.HtmlBlock:
blockTypeTree.Add(BlockType.Html);
break;
case BlockTag.Paragraph:
blockTypeTree.Add(BlockType.Paragraph);
break;
case BlockTag.AtxHeading:
case BlockTag.SetextHeading:
blockTypeTree.Add(BlockType.Heading);
break;
case BlockTag.ListItem: // this is already covered by BlockTag.List
case BlockTag.Document:
case BlockTag.ThematicBreak:
case BlockTag.ReferenceDefinition:
case null:
default:
break;
}
}

// Add current BlockType as most recent Ancestor
// but skip Lists as current to prevent double List entries
// because current List and ListItem levels are already picked up from Workbench
if (currentBlockType != BlockType.List)
{
blockTypeTree.Add(currentBlockType);
}

return blockTypeTree.ToArray();
}

private T StackViews(List<T> views)
{
if (views == null
Expand Down
4 changes: 4 additions & 0 deletions test/MarkdownParser.Test/MarkdownParser.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
<EmbeddedResource Include="Resources\Examples\Combined\basic-example.md" />
<EmbeddedResource Include="Resources\Examples\Combined\full-example.md" />
<EmbeddedResource Include="Resources\Examples\Combined\minimal-example.md" />
<EmbeddedResource Include="Resources\Examples\Sections\innerhtmlblocks.md" />
<EmbeddedResource Include="Resources\Examples\Sections\innercodeblocks.md" />
<EmbeddedResource Include="Resources\Examples\Sections\innernestedlist.md" />
<EmbeddedResource Include="Resources\Examples\Sections\innerblockquotes.md" />
<EmbeddedResource Include="Resources\Examples\Sections\codeblocks.md" />
<EmbeddedResource Include="Resources\Examples\Sections\headers.md" />
<EmbeddedResource Include="Resources\Examples\Sections\htmlblocks.md" />
Expand Down
Loading
Loading