Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String renderer (WIP) #142

Merged
merged 13 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading