mdgriffith / elm-markup

Elm-friendly markup

Home Page:https://package.elm-lang.org/packages/mdgriffith/elm-markup/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Newlines on Windows

lukewilliamboswell opened this issue · comments

Not sure if this is an issue but I thought I would mention in case there is a way to prevent someone else having a similar issue?

I wanted to get a minimal example of Elm Markup working using reactor using Visual Studio Code ... but had an issue with the windows new-line characters being rejected as an error by Mark.compile.

The workaround was to use String.replace "\u{000D}\n" "\n"

I have included an copy of the error and the code example below.

image

module Main exposing (main)

import Browser exposing (sandbox)
import Html exposing (Html)
import Html.Attributes as Attr
import Mark
import Mark.Error


main : Program () Model Msg
main =
    sandbox
        { init = init
        , view = view
        , update = update
        }


type alias Model =
    {}


init : Model
init =
    {}


type Msg
    = NoOP


update : Msg -> Model -> Model
update msg model =
    model


view : Model -> Html.Html msg
view model =
    case Mark.compile document source of
        Mark.Success html ->
            Html.div [] [ html ]

        Mark.Almost { result, errors } ->
            Html.div []
                [ Html.div [] (viewErrors errors)
                , Html.div [] [ result ]
                ]

        Mark.Failure errors ->
            Html.div []
                (viewErrors errors)


viewErrors errors =
    errors
        |> List.map
            (\error ->
                let
                    { title, message, region } =
                        Mark.Error.toDetails error
                in
                Html.p []
                    [ Html.text (Debug.toString <| Mark.Error.toDetails error)
                    ]
            )


document =
    Mark.document
        (\meta ->
            Html.node "blah"
                []
                [ Html.h1 [] [ Html.text meta.title ]
                , Html.h1 [] [ Html.text meta.description ]
                , Html.h1 [] [ Html.text meta.author ]
                ]
        )
        metadata


metadata =
    Mark.record "Article"
        (\author description title ->
            { author = author
            , description = description
            , title = title
            }
        )
        |> Mark.field "author" Mark.string
        |> Mark.field "description" Mark.string
        |> Mark.field "title" Mark.string
        |> Mark.toBlock


source =
    """
|> Article
    author = Matthew Griffith
    description = How I learned to use elm-markup.
    title = How I Learned /elm-markup/
"""
        |> String.trim
        -- Without the following line it will error on Windows
        -- |> String.replace "\u{000D}\n" "\n"

I am running into more subtle errors with newlines. The sympoms are paragraphs and even blocks not being detected correctly, leading to errors that are hard to trace back to the newlines.

The most devious one I had was a block that took a Mark.string, but that got interpreted as Mark.text (presumably because the block was not detected as a block due to the newlines) and the content containing [FILE]. The error message told me that [FILE] was not a known annotation.

After I switched the newline format in VSCode from CRLF to LF (you can do that by clicking on "CRLF" in the bottom right corner and choosing the other one), everything works as expected.

This leads me to think that it might be possible and worth it to handle these different newline codes in elm-markup itself.

I am having the same issue. I am having trouble compiling even basic markup syntaxes such as basic string parsing

Yeah, I've just encountered the same issue when working with this.