0

Wondering what methods exist to preserve things like tabs and newlines when storing text in a JSON database? There are a couple ways I imagine going about this:

  • Storing text in markup and reading the JSON string as html. (Not sure what the best methods are for this.)
  • Storing JavaScript strings with \n and \t characters and trying to interpolate those characters properly i.e. not just printing "\nThis is a newline." Which it does by default.

EDIT: The example problem I'm having is using angular binding to print strings.

blogPost {
  title: 'Hello There',
  body: '\nThis is \ta test!'
}

Then in the html file:

<p>{{blogPost.body}}</p>

This renders 'This is a test' with no new line or tab but also not showing the \n \t characters. However when I use firebase console to store the string in the JSON object and call the storage from typescript I get the same object it prints like this '\nThis is \ta test!' with the characters showing (and not tabbing or newlining) both of which are undesirable behavior.

Zachscs
  • 3,353
  • 7
  • 27
  • 52
  • What problems do you encounter when `\n` and `\t` are included? They are universally accepted as non printing characters – charlietfl Nov 18 '17 at 14:01
  • @charlietfl when I print the string it prints the characters '\n' not a new line. – Zachscs Nov 18 '17 at 14:07
  • Print it where and how? Sounds very strange. Provide a [mcve] – charlietfl Nov 18 '17 at 14:09
  • @charlietfl Ok one moment – Zachscs Nov 18 '17 at 14:10
  • @charlietfl is that sufficient? – Zachscs Nov 18 '17 at 14:19
  • So my guess is you are typing those characters into the firebase console making them literal characters in the text. If you can see them when you do the input you will see them in the output – charlietfl Nov 18 '17 at 14:23
  • Yes that is correct. Regular string also don't work as expected, they don't display the \n \t characters but they don't newline or tab either. – Zachscs Nov 18 '17 at 14:24
  • They will if you pass them into a textaraea or `
    ` tag or use css rules to allow them to work. Otherwise they are ignored in html text
    – charlietfl Nov 18 '17 at 14:27
  • So how should I upload the text to store that formatting? Right the
     tag works properly, but how should I upload the text so that the \n \t are read as desired?
    – Zachscs Nov 18 '17 at 14:27
  • Depends where you generate that text. If it is generated in a textarea or copy/pasted with breaks and tabs they will be preserved – charlietfl Nov 18 '17 at 14:30
  • If you want them to display in html convert them to html entities like `
    ` and multiple ` `
    – charlietfl Nov 18 '17 at 14:33
  • Try

    . See https://stackoverflow.com/questions/31548311/angular-html-binding
    – Andrey Nov 18 '17 at 14:37
  • So I tried copying and pasting the formatted text from a text editor (libre office) into the fire base console and the formatting is not preserved. It does seem to turn newlines into tabs... When I enter markup entities into the firebase console it just prints them as part of the string. – Zachscs Nov 18 '17 at 14:39
  • If you put html into the text you can't render using `{{}}` as it only renders text not html and will treat the html tags as literal characters. See `[innerHTML]` comment above – charlietfl Nov 18 '17 at 14:40
  • Right thanks the solution you linked worked I'll just add html tags, thanks! – Zachscs Nov 18 '17 at 14:42
  • Note you can store as `\n` and `\t` and use string replace to generate html prior to rendering, or just store html. Depends on use case – charlietfl Nov 18 '17 at 14:44

0 Answers0