public IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            if (MarkdownInlineContext.GetIsInLink(parser.Context))
            {
                return(null);
            }
            var match = XrefShortcutRegexWithQuote.Match(context.CurrentMarkdown);

            if (match.Length == 0)
            {
                match = XrefShortcutRegex.Match(context.CurrentMarkdown);
                if (match.Length == 0)
                {
                    return(null);
                }
            }

            var sourceInfo = context.Consume(match.Length);

            // @String=>cap[2]=String, @'string'=>cap[2]=string
            // For cross-reference, add ~/ prefix
            var content = match.Groups["uid"].Value;

            return(new DfmXrefInlineToken(this, parser.Context, content, ImmutableArray <IMarkdownToken> .Empty, null, false, sourceInfo));
        }
        public IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            if (MarkdownInlineContext.GetIsInLink(parser.Context))
            {
                return(null);
            }
            var match = XrefAutoLinkRegexWithQuote.Match(context.CurrentMarkdown);

            if (match.Length == 0)
            {
                match = XrefAutoLinkRegex.Match(context.CurrentMarkdown);
                if (match.Length == 0)
                {
                    return(null);
                }
            }
            var sourceInfo = context.Consume(match.Length);
            var content    = match.Groups[2].Value;

            return(new DfmXrefInlineToken(this, parser.Context, content, ImmutableArray <IMarkdownToken> .Empty, null, true, sourceInfo));
        }
Esempio n. 3
0
        public override IMarkdownToken TryMatch(IMarkdownParser parser, IMarkdownParsingContext context)
        {
            // NOTE: This copies the logic from MarkdownLinkInlineRule, but does not match if there are no links to replace.
            // This isn't perfect, but at least it ensures we only match the one [Changes]() link it is intended to match
            // and replaces multiple environment variables if they exist.

            // The when the Consume() method below is called, there doesn't appear to be any way to make the cursor backtrack,
            // and the scan is only performed once. This is why we have to resort to this. Not sure if there is a better way to make
            // nested tokens than this, but it doesn't seem like there is.

            var match = Link.Match(context.CurrentMarkdown);

            if (match.Length == 0)
            {
                return(null);
            }
            if (MarkdownInlineContext.GetIsInLink(parser.Context) && match.Value[0] != '!')
            {
                return(null);
            }
            if (IsEscape(match.Groups[1].Value) || IsEscape(match.Groups[2].Value))
            {
                return(null);
            }

            var text  = match.Groups[1].Value;
            var title = match.Groups[4].Value;
            var href  = match.Groups[2].Value;

            var textMatch  = EnvVarRegex.Match(text);
            var titleMatch = EnvVarRegex.Match(title);
            var hrefMatch  = EnvVarRegex.Match(href);

            // Don't match unless we have a match for our EnvVar pattern in any part of the link
            if (!textMatch.Success && !titleMatch.Success && !hrefMatch.Success)
            {
                return(null);
            }

            StringBuilder scratch = null;

            if (textMatch.Success || titleMatch.Success || hrefMatch.Success)
            {
                scratch = new StringBuilder();
            }

            if (textMatch.Success)
            {
                text = EnvironmentVariableUtil.ReplaceEnvironmentVariables(text, textMatch, scratch);
            }
            if (titleMatch.Success)
            {
                title = EnvironmentVariableUtil.ReplaceEnvironmentVariables(title, titleMatch, scratch);
            }
            if (hrefMatch.Success)
            {
                href = EnvironmentVariableUtil.ReplaceEnvironmentVariables(href, hrefMatch, scratch);
            }

            var sourceInfo = context.Consume(match.Length);

            return(GenerateToken(parser, href, title, text, match.Value[0] == '!', sourceInfo, MarkdownLinkType.NormalLink, null));
        }