From d1576fd2eead1293dc22692476215fd08cf55e4c Mon Sep 17 00:00:00 2001 From: Miha Zupan Date: Sat, 15 Sep 2018 15:25:16 +0200 Subject: [PATCH] Fix LinkReferenceDefinition source positions --- src/Markdig/MarkdownPipelineBuilder.cs | 15 ++++++++---- src/Markdig/Parsers/ParagraphBlockParser.cs | 23 +++++++++++-------- .../Syntax/LinkReferenceDefinitionGroup.cs | 15 +++++++----- src/Markdig/Syntax/SourceSpan.cs | 7 +++++- 4 files changed, 40 insertions(+), 20 deletions(-) diff --git a/src/Markdig/MarkdownPipelineBuilder.cs b/src/Markdig/MarkdownPipelineBuilder.cs index 78269564..8bfb596b 100644 --- a/src/Markdig/MarkdownPipelineBuilder.cs +++ b/src/Markdig/MarkdownPipelineBuilder.cs @@ -1,4 +1,4 @@ -// Copyright (c) Alexandre Mutel. All rights reserved. +// 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 System; @@ -116,9 +116,16 @@ namespace Markdig extension.Setup(this); } - pipeline = new MarkdownPipeline(new OrderedList(Extensions), - new BlockParserList(BlockParsers), new InlineParserList(InlineParsers), StringBuilderCache, DebugLog, - GetDocumentProcessed) {PreciseSourceLocation = PreciseSourceLocation}; + pipeline = new MarkdownPipeline( + new OrderedList(Extensions), + new BlockParserList(BlockParsers), + new InlineParserList(InlineParsers), + StringBuilderCache, + DebugLog, + GetDocumentProcessed) + { + PreciseSourceLocation = PreciseSourceLocation + }; return pipeline; } } diff --git a/src/Markdig/Parsers/ParagraphBlockParser.cs b/src/Markdig/Parsers/ParagraphBlockParser.cs index 2a69cbb9..afe79417 100644 --- a/src/Markdig/Parsers/ParagraphBlockParser.cs +++ b/src/Markdig/Parsers/ParagraphBlockParser.cs @@ -1,4 +1,4 @@ -// Copyright (c) Alexandre Mutel. All rights reserved. +// 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.Helpers; @@ -9,7 +9,7 @@ namespace Markdig.Parsers /// /// Block parser for a . /// - /// + /// public class ParagraphBlockParser : BlockParser { public override BlockState TryOpen(BlockProcessor processor) @@ -152,15 +152,20 @@ namespace Markdig.Parsers { // If we have found a LinkReferenceDefinition, we can discard the previous paragraph var iterator = lines.ToCharIterator(); - LinkReferenceDefinition linkReferenceDefinition; - if (LinkReferenceDefinition.TryParse(ref iterator, out linkReferenceDefinition)) - { - if (!state.Document.ContainsLinkReferenceDefinition(linkReferenceDefinition.Label)) - { - state.Document.SetLinkReferenceDefinition(linkReferenceDefinition.Label, linkReferenceDefinition); - } + if (LinkReferenceDefinition.TryParse(ref iterator, out LinkReferenceDefinition linkReferenceDefinition)) + { + state.Document.SetLinkReferenceDefinition(linkReferenceDefinition.Label, linkReferenceDefinition); atLeastOneFound = true; + // Correct the locations of each field + linkReferenceDefinition.Line = lines.Lines[0].Line; + int lineStartPosition = lines.Lines[0].Position + 1; + + linkReferenceDefinition.Span = linkReferenceDefinition.Span .MoveForward(lineStartPosition); + linkReferenceDefinition.LabelSpan = linkReferenceDefinition.LabelSpan .MoveForward(lineStartPosition); + linkReferenceDefinition.UrlSpan = linkReferenceDefinition.UrlSpan .MoveForward(lineStartPosition); + linkReferenceDefinition.TitleSpan = linkReferenceDefinition.TitleSpan .MoveForward(lineStartPosition); + // Remove lines that have been matched if (iterator.Start > iterator.End) { diff --git a/src/Markdig/Syntax/LinkReferenceDefinitionGroup.cs b/src/Markdig/Syntax/LinkReferenceDefinitionGroup.cs index 2d6969eb..e3e8e263 100644 --- a/src/Markdig/Syntax/LinkReferenceDefinitionGroup.cs +++ b/src/Markdig/Syntax/LinkReferenceDefinitionGroup.cs @@ -1,4 +1,4 @@ -// Copyright (c) Alexandre Mutel. All rights reserved. +// 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. @@ -28,11 +28,14 @@ namespace Markdig.Syntax public void Set(string label, LinkReferenceDefinition link) { - if (link == null) throw new ArgumentNullException(nameof(link)); - Links[label] = link; - if (!Contains(link)) - { - Add(link); + if (link == null) throw new ArgumentNullException(nameof(link)); + if (!Contains(link)) + { + Add(link); + if (!Links.ContainsKey(label)) + { + Links[label] = link; + } } } diff --git a/src/Markdig/Syntax/SourceSpan.cs b/src/Markdig/Syntax/SourceSpan.cs index 8b17d3c4..d08c5157 100644 --- a/src/Markdig/Syntax/SourceSpan.cs +++ b/src/Markdig/Syntax/SourceSpan.cs @@ -1,4 +1,4 @@ -// Copyright (c) Alexandre Mutel. All rights reserved. +// 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. @@ -43,6 +43,11 @@ namespace Markdig.Syntax public bool IsEmpty => Start > End; + public SourceSpan MoveForward(int count) + { + return new SourceSpan(Start + count, End + count); + } + public bool Equals(SourceSpan other) { return Start == other.Start && End == other.End;