Skip to content

Commit

Permalink
Merge pull request #42 from krymtkts:feature/task-list
Browse files Browse the repository at this point in the history
Associate label to GitHub styled task lists.
  • Loading branch information
krymtkts committed Aug 27, 2023
2 parents 1b79629 + 7d28a5d commit 7e9e039
Showing 1 changed file with 37 additions and 21 deletions.
58 changes: 37 additions & 21 deletions src/Common.fs
Expand Up @@ -25,28 +25,45 @@ module private Util =
let markedHighlight: obj -> Marked.MarkedExtension = importMember "marked-highlight"

let renderer =
let heading =
fun (text: string) (level: int) ->
let escapedText = Regex.Replace(string text, @"[^\w]+", "-")
let l = string level
let heading text level =
let escapedText = Regex.Replace(text, @"[^\w]+", "-")
let l = level

$"""<h{l}><a name="{escapedText}" class="anchor" href="#{escapedText}">{text}</a></h{l}>"""
$"""<h{l}><a name="{escapedText}" class="anchor" href="#{escapedText}">{text}</a></h{l}>"""

let link =
fun href title text ->
let ref =
match href with
| Some s -> mdToHtml s
| None -> ""
let link href title text =
let ref =
match href with
| Some s -> mdToHtml s
| None -> ""

let title =
match title with
| null -> text
| _ -> title
let title =
match title with
| null -> text
| _ -> title

$"""<a href="{ref}" title="{title}">{text}</a>"""
$"""<a href="{ref}" title="{title}">{text}</a>"""

let mops = !!{| heading = heading; link = link |}
let listitem text task check =
let checkState =
match check with
| true -> "checked"
| false -> ""

match task with
| true ->
$"""<li><label class="checkbox"><input type="checkbox" class="checkbox" disabled {checkState} />{text}</label></li>"""
| false -> $"""<li>{text}</li>"""

let checkbox _ =
// NOTE: checkbox generation is handled by listitem.
""

let mops =
!!{| heading = heading
link = link
listitem = listitem
checkbox = checkbox |}


jsOptions<Marked.MarkedExtension> (fun o ->
Expand All @@ -55,10 +72,9 @@ module private Util =
o.headerIds <- Some true)

let highlighter =
let highlight =
fun (code: string) (lang: string) ->
(hljs.highlight (code, !!{| language = lang |}))
.value
let highlight (code: string) (lang: string) =
(hljs.highlight (code, !!{| language = lang |}))
.value

markedHighlight !!{| highlight = highlight |}

Expand Down

0 comments on commit 7e9e039

Please sign in to comment.