diff --git a/MarkdigToc.sln b/MarkdigToc.sln index 52dccfb..baacc8a 100644 --- a/MarkdigToc.sln +++ b/MarkdigToc.sln @@ -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 diff --git a/Readme.md b/Readme.md index 1371830..3413edd 100644 --- a/Readme.md +++ b/Readme.md @@ -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. @@ -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` @@ -159,7 +159,7 @@ Code copied from `AutoIdentifierExtension`, then added some code and options. ## Others -Markdown content: +Markdown document: ```markdown [TOC] diff --git a/changelog.md b/changelog.md new file mode 100644 index 0000000..6161bd3 --- /dev/null +++ b/changelog.md @@ -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. + diff --git a/src/HtmlTocRenderer.cs b/src/HtmlTocRenderer.cs index 8e4aff0..1345084 100644 --- a/src/HtmlTocRenderer.cs +++ b/src/HtmlTocRenderer.cs @@ -65,9 +65,6 @@ protected override void Write(HtmlRenderer renderer, TocBlock obj) renderer.Write($""); } renderer.EnsureLine(); - - //once randerered clear the tree - Options.Headings.Clear(); } void WriteTitle(HtmlRenderer renderer, TocBlock obj) diff --git a/src/MarkdigToc.csproj b/src/MarkdigToc.csproj index 70e7a51..1413a14 100644 --- a/src/MarkdigToc.csproj +++ b/src/MarkdigToc.csproj @@ -1,4 +1,4 @@ - + Library @@ -10,15 +10,19 @@ Leisn.MarkdigToc - 0.1.2 + 0.1.3 leisn BSD-2-Clause Markdown toc Markdig md md2html - A extension for Markdig to generate table of content by parse [toc] in markdown content. + A extension for Markdig to generate table of content by parse [toc] in markdown document. Readme.md icon.png - Leisn + Copyright (c) Leisn 2021 https://github.com/leisn/MarkdigToc + 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. + diff --git a/src/TocExtension.cs b/src/TocExtension.cs index e1b2fe4..7a938e0 100644 --- a/src/TocExtension.cs +++ b/src/TocExtension.cs @@ -1,8 +1,10 @@ using Markdig; using Markdig.Renderers; using Markdig.Renderers.Html; +using Markdig.Syntax; using System; +using System.Linq; namespace Leisn.MarkdigToc { @@ -28,22 +30,49 @@ public TocExtension(TocOptions options) //register parsers public void Setup(MarkdownPipelineBuilder pipeline) { - var cidE = pipeline.Extensions.Find(); - if (cidE == null) + var autoIdExtension = pipeline.Extensions.Find(); + 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(new HtmlTocRenderer(Options)); + if (!htmlRenderer.ObjectRenderers.Contains()) + htmlRenderer.ObjectRenderers.InsertBefore(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(); + } } } diff --git a/src/TocOptions.cs b/src/TocOptions.cs index 08c36cd..f38f9c8 100644 --- a/src/TocOptions.cs +++ b/src/TocOptions.cs @@ -63,7 +63,7 @@ public TocOptions() public string TocTag { get; set; } = "nav"; /// /// Class names for toc element.
- /// Notice: If also defined in markdown content , this will be auto add without repeating.
+ /// Notice: If also defined in markdown document , this will be auto add without repeating.
/// e.g.
/// [toc] {.tocClass1.tocClass2} /// TocClass="toc tocClass1"; @@ -73,7 +73,7 @@ public TocOptions() public string? TocClass { get; set; } /// /// Id for toc element.
- /// Notice: If defined in markdown content , this will be ignored.
+ /// Notice: If defined in markdown document , this will be ignored.
/// e.g.
/// [toc] {#tocId} /// TocId="tocId"; @@ -124,6 +124,7 @@ public TocOptions() #endregion internal void AddHeading(HeadingInfo info) - => Headings.Append(HeadingInfos.FromHeading(info)); + => Headings.Append(HeadingInfos.FromHeading(info)); + } }