This repository has been archived by the owner on Oct 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a506c9c
commit a54f783
Showing
3 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
namespace Fornax.Nfdi4Plants.MarkdigExtensions | ||
|
||
open System.Text | ||
open Markdig.Parsers; | ||
open Markdig.Renderers; | ||
open Markdig.Renderers.Html; | ||
open Markdig.Syntax; | ||
open Markdig | ||
|
||
module NfdiCode = | ||
|
||
open System | ||
|
||
// https://github.com/ilich/Markdig.Prism/blob/main/src/Markdig.Prism/PrismCodeBlockRenderer.cs | ||
type NFDICodeBlockRenderer() = | ||
inherit HtmlObjectRenderer<CodeBlock>() | ||
|
||
let extractSourcecode (node: LeafBlock) = | ||
let code = new StringBuilder() | ||
let lines = node.Lines.Lines | ||
let totalLines = lines.Length | ||
let rec appendLines (counter: int) (c: StringBuilder) = | ||
if counter >= totalLines then | ||
c | ||
else | ||
let line = lines[counter] | ||
let slice = line.Slice | ||
if isNull slice.Text then | ||
appendLines (counter + 1) c | ||
else | ||
let lineText = slice.Text.Substring(slice.Start, slice.Length); | ||
if counter > 0 then | ||
appendLines (counter+1) (c.AppendLine().Append(lineText)) | ||
else | ||
appendLines (counter+1) (c.Append(lineText)) | ||
appendLines 0 code | ||
|> fun x -> x.ToString() | ||
|
||
override this.Write(renderer : HtmlRenderer , cb : CodeBlock ) = | ||
|
||
if cb :? FencedCodeBlock && cb.Parser :? FencedCodeBlockParser then | ||
let fcb = cb :?> FencedCodeBlock | ||
let parser = cb.Parser :?> FencedCodeBlockParser | ||
let languageCode = fcb.Info.Replace(parser.InfoPrefix, "").Trim() | ||
let code = extractSourcecode(cb) | ||
if languageCode = "" then | ||
renderer | ||
.Write("<nfdi-code>") | ||
.Write(code) | ||
.Write("</nfdi-code>") | ||
|> ignore | ||
else | ||
let attributes = new HtmlAttributes() | ||
attributes.AddClass($"language-{languageCode}") | ||
|
||
renderer | ||
.Write("<nfdi-code") | ||
.WriteAttributes(attributes) | ||
.Write(">") | ||
.Write(code) | ||
.Write("</nfdi-code>") | ||
|> ignore | ||
else | ||
// let codeBlockRenderer = new CodeBlockRenderer() | ||
renderer.Write(cb) |> ignore | ||
|
||
/// An extension for Markdig that highlights syntax in fenced code blocks | ||
type NFDICodeExtension() = | ||
|
||
interface IMarkdownExtension with | ||
|
||
member __.Setup(_) = () | ||
|
||
member __.Setup(_, renderer) = | ||
renderer.ObjectRenderers.ReplaceOrAdd<CodeBlockRenderer>(new NFDICodeBlockRenderer()) |> ignore | ||
|
||
open System.Runtime.CompilerServices | ||
|
||
[<Extension>] | ||
type MarkdownPipelineBuilderExtensions() = | ||
[<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 UseNFDICodeBlock(pipeline : MarkdownPipelineBuilder) = | ||
pipeline.Extensions.Add(NFDICodeExtension()) | ||
pipeline |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters