diff --git a/src/Markdig.Tests/Markdig.Tests.csproj b/src/Markdig.Tests/Markdig.Tests.csproj index 397f4c20..ad97c1fa 100644 --- a/src/Markdig.Tests/Markdig.Tests.csproj +++ b/src/Markdig.Tests/Markdig.Tests.csproj @@ -62,6 +62,7 @@ + diff --git a/src/Markdig.Tests/TestPragmaLines.cs b/src/Markdig.Tests/TestPragmaLines.cs new file mode 100644 index 00000000..f29fe57c --- /dev/null +++ b/src/Markdig.Tests/TestPragmaLines.cs @@ -0,0 +1,81 @@ +// Copyright (c) Alexandre Mutel. All rights reserved. +// This file is licensed under the BSD-Clause 2 license. +// See the license.txt file in the project root for more information. + +using Markdig.Renderers; +using Markdig.Syntax; +using NUnit.Framework; + +namespace Markdig.Tests +{ + [TestFixture] + public class TestPragmaLines + { + [Test] + public void TestFindClosest() + { + var doc = Markdown.Parse( +"test1\n" + // 0 +"\n" + // 1 +"test2\n" + // 2 +"\n" + // 3 +"test3\n" + // 4 +"\n" + // 5 +"test4\n" + // 6 +"\n" + // 7 +"# Heading\n" + // 8 +"\n" + // 9 +"Long para\n" + // 10 +"on multiple\n" + // 11 +"lines\n" + // 12 +"to check that\n" + // 13 +"lines are\n" + // 14 +"correctly \n" + // 15 +"found\n" + // 16 +"\n" + // 17 +"- item1\n" + // 18 +"- item2\n" + // 19 +"- item3\n" + // 20 +"\n" + // 21 +"This is a last paragraph\n" // 22 + , new MarkdownPipelineBuilder().UsePragmaLines().Build()); + + foreach (var exact in new int[] {0, 2, 4, 6, 8, 10, 18, 19, 20, 22}) + { + Assert.AreEqual(exact, doc.FindClosestLine(exact)); + } + + Assert.AreEqual(22, doc.FindClosestLine(23)); + + Assert.AreEqual(10, doc.FindClosestLine(11)); + Assert.AreEqual(10, doc.FindClosestLine(12)); + Assert.AreEqual(10, doc.FindClosestLine(13)); + Assert.AreEqual(18, doc.FindClosestLine(14)); // > 50% of the paragraph, we switch to next + Assert.AreEqual(18, doc.FindClosestLine(15)); + Assert.AreEqual(18, doc.FindClosestLine(16)); + } + + [Test] + public void TestFindClosest1() + { + var text = +"- item1\n" + // 0 +" - item11\n" + // 1 +" - item12\n" + // 2 +" - item121\n" + // 3 +" - item13\n" + // 4 +" - item131\n" + // 5 +" - item1311\n"; // 6 + + var pipeline = new MarkdownPipelineBuilder().UsePragmaLines().Build(); + var doc = Markdown.Parse(text, pipeline); + + for (int exact = 0; exact < 7; exact++) + { + Assert.AreEqual(exact, doc.FindClosestLine(exact)); + } + + Assert.AreEqual(6, doc.FindClosestLine(50)); + } + } +} \ No newline at end of file diff --git a/src/Markdig/Syntax/BlockExtensions.cs b/src/Markdig/Syntax/BlockExtensions.cs index 9ee1a7dc..0c800176 100644 --- a/src/Markdig/Syntax/BlockExtensions.cs +++ b/src/Markdig/Syntax/BlockExtensions.cs @@ -53,10 +53,6 @@ namespace Markdig.Syntax public static int FindClosestLine(this MarkdownDocument root, int line) { - // Forces the preview window to scroll to the top of the document - if (line <= 3) - return 0; - var closestBlock = root.FindClosestBlock(line); return closestBlock?.Line ?? 0; } @@ -89,7 +85,7 @@ namespace Markdig.Syntax } // If we are between two lines, try to find the best spot - if (lowerIndex >= 0 && lowerIndex < blocks.Count) + if (lowerIndex > 0 && lowerIndex < blocks.Count) { var prevBlock = blocks[lowerIndex - 1].FindClosestBlock(line) ?? blocks[lowerIndex - 1]; var nextBlock = blocks[lowerIndex].FindClosestBlock(line) ?? blocks[lowerIndex]; @@ -113,6 +109,18 @@ namespace Markdig.Syntax return middle < 0.5 ? prevBlock : nextBlock; } + if (lowerIndex == 0) + { + var prevBlock = blocks[lowerIndex].FindClosestBlock(line) ?? blocks[lowerIndex]; + return prevBlock; + } + + if (lowerIndex == blocks.Count) + { + var prevBlock = blocks[lowerIndex - 1].FindClosestBlock(line) ?? blocks[lowerIndex - 1]; + return prevBlock; + } + return null; }