Skip to content

Commit

Permalink
bug fixed
Browse files Browse the repository at this point in the history
Fix parse two markdown document use same MarkdownPipelineBuilder, if just one has [toc], it will has both headings from two document.
  • Loading branch information
leisn committed Sep 15, 2021
1 parent 6802507 commit f0c6642
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 19 deletions.
1 change: 1 addition & 0 deletions MarkdigToc.sln
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Build", "Build", "{25A1C149
.editorconfig = .editorconfig
.gitattributes = .gitattributes
.gitignore = .gitignore
changelog.md = changelog.md
global.json = global.json
License = License
Readme.md = Readme.md
Expand Down
8 changes: 4 additions & 4 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# MarkdigToc [![NuGet](https://img.shields.io/nuget/v/Leisn.MarkdigToc)](https://www.nuget.org/packages/Leisn.MarkdigToc/)

MarkdigToc is a extension for [Markdig](https://github.com/xoofx/markdig) to generate table of content by parse [toc] in markdown content.
MarkdigToc is a extension for [Markdig](https://github.com/xoofx/markdig) to generate table of content by parse [toc] in markdown document.

> Currently just for render to html.
Expand Down Expand Up @@ -123,11 +123,11 @@ Code copied from `AutoIdentifierExtension`, then added some code and options.

#### TOC Title

> NOTICE: I also parse toc title and use it's attributes from markdown content , but that is not a regular *syntax*, you should know that.
> NOTICE: I also parse toc title and use it's attributes from markdown document , but that is not a regular *syntax*, you should know that.

* `OverrideTitle`: `string? : default null`

Override toc title , ignore defined in markdown content.
Override toc title , ignore defined in markdown document.

* `TitleTag`: `string : default p`

Expand Down Expand Up @@ -159,7 +159,7 @@ Code copied from `AutoIdentifierExtension`, then added some code and options.

## Others

Markdown content:
Markdown document:

```markdown
[TOC]
Expand Down
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
### 0.1.3
------

* Fix parse two markdown document use same `MarkdownPipelineBuilder` , one document without `[toc]` and the other one has, the generated toc block will contains both. This bug is occur case I use the `TocOptions.Headings` to store the headings, and don't clear them when all renderer completed, not just clear after toc rendered.
* After fixed, markdown document can has more than one `[toc]` , and all of these can be rendered, though it's useless.

3 changes: 0 additions & 3 deletions src/HtmlTocRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ protected override void Write(HtmlRenderer renderer, TocBlock obj)
renderer.Write($"</{Options.ContainerTag}>");
}
renderer.EnsureLine();

//once randerered clear the tree
Options.Headings.Clear();
}

void WriteTitle(HtmlRenderer renderer, TocBlock obj)
Expand Down
12 changes: 8 additions & 4 deletions src/MarkdigToc.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
Expand All @@ -10,15 +10,19 @@

<PropertyGroup>
<PackageId>Leisn.MarkdigToc</PackageId>
<VersionPrefix>0.1.2</VersionPrefix>
<Version>0.1.3</Version>
<Authors>leisn</Authors>
<PackageLicenseExpression>BSD-2-Clause</PackageLicenseExpression>
<PackageTags>Markdown toc Markdig md md2html</PackageTags>
<Description>A extension for Markdig to generate table of content by parse [toc] in markdown content.</Description>
<Description>A extension for Markdig to generate table of content by parse [toc] in markdown document.</Description>
<PackageReadmeFile>Readme.md</PackageReadmeFile>
<PackageIcon>icon.png</PackageIcon>
<Copyright>Leisn</Copyright>
<Copyright>Copyright (c) Leisn 2021</Copyright>
<PackageProjectUrl>https://github.com/leisn/MarkdigToc</PackageProjectUrl>
<PackageReleaseNotes> v0.1.3:
1. Fix parse two markdown document use same `MarkdownPipelineBuilder` , one document without `[toc]` and the other one has, the generated toc block will contains both.
2. After fixed, markdown document can has more than one `[toc]` , and all of these can be rendered, though it's useless.
</PackageReleaseNotes>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
Expand Down
39 changes: 34 additions & 5 deletions src/TocExtension.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Markdig;
using Markdig.Renderers;
using Markdig.Renderers.Html;
using Markdig.Syntax;

using System;
using System.Linq;

namespace Leisn.MarkdigToc
{
Expand All @@ -28,22 +30,49 @@ public TocExtension(TocOptions options)
//register parsers
public void Setup(MarkdownPipelineBuilder pipeline)
{
var cidE = pipeline.Extensions.Find<CustomAutoIdExtension>();
if (cidE == null)
var autoIdExtension = pipeline.Extensions.Find<CustomAutoIdExtension>();
if (autoIdExtension == null)
throw new InvalidOperationException("CustomAutoIdExtension is null");
cidE.OnHeadingParsed += (heading) => Options.AddHeading(heading);
autoIdExtension.OnHeadingParsed -= AutoIdExtension_OnHeadingParsed;
autoIdExtension.OnHeadingParsed += AutoIdExtension_OnHeadingParsed;
pipeline.BlockParsers.AddIfNotAlready(new TocBlockParser(Options));

//clear headings after all rendered, not here
//pipeline.DocumentProcessed -= Pipeline_DocumentProcessed;
//pipeline.DocumentProcessed += Pipeline_DocumentProcessed;
}

//register randerers
//private void Pipeline_DocumentProcessed(MarkdownDocument document)
//{
// //if there is no [toc] in markdown document, clear the headings.
// var tocBlocks = document.Where(block => block is TocBlock).ToList();
// if (tocBlocks == null || tocBlocks.Count < 1)
// Options.Headings.Clear();
//}

private void AutoIdExtension_OnHeadingParsed(HeadingInfo heading)
=> Options.AddHeading(heading);

//register randerers, this method will execute after all parsers completed.
public void Setup(MarkdownPipeline pipeline, IMarkdownRenderer renderer)
{
if (renderer is HtmlRenderer htmlRenderer)
{
//because TocBlock:HeadingBlock ,so renderer must before HeadingRenderer
htmlRenderer.ObjectRenderers.InsertBefore<HeadingRenderer>(new HtmlTocRenderer(Options));
if (!htmlRenderer.ObjectRenderers.Contains<HtmlTocRenderer>())
htmlRenderer.ObjectRenderers.InsertBefore<HeadingRenderer>(new HtmlTocRenderer(Options));
}

renderer.ObjectWriteAfter += Renderer_ObjectWriteAfter;
}

private void Renderer_ObjectWriteAfter(IMarkdownRenderer arg1, MarkdownObject arg2)
{
//move HtmlTocRenderer -> Write -> 'Options.Headings.Clear()' to here
//now it can be more than one [toc] in markdown document , and all of these can be rendered
//though it's useless
if (arg2 is MarkdownDocument)// when the document all writed
Options.Headings.Clear();
}
}
}
7 changes: 4 additions & 3 deletions src/TocOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public TocOptions()
public string TocTag { get; set; } = "nav";
/// <summary>
/// Class names for toc element.<br/>
/// Notice: If also defined in markdown content , this will be auto add without repeating.<br/>
/// Notice: If also defined in markdown document , this will be auto add without repeating.<br/>
/// e.g.<br/>
/// <code>[toc] {.tocClass1.tocClass2}</code>
/// <code>TocClass="toc tocClass1";</code>
Expand All @@ -73,7 +73,7 @@ public TocOptions()
public string? TocClass { get; set; }
/// <summary>
/// Id for toc element.<br/>
/// Notice: If defined in markdown content , this will be <strong>ignored</strong>.<br/>
/// Notice: If defined in markdown document , this will be <strong>ignored</strong>.<br/>
/// e.g.<br/>
/// <code>[toc] {#tocId}</code>
/// <code>TocId="tocId";</code>
Expand Down Expand Up @@ -124,6 +124,7 @@ public TocOptions()
#endregion

internal void AddHeading(HeadingInfo info)
=> Headings.Append(HeadingInfos.FromHeading(info));
=> Headings.Append(HeadingInfos.FromHeading(info));

}
}

0 comments on commit f0c6642

Please sign in to comment.