Skip to content

Commit

Permalink
Add support for attaching class attribute to all markdown images (via…
Browse files Browse the repository at this point in the history
… `markdown.options.css_img_attr`)
  • Loading branch information
xoofx committed Aug 30, 2023
1 parent dcb0453 commit ed490c1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
26 changes: 26 additions & 0 deletions src/Lunet.Markdig/MarkdownOptions.cs
Original file line number Diff line number Diff line change
@@ -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<MarkdownPlugin>
{
public MarkdownOptions(MarkdownPlugin parent) : base(parent)
{
}

public string Extensions
{
get => GetSafeValue<string>("extensions", "advanced");
set => SetValue("extensions", value);
}

public string CssImageAttribute
{
get => GetSafeValue<string>("css_img_attr");
set => SetValue("css_img_attr", value);
}
}
36 changes: 26 additions & 10 deletions src/Lunet.Markdig/MarkdownPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -27,13 +29,13 @@ public class MarkdownModule : SiteModule<MarkdownPlugin>

public class MarkdownPlugin : SitePlugin, ILayoutConverter
{
private readonly DynamicObject<MarkdownPlugin> _markdigOptions;
private readonly MarkdownOptions _markdigOptions;
private readonly DynamicObject<MarkdownPlugin> _markdownHelper;
private readonly ThreadLocal<MarkdownPipeline> _markdownPipeline;

public MarkdownPlugin(SiteObject site, LayoutPlugin layoutPlugin) : base(site)
{
_markdigOptions = new DynamicObject<MarkdownPlugin>(this);
_markdigOptions = new MarkdownOptions(this);
_markdownHelper = new DynamicObject<MarkdownPlugin>(this);
_markdownPipeline = new ThreadLocal<MarkdownPipeline>();

Expand Down Expand Up @@ -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<XRefMarkdownExtension>();
pipeline = builder.Build();
_markdownPipeline.Value = pipeline;
Expand All @@ -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<string>() : cssImgAttr.Split(',');

foreach (var inline in markdownDocument.Descendants<Inline>())
{
string url = null;
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit ed490c1

Please sign in to comment.