diff --git a/src/Lunet.Markdig/MarkdownOptions.cs b/src/Lunet.Markdig/MarkdownOptions.cs new file mode 100644 index 0000000..a3f6d31 --- /dev/null +++ b/src/Lunet.Markdig/MarkdownOptions.cs @@ -0,0 +1,26 @@ +// Copyright (c) Alexandre Mutel. All rights reserved. +// This file is licensed under the BSD-Clause 2 license. +// See the license.txt file in the project root for more information. + +using Lunet.Core; + +namespace Lunet.Markdown; + +public class MarkdownOptions : DynamicObject +{ + public MarkdownOptions(MarkdownPlugin parent) : base(parent) + { + } + + public string Extensions + { + get => GetSafeValue("extensions", "advanced"); + set => SetValue("extensions", value); + } + + public string CssImageAttribute + { + get => GetSafeValue("css_img_attr"); + set => SetValue("css_img_attr", value); + } +} \ No newline at end of file diff --git a/src/Lunet.Markdig/MarkdownPlugin.cs b/src/Lunet.Markdig/MarkdownPlugin.cs index e7a2f68..9c27a2b 100644 --- a/src/Lunet.Markdig/MarkdownPlugin.cs +++ b/src/Lunet.Markdig/MarkdownPlugin.cs @@ -3,6 +3,7 @@ // See the license.txt file in the project root for more information. using System; +using System.Collections.Generic; using System.IO; using System.Text.RegularExpressions; using System.Threading; @@ -11,6 +12,7 @@ using Lunet.Markdown.Extensions; using Markdig; using Markdig.Renderers; +using Markdig.Renderers.Html; using Markdig.Syntax; using Markdig.Syntax.Inlines; using Scriban.Functions; @@ -27,13 +29,13 @@ public class MarkdownModule : SiteModule public class MarkdownPlugin : SitePlugin, ILayoutConverter { - private readonly DynamicObject _markdigOptions; + private readonly MarkdownOptions _markdigOptions; private readonly DynamicObject _markdownHelper; private readonly ThreadLocal _markdownPipeline; public MarkdownPlugin(SiteObject site, LayoutPlugin layoutPlugin) : base(site) { - _markdigOptions = new DynamicObject(this); + _markdigOptions = new MarkdownOptions(this); _markdownHelper = new DynamicObject(this); _markdownPipeline = new ThreadLocal(); @@ -73,16 +75,16 @@ private MarkdownPipeline GetPipeline() { var builder = new MarkdownPipelineBuilder(); - if (_markdigOptions.Count == 0) + switch (_markdigOptions.Extensions) { - builder.UseAdvancedExtensions(); - } - else - { - // would need a different caching strategy - // TODO: handle Markdig options - } + // TODO: Add support for other extensions. + case "advanced": + default: + builder.UseAdvancedExtensions(); + break; + } + builder.Extensions.AddIfNotAlready(); pipeline = builder.Build(); _markdownPipeline.Value = pipeline; @@ -105,6 +107,10 @@ private string ToHtml(ContentObject page, MarkdownPipeline pipeline) var markdown = page.Content; var markdownDocument = Markdig.Markdown.Parse(markdown, pipeline); + // Get css_img_attr + string cssImgAttr = _markdigOptions.CssImageAttribute; + var cssImgAttrParts = cssImgAttr is null ? Array.Empty() : cssImgAttr.Split(','); + foreach (var inline in markdownDocument.Descendants()) { string url = null; @@ -166,6 +172,16 @@ private string ToHtml(ContentObject page, MarkdownPipeline pipeline) link.AppendChild(new LiteralInline(label)); } link.Url = resolvedUrl; + + // Apply css_img_attr by adding class attribute to all images + if (link.IsImage && cssImgAttrParts.Length > 0) + { + var attr = link.GetAttributes(); + foreach (var cssClass in cssImgAttrParts) + { + attr.AddClass(cssClass); + } + } } var renderer = new HtmlRenderer(new StringWriter());