private string CreateNodeAnnotationContent(OpenNodeAnnotationEventArgs e)
        {
            var writer = new StringWriter();

            try
            {
                writer.WriteLine(DocumentationBrowserUtils.GetContentFromEmbeddedResource(STYLE_RESOURCE));

                // Get the Node info section
                var nodeDocumentation = NodeDocumentationHtmlGenerator.FromAnnotationEventArgs(e);
                writer.WriteLine(nodeDocumentation);

                // Convert the markdown file to html
                MarkdownHandlerInstance.ParseToHtml(ref writer, e.MinimumQualifiedName, e.PackageName);

                writer.Flush();
                var output = writer.ToString();

                // Sanitize html and warn if any changes where made
                if (MarkdownHandlerInstance.SanitizeHtml(ref output))
                {
                    LogWarning(Resources.ScriptTagsRemovalWarning, WarningLevel.Mild);
                }

                // inject the syntax highlighting script at the bottom at the document.
                output += DocumentationBrowserUtils.GetDPIScript();
                output += DocumentationBrowserUtils.GetSyntaxHighlighting();

                return(output);
            }
            finally
            {
                writer?.Dispose();
            }
        }
        private string CreateNodeAnnotationContent(OpenNodeAnnotationEventArgs e)
        {
            var writer = new StringWriter();

            try
            {
                writer.WriteLine(DocumentationBrowserUtils.GetContentFromEmbeddedResource(STYLE_RESOURCE));

                // Get the Node info section and remove script tags if any
                var nodeDocumentation = NodeDocumentationHtmlGenerator.FromAnnotationEventArgs(e);
                if (MarkdownHandlerInstance.SanitizeHtml(ref nodeDocumentation))
                {
                    LogWarning(Resources.ScriptTagsRemovalWarning, WarningLevel.Mild);
                }

                writer.WriteLine(nodeDocumentation);

                // Convert the markdown file to html and remove script tags if any
                if (MarkdownHandlerInstance.ParseToHtml(ref writer, e.MinimumQualifiedName))
                {
                    LogWarning(Resources.ScriptTagsRemovalWarning, WarningLevel.Mild);
                }

                writer.Flush();
                return(writer.ToString());
            }
            finally
            {
                writer?.Dispose();
            }
        }
예제 #3
0
        /// <summary>
        /// Converts a markdown string into Html.
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="nodeNamespace"></param>
        /// <returns>Returns true if any script tags was removed from the string</returns>
        internal bool ParseToHtml(ref StringWriter writer, string nodeNamespace)
        {
            if (writer is null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            var mdFilePath = PackageDocumentationManager.Instance.GetAnnotationDoc(nodeNamespace);

            string mdString;
            bool   scriptTagsRemoved = false;

            if (string.IsNullOrWhiteSpace(mdFilePath) ||
                !File.Exists(mdFilePath))
            {
                mdString = DocumentationBrowserUtils.GetContentFromEmbeddedResource(NODE_ANNOTATION_NOT_FOUND);
            }

            else
            {
                // Doing this to avoid 'System.ObjectDisposedException'
                // https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2202?view=vs-2019
                Stream stream = null;
                try
                {
                    stream = File.Open(mdFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        stream   = null;
                        mdString = reader.ReadToEnd();
                    }
                }
                finally
                {
                    stream?.Dispose();
                }

                if (string.IsNullOrWhiteSpace(mdString))
                {
                    return(false);
                }

                // Remove scripts from user content for security reasons.
                if (SanitizeHtml(ref mdString))
                {
                    scriptTagsRemoved = true;
                }
            }

            var html = converter.ParseMd2Html(mdString, mdFilePath);

            writer.WriteLine(html);

            // inject the syntax highlighting script at the bottom at the document.
            writer.WriteLine(DocumentationBrowserUtils.GetDPIScript());
            writer.WriteLine(GetSyntaxHighlighting());

            return(scriptTagsRemoved);
        }
        /// <summary>
        /// Inject syntax highlighting into a html string.
        /// </summary>
        /// <param name="content"></param>
        internal static string GetSyntaxHighlighting()
        {
            var syntaxHighlightingContent = DocumentationBrowserUtils.GetContentFromEmbeddedResource(SYNTAX_HIGHLIGHTING);

            if (string.IsNullOrWhiteSpace(syntaxHighlightingContent))
            {
                return(string.Empty);
            }

            return(syntaxHighlightingContent);
        }
예제 #5
0
        /// <summary>
        /// Converts a markdown string into Html.
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="nodeNamespace"></param>
        internal void ParseToHtml(ref StringWriter writer, string nodeNamespace)
        {
            if (writer is null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            var mdFilePath = PackageDocumentationManager.Instance.GetAnnotationDoc(nodeNamespace);

            string mdString;

            if (string.IsNullOrWhiteSpace(mdFilePath) ||
                !File.Exists(mdFilePath))
            {
                mdString = DocumentationBrowserUtils.GetContentFromEmbeddedResource(NODE_ANNOTATION_NOT_FOUND);
            }

            else
            {
                // Doing this to avoid 'System.ObjectDisposedException'
                // https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2202?view=vs-2019
                Stream stream = null;
                try
                {
                    stream = File.Open(mdFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        stream   = null;
                        mdString = reader.ReadToEnd();
                    }
                }
                finally
                {
                    stream?.Dispose();
                }

                if (string.IsNullOrWhiteSpace(mdString))
                {
                    return;
                }
            }

            var html = converter.ParseMd2Html(mdString, mdFilePath);

            writer.WriteLine(html);
        }