Skip to content

Commit

Permalink
Merge pull request #142 from bburdette/string-renderer
Browse files Browse the repository at this point in the history
String renderer (WIP)
  • Loading branch information
dillonkearns authored Apr 25, 2024
2 parents 0623de2 + 6bfd048 commit fa2bcda
Show file tree
Hide file tree
Showing 3 changed files with 435 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:
- name: elm-format
run: npx --no-install elm-format --validate src/ benchmarks/src/ review/src/ examples/src/ tests/
- name: Add elm-review, elm and elm-format to path
run: npm bin >> $GITHUB_PATH
run: echo "${{ github.workspace }}/node_modules/.bin" >> $GITHUB_PATH
- uses: sparksp/elm-review-action@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
161 changes: 159 additions & 2 deletions src/Markdown/Renderer.elm
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
module Markdown.Renderer exposing
( Renderer, render
, defaultHtmlRenderer
, defaultHtmlRenderer, defaultStringRenderer
, renderWithMeta
)

{-|
@docs Renderer, render
@docs defaultHtmlRenderer
@docs defaultHtmlRenderer, defaultStringRenderer
## Attaching Metadata to Blocks
Expand Down Expand Up @@ -255,6 +255,163 @@ defaultHtmlRenderer =
}


{-| This renders the parsed markdown structs to a string.
-}
defaultStringRenderer : Renderer String
defaultStringRenderer =
{ heading =
\{ level, children } ->
(case level of
Block.H1 ->
"# " ++ String.concat children

Block.H2 ->
"## " ++ String.concat children

Block.H3 ->
"### " ++ String.concat children

Block.H4 ->
"#### " ++ String.concat children

Block.H5 ->
"##### " ++ String.concat children

Block.H6 ->
"###### "
++ String.concat children
)
++ "\n\n"
, paragraph =
\strs ->
String.concat strs
++ "\n\n"
, hardLineBreak = " \n"
, blockQuote =
\strs ->
strs
|> List.map (\s -> " " ++ s ++ "\n")
|> String.concat
, strong =
\s ->
String.concat
("**" :: s ++ [ "**" ])
, emphasis =
\s ->
String.concat
("*" :: s ++ [ "*" ])
, strikethrough =
\s ->
String.concat
("~~" :: s ++ [ "~~" ])
, codeSpan =
\s ->
"`" ++ s ++ "`"
, link =
\link content ->
String.concat
[ "["
, String.concat content
, "]("
, link.destination
, ")"
]
, image =
\imageInfo ->
String.concat
[ "!["
, imageInfo.alt
, "]("
, imageInfo.src
, ")"
]
, text = identity
, unorderedList =
\items ->
items
|> List.map
(\listitem ->
case listitem of
Block.ListItem Block.NoTask childs ->
"- " ++ String.concat childs ++ "\n"

Block.ListItem Block.IncompleteTask childs ->
"- [ ]" ++ String.concat childs ++ "\n"

Block.ListItem Block.CompletedTask childs ->
"- [x]" ++ String.concat childs ++ "\n"
)
|> String.concat
, orderedList =
\startingIndex items ->
items
|> List.indexedMap (\i item -> String.fromInt (i + startingIndex) ++ ") " ++ String.concat item ++ "\n")
|> String.concat
, html = Markdown.Html.oneOf []
, codeBlock =
\{ body, language } ->
String.concat
[ "```"
, language |> Maybe.withDefault ""
, "\n"
, body
, "```\n\n"
]
, thematicBreak = "--------------------\n"
, table = String.concat >> (++) "\n"
, tableHeader =
-- we get the whole header as one string here, contained in a single element list.
List.map
twoheads
>> String.concat
, tableBody = List.foldr (\s l -> s :: "\n" :: l) [] >> String.concat
, tableRow = List.intersperse " | " >> String.concat
, tableHeaderCell =
\maybeAlignment strs ->
String.concat strs
++ " | "
++ (case maybeAlignment of
Just Block.AlignLeft ->
":-"

Just Block.AlignRight ->
"-:"

Just Block.AlignCenter ->
":-:"

Nothing ->
"--"
)
, tableCell =
\_ strs ->
String.concat strs
}


twoheads : String -> String
twoheads headstr =
headstr
|> String.split " | "
|> toheads ( [], [] )
|> (\( heads, aligns ) ->
String.concat (List.intersperse " | " heads)
++ "\n"
++ String.concat (List.intersperse "|" aligns)
++ "\n"
)


toheads : ( List String, List String ) -> List String -> ( List String, List String )
toheads ( llst, rlst ) strs =
case strs of
l :: r :: cdr ->
toheads ( l :: llst, r :: rlst ) cdr

_ ->
( List.reverse llst, List.reverse rlst )


{-| Apply a `Renderer` to turn parsed `Markdown.Block`s into your rendered markdown view.
-}
render :
Expand Down
Loading

0 comments on commit fa2bcda

Please sign in to comment.