Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
add sidebar basepath logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Freymaurer committed Jul 8, 2022
1 parent 3036cf4 commit 1de8f07
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module internal Aux =

module NfdiSidebarElementHeader =

type SidebarHeaderRenderer() =
type SidebarHeaderRenderer(?productionBasePath: string) =
inherit HeadingRenderer()

override __.Write(renderer : HtmlRenderer, hb : HeadingBlock ) =
Expand All @@ -29,9 +29,6 @@ module NfdiSidebarElementHeader =
"h1";
"h2";
"h3";
// "nfdi-h4";
// "nfdi-h5";
// "nfdi-h6";
|]
let index = hb.Level - 1
let headingText =
Expand All @@ -45,7 +42,11 @@ module NfdiSidebarElementHeader =

let attr = hb.GetAttributes()
attr.AddProperty("slot", "inner")
if href.IsSome then attr.AddProperty("href", href.Value)
if href.IsSome then
attr.AddProperty(
"href",
if productionBasePath.IsSome then System.IO.Path.Combine(productionBasePath.Value, href.Value) else href.Value
)

if (renderer.EnableHtmlForBlock) then
renderer.Write('<') |> ignore
Expand All @@ -63,14 +64,15 @@ module NfdiSidebarElementHeader =
renderer.EnsureLine() |> ignore

/// An extension for Markdig that highlights syntax in fenced code blocks
type SidebarHeaderExtension() =
type SidebarHeaderExtension(?productionBasePath: string) =

interface IMarkdownExtension with

member __.Setup(_) = ()

member __.Setup(_, renderer) =
renderer.ObjectRenderers.ReplaceOrAdd<HeadingRenderer>(new SidebarHeaderRenderer()) |> ignore
let x = if productionBasePath.IsSome then new SidebarHeaderRenderer(productionBasePath.Value) else new SidebarHeaderRenderer()
renderer.ObjectRenderers.ReplaceOrAdd<HeadingRenderer>(x) |> ignore

open System.Runtime.CompilerServices

Expand All @@ -79,6 +81,7 @@ module NfdiSidebarElementHeader =
[<Extension>]
// <summary>Highlight code in fenced code blocks</summary>
// <param name="pipeline">The Markdig <see cref="MarkdownPipelineBuilder"/> to add the extension to</param>
static member UseSidebarHeader(pipeline : MarkdownPipelineBuilder) =
pipeline.Extensions.Add(SidebarHeaderExtension())
static member UseSidebarHeader(pipeline : MarkdownPipelineBuilder, ?productionBasePath) =
let x = if productionBasePath.IsSome then SidebarHeaderExtension(productionBasePath.Value) else SidebarHeaderExtension()
pipeline.Extensions.Add(x)
pipeline
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ let pipeline =
.UseSidebarHeader()
.Build()

let basePathPipeline =
let builder = new MarkdownPipelineBuilder()
builder
.UseSidebarHeader("TestURL")
.Build()

[<Tests>]
let tests =
testList "UseSidebarHeader" [
Expand All @@ -29,4 +35,9 @@ let tests =
let result = Markdown.ToHtml(markdown, pipeline)
Expect.equal result $"""<h3 slot="inner">Start testing!</h3>{'\010'}""" ""
}
test "basePathPipeline" {
let markdown = """# Start testing!:/docs/start-testing"""
let result = Markdown.ToHtml(markdown, basePathPipeline)
Expect.equal result $"""<h1 slot="inner" href="TestURL/docs/start-testing">Start testing!</h1>{'\010'}""" ""
}
]

0 comments on commit 1de8f07

Please sign in to comment.