From 0ef08051ec3688990e9622537ef91900d4025254 Mon Sep 17 00:00:00 2001 From: TheBlob42 Date: Tue, 21 Nov 2023 07:05:42 +0100 Subject: [PATCH] fix: Don't parse tags inside words & URLs fixes #265 --- Marksman/Parser.fs | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/Marksman/Parser.fs b/Marksman/Parser.fs index 557e6c9..c1eef81 100644 --- a/Marksman/Parser.fs +++ b/Marksman/Parser.fs @@ -47,25 +47,28 @@ module Markdown = do this.OpeningCharacters <- [| '#' |] override this.Match(processor, slice) = - let start = slice.Start - let offsetStart = processor.GetSourcePosition(slice.Start) - - let shouldAccept (c: char) = c.IsAlphaNumeric() || c = '-' || c = '_' - - while (shouldAccept (slice.PeekChar())) do - slice.NextChar() |> ignore - - let end_ = slice.Start - let offsetEnd = offsetStart + (end_ - start) - - if end_ > start then - let text = slice.Text.Substring(start, end_ - start + 1) - let tag = TagInline(text) - tag.Span <- SourceSpan(offsetStart, offsetEnd) - processor.Inline <- tag - true - else + // tags should not be placed inside words, URLs etc. + if (slice.PeekCharExtra -1).IsAlphaNumeric() then false + else + let start = slice.Start + let offsetStart = processor.GetSourcePosition(slice.Start) + let shouldAccept (c: char) = c.IsAlphaNumeric() || c = '-' || c = '_' + + while (shouldAccept (slice.PeekChar())) do + slice.NextChar() |> ignore + + let end_ = slice.Start + let offsetEnd = offsetStart + (end_ - start) + + if end_ > start then + let text = slice.Text.Substring(start, end_ - start + 1) + let tag = TagInline(text) + tag.Span <- SourceSpan(offsetStart, offsetEnd) + processor.Inline <- tag + true + else + false /// Match links of the form `[[doc#heading|title]]`, where at least one of `doc` and `#heading` must be present (`|title` may be omitted). type WikiLinkParser() as this =