예제 #1
0
        /// <summary>
        /// 将html转换为markdown
        /// </summary>
        /// <param name="source">待转换的html代码</param>
        /// <returns>生成的markdown代码</returns>
        public static string ConvertHtmlToMarkdown(string source)
        {
            if (string.IsNullOrEmpty(source))
            {
                return("");
            }



            var config = new ReverseMarkdown.Config
            {
                UnknownTags = Config.UnknownTagsOption.Bypass,
                // generate GitHub flavoured markdown, supported for BR, PRE and table tags
                GithubFlavored = false,
                // will ignore all comments
                RemoveComments = true,
                // remove markdown output for links where appropriate
                SmartHrefHandling = true
            };

            var converter = new ReverseMarkdown.Converter(config);
            var markdown  = converter.Convert(ClearHtml(source));

            return(markdown);
        }
예제 #2
0
    private void buttonEditMarkdown_Click(object sender, EventArgs e)
    {
        if (htmlDescription.Visible)
        {
            var config = new ReverseMarkdown.Config
            {
                UnknownTags       = ReverseMarkdown.Config.UnknownTagsOption.PassThrough, // Include the unknown tag completely in the result (default as well)
                GithubFlavored    = true,                                                 // generate GitHub flavoured markdown, supported for BR, PRE and table tags
                RemoveComments    = false,                                                // will ignore all comments
                SmartHrefHandling = true                                                  // remove markdown output for links where appropriate
            };
            var    converter = new ReverseMarkdown.Converter(config);
            string html      = htmlDescription.BodyHtml;
            string result    = converter.Convert(html);
            textDescription.Text = result;
        }
        else if (webView2.Visible)
        {
            textDescription.Text = webView2.TextUrl;
        }

        _com.Model.ContentType = "markdown";

        EnableMarkdownView();
    }
예제 #3
0
        private void ProcessArticleRawContentSmartReader(Article article, ReverseMarkdown.Converter converter)
        {
            _logger.LogTrace($"Parsing article content for {article.ArticleUrl}");

            var parsedarticle = SmartReader.Reader.ParseArticle(article.ArticleUrl, article.RawContent);

            article.Content = converter.Convert(parsedarticle.IsReadable ? parsedarticle.Content : article.Description);
            if (string.IsNullOrWhiteSpace(article.Content))
            {
                article.Content = "###### no content";
            }
        }
예제 #4
0
    public async Task Usage()
    {
        #region Usage

        var converter = new ReverseMarkdown.Converter();

        string html = "This a sample <strong>paragraph</strong> from <a href=\"http://test.com\">my site</a>";

        string result = converter.Convert(html);

        #endregion

        await Verifier.Verify(result);
    }
예제 #5
0
        private void ProcessArticleRawContentXPathSelector(Article article, ReverseMarkdown.Converter converter)
        {
            var html = new HtmlDocument();

            html.LoadHtml(article.RawContent);
            var document = html.DocumentNode;

            article.Title       = document.SelectSingleNode(article.Feed.ParserTitle)?.InnerText;
            article.Description = document.SelectSingleNode(article.Feed.ParserDescription)?.InnerText;
            article.Content     = converter.Convert(
                string.Join("", document.SelectNodes(article.Feed.ParserContent)
                            ?.Select(x => x.OuterHtml))
                );
            article.Author = document.SelectSingleNode(article.Feed.ParserAuthor)?.InnerText;
            article.Tags   = string.Join(", ", document.SelectNodes(article.Feed.ParserTags)?.Select(x => x.InnerText));
        }
예제 #6
0
        public static string Convert(string html)
        {
            html = PreMarkdownConversion.Process(html);

            var ignoreHtmlTags = new HashSet <string> {
                "excerpt", "mark", "sup", "dd", "dl", "dt", "font", "char", "type", "crmwebsiteroot"
            };

            string result = "";

            try
            {
                result = converter.Convert(html);
            } catch (Exception e)
            {
                var error = e;
                if (e.Message.Contains("Unknown tag: "))
                {
                    var tag = e.Message.Replace("Unknown tag: ", "");
                    if (ignoreHtmlTags.Contains(tag))
                    {
                    }
                    else
                    {
                        result = html;
                    }
                }
            }


            result = PostMarkdownConversion.Process(result);

            if (string.IsNullOrEmpty(result))
            {
                return(" ");
            }

            return(result.Trim());
        }
 private static void CheckConversion(string html, string expected)
 {
     var converter = new Converter();
     var result = converter.Convert(html);
     Assert.Equal<string>(expected, result);
 }