/// <summary>
        /// Parses content from a url as Markdown to HTML.
        /// </summary>
        /// <param name="url">A Url that contains Markdown</param>
        /// <param name="usePragmaLines">Generates line numbers as ids into headers and paragraphs. Useful for previewers to match line numbers to rendered output</param>
        /// <param name="forceReload">Forces the parser to reloaded. Otherwise cached instance is used</param>
        /// <param name="sanitizeHtml">Strips out scriptable tags and attributes for prevent XSS attacks. Minimal implementation.</param>
        /// <param name="fixupBaseUrl">Flag that determines whether relative Markdown images and links are fixed up with the document's base path</param>
        /// <param name="noHttpException">If true returns null instead of throwing a URL load exception</param>
        /// <returns>HTML result as a string</returns>
        public static async Task <string> ParseFromUrlAsync(string url, bool usePragmaLines = false, bool forceReload = false, bool sanitizeHtml = false, bool fixupBaseUrl = true, bool noHttpException = false)
        {
            if (string.IsNullOrEmpty(url))
            {
                return(url);
            }

            // Fix up common Markdown Urls like Github, MS docs, BitBucket root repos
            url = MarkdownUtilities.ParseMarkdownUrl(url);

            string content = null;

            try
            {
                var client = new WebClient();
                content = await client.DownloadStringTaskAsync(new Uri(url));
            }
            catch (Exception ex)
            {
                if (noHttpException)
                {
                    return(null);
                }

                throw new FileLoadException($"Couldn\'t load Markdown from Url: {url}.", ex);
            }

            if (fixupBaseUrl)
            {
                content = MarkdownUtilities.FixupMarkdownRelativePaths(content, url);
            }

            return(Parse(content, usePragmaLines, forceReload, sanitizeHtml));
        }
Esempio n. 2
0
        /// <summary>
        /// Parses content from a url as Markdown to HTML.
        /// </summary>
        /// <param name="url">A Url that contains Markdown</param>
        /// <param name="usePragmaLines">Generates line numbers as ids into headers and paragraphs. Useful for previewers to match line numbers to rendered output</param>
        /// <param name="forceReload">Forces the parser to reloaded. Otherwise cached instance is used</param>
        /// <param name="sanitizeHtml">Strips out scriptable tags and attributes for prevent XSS attacks. Minimal implementation.</param>
        /// <param name="fixupBaseUrl">Flag that determines whether relative Markdown images and links are fixed up with the document's base path</param>
        /// <param name="noHttpException">If true returns null instead of throwing an Excpetion for URL not found</param>
        /// <returns>HTML result as a string</returns>
        public static string ParseFromUrl(string url, bool usePragmaLines = false, bool forceReload = false, bool sanitizeHtml = false, bool fixupBaseUrl = true, bool noHttpException = false)
        {
            if (string.IsNullOrEmpty(url))
            {
                return(url);
            }

            // Fix up common Markdown Urls like Github, MS docs, BitBucket root repos
            url = MarkdownUtilities.ParseMarkdownUrl(url);

            string content = null;

            try
            {
#pragma warning disable SYSLIB0014
                var client = new WebClient();
#pragma warning restore SYSLIB0014
                content = client.DownloadString(new Uri(url));
            }
            catch (Exception ex)
            {
                if (noHttpException)
                {
                    return(null);
                }

                throw new FileLoadException("Couldn't load Markdown file: " + url, ex);
            }

            if (fixupBaseUrl)
            {
                content = MarkdownUtilities.FixupMarkdownRelativePaths(content, url);
            }

            return(Parse(content, usePragmaLines, forceReload, sanitizeHtml));
        }