Ejemplo n.º 1
0
        private static bool TryPrepareSharplabPreview(string url, int markdownLength, out string?preview)
        {
            if (!url.Contains("#v2:"))
            {
                preview = null;
                return(false);
            }

            try
            {
                // Decode the compressed code from the URL payload
                var base64Text = url.Substring(url.IndexOf("#v2:") + "#v2:".Length);
                var plainText  = LZString.DecompressFromBase64(base64Text);

                // Extract the option and get the target language
                var textParts      = Regex.Match(plainText, @"([^|]*)\|([\s\S]*)$");
                var languageOption = Regex.Match(textParts.Groups[1].Value, @"l:(\w+)");
                var language       = languageOption.Success ? languageOption.Groups[1].Value : "cs";
                var sourceCode     = textParts.Groups[2].Value;

                // Replace the compression tokens
                if (language is "cs")
                {
                    sourceCode = ReplaceTokens(sourceCode, _sharplabCSTokens);

                    // Strip using directives
                    sourceCode = Regex.Replace(sourceCode, @"using \w+(?:\.\w+)*;", string.Empty);
                }
                else if (language is "il")
                {
                    sourceCode = ReplaceTokens(sourceCode, _sharplabILTokens);
                }
                else
                {
                    sourceCode = sourceCode.Replace("@@", "@");
                }

                var maxPreviewLength = EmbedBuilder.MaxDescriptionLength - (markdownLength + language.Length + "```\n\n```".Length);

                preview = FormatUtilities.FormatCodeForEmbed(language, sourceCode, maxPreviewLength);

                return(!string.IsNullOrWhiteSpace(preview));
            }
            catch
            {
                preview = null;
                return(false);
            }
        }
Ejemplo n.º 2
0
        private void VerifyWithLength(string language, int length, string source, string expected)
        {
            var actual = FormatUtilities.FormatCodeForEmbed(language, source, length);

            actual.ShouldBe(expected.Replace("\r", string.Empty));
        }