예제 #1
0
    /// <summary>
    /// Gets a preview of the given markdown's rendered HTML.
    /// </summary>
    /// <param name="options">An <see cref="IWikiOptions"/> instance.</param>
    /// <param name="dataStore">An <see cref="IDataStore"/> instance.</param>
    /// <param name="markdown">The markdown content.</param>
    /// <returns>A preview of the rendered HTML.</returns>
    public static string RenderPreview(IWikiOptions options, IDataStore dataStore, string?markdown)
    {
        if (string.IsNullOrWhiteSpace(markdown))
        {
            return(string.Empty);
        }

        var document = Markdown.Parse(markdown, WikiConfig.GetMarkdownPipeline(options, dataStore));

        if (AnyPreviews(document))
        {
            TrimNonPreview(document);
        }
        else
        {
            var minCharactersAvailable = PreviewCharacterMin;
            var maxCharactersAvailable = PreviewCharacterMax;
            Trim(document, ref minCharactersAvailable, ref maxCharactersAvailable);
        }

        string html;

        using (var writer = new StringWriter())
        {
            var renderer = new HtmlRenderer(writer);
            WikiConfig.GetMarkdownPipeline(options, dataStore).Setup(renderer);
            renderer.Render(document);
            html = writer.ToString();
        }

        if (!string.IsNullOrWhiteSpace(html) &&
            options.Postprocessors is not null)
        {
            foreach (var preprocessor in options.Postprocessors)
            {
                html = preprocessor.Process.Invoke(html);
            }
        }

        return(WikiConfig.GetHtmlSanitizer(options).Sanitize(html) ?? string.Empty);
    }
예제 #2
0
    /// <summary>
    /// Renders the given <paramref name="markdown"/> as HTML.
    /// </summary>
    /// <param name="options">An <see cref="IWikiOptions"/> instance.</param>
    /// <param name="dataStore">An <see cref="IDataStore"/> instance.</param>
    /// <param name="markdown">The markdown content.</param>
    /// <returns>The rendered HTML.</returns>
    public static string RenderHtml(IWikiOptions options, IDataStore dataStore, string?markdown)
    {
        if (string.IsNullOrWhiteSpace(markdown))
        {
            return(string.Empty);
        }

        var html = Markdown.ToHtml(markdown, WikiConfig.GetMarkdownPipeline(options, dataStore));

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

        if (options.Postprocessors is not null)
        {
            foreach (var preprocessor in options.Postprocessors)
            {
                html = preprocessor.Process.Invoke(html);
            }
        }

        return(WikiConfig.GetHtmlSanitizer(options).Sanitize(html));
    }