hjson / hjson-js

Hjson for JavaScript
https://hjson.github.io/
MIT License
417 stars 49 forks source link

Support ES5 style multiline strings using backtick #30

Closed MartinMuzatko closed 6 years ago

MartinMuzatko commented 7 years ago

Hello! First: thanks for this awesome module! It does exactly what I need. I would love to propose multiline strings using the backtick - as introduced with ES5.

e.g.:

{
    string: `multi
        line
        testing
    `
}

Your thoughts?

laktak commented 6 years ago

Thank you for the suggestion. I guess you meant ES6 template literals?

Multiline strings in Hjson are quite different - they allow you to indent without having to worry about whitespace and/or escapes. So I don't really see the value of these template literals here.

MartinMuzatko commented 6 years ago

My thought was, that ''' would be pretty non-standard compared to . As'''has its roots in CSON and different languages that wanted to be able to markup multiple lines. However, with the latest additions to the standards (namely template literals) we should be able to convey the same idea and functionality using.

How does ''' and differ? How does'''` work around a whitespace problem? Are the beginning tabs/spaces ignored?

Best, Martin

laktak commented 6 years ago

Yes, for example:

{
  md:
    '''
    First line.
    Second line.
      This line is indented by two spaces.
    '''
}

So I don't see any advantages of ` over the current implementation.

nyurik commented 6 years ago

@laktak I also think ''' is bad because it conflicts with other formatting systems. For example, I have been thinking of using HJSON in Wikipedia for many inline JSON blobs. The ''' already has a meaning -- make something bold. Currently, the only two JSON elements that need special handling are | (pipe) and }}. The pipe can be escaped with {{!}}, and }} can be escaped by writing it with a space - } }. For JSON strings, they can be replaced with \x7b .. \x7d.

With HJSON, I don't see any way to escape ''' within wiki markup, whereas the backtick is not used at the moment, and can be safely used in place of ''' or even ".

laktak commented 6 years ago

@nyurik Not having an escape is a feature and IMO way better than having to escape every \.

Would having a variable number of opening and closing ' help? Like ''''' ml string containing ''' ''''' - that's a different issue though so I'm going to close this.

nyurik commented 6 years ago

@laktak sadly no - in wiki markup, '' -- italic, ''' -- bold, and it could be something like '''''bold-italic''' italic''. I could try to adapt HJSON to Wiki markup only if ''' was disabled. At the moment, Wiki uses JSON + allow comments + allow trailing commas, but nothing else.