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

Trailing spaces in string blocks are ignored even in Mark.New.string

tad-lispy opened this issue · comments

When parsing a string block like this:

|> Title
    There are spaces here ->  

the trailing spaces will be ignored. In most cases it makes sense - probably the whitespace is there by mistake. The problem is that when creating a new block programmatically with Mark.New the trimming is also applied. This makes incremental updates to string block difficult.

Say I have a text input that on change replaces the text block:

setTitle : Mark.Edit.Id -> String -> Mark.Parsed -> Result (List Error) Mark.Parsed
setTitle id value ast =
    let
        block =
            Mark.New.string value

        edit =
            Mark.Edit.replace id block
    in
    Mark.update document edit ast

This works great except when a user tries to insert a space at the end of the field with intention of adding another word after it. Then the space simply does not get added.

It seems to me that trimming should be applied when parsing, but not when blocks are created with Mark.New.string.

After some investigation I see that actually the whitespace is preserved in AST (Mark.Parsed) and trimmed when it's rendered. It seems to me that trimming should be a responsibility of the parser, not the render function. I'll prepare a PR. Please let me know if you disagree.

As usual, thanks for your awesome work 😁