Ejemplo n.º 1
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();
    }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 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";
            }
        }
Ejemplo n.º 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);
    }
Ejemplo n.º 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));
        }
Ejemplo n.º 6
0
    public void UsageWithConfig()
    {
        #region UsageWithConfig

        var config = new ReverseMarkdown.Config
        {
            // Include the unknown tag completely in the result (default as well)
            UnknownTags = Config.UnknownTagsOption.PassThrough,
            // generate GitHub flavoured markdown, supported for BR, PRE and table tags
            GithubFlavored = true,
            // will ignore all comments
            RemoveComments = true,
            // remove markdown output for links where appropriate
            SmartHrefHandling = true
        };

        var converter = new ReverseMarkdown.Converter(config);

        #endregion
    }
Ejemplo n.º 7
0
        private async Task ProcessArticleRawContent(ApplicationDbContext context, Article article)
        {
            try
            {
                if (!string.IsNullOrWhiteSpace(article.Content))
                {
                    return;
                }

                var converter = new ReverseMarkdown.Converter();

                switch (article.Feed.ParserMode)
                {
                default:
                case ParserMode.None:
                    break;

                case ParserMode.SmartReader:
                    ProcessArticleRawContentSmartReader(article, converter);
                    break;

                case ParserMode.XPathSelector:
                    ProcessArticleRawContentXPathSelector(article, converter);
                    break;

                case ParserMode.YouTube:
                    ProcessArticleRawContentYouTube(article, converter);
                    break;
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"An error occurred while processing {article.ArticleUrl}");
                article.Content = $"# Error\n\n{ex.ToString()}";
            }
            finally
            {
                await context.SaveChangesAsync();
            }
        }
 private static void CheckConversion(string html, string expected)
 {
     var converter = new Converter();
     var result = converter.Convert(html);
     Assert.Equal<string>(expected, result);
 }