aichaos / rivescript-js

A RiveScript interpreter for JavaScript. RiveScript is a scripting language for chatterbots.
https://www.rivescript.com/
MIT License
377 stars 144 forks source link

line breaking not working #349

Closed enauman closed 4 years ago

enauman commented 4 years ago

Line breaking using the ^ continuation command is not working. The line breaks don't happen, whether \n is added to the line or not. I'm using Chrome V 85.

kirsle commented 4 years ago

Hey @enauman

The \n line breaks do work, but if you're displaying the result on a web page (HTML), a line break doesn't mean much in HTML except it adds a single space character instead.

https://play.rivescript.com/s/0q70ALPNGa

! version = 2.0

// Output: "Line1Line 2"
+ test 1
- Line 1
^ Line 2

// Output: "Line 1\nLine 2" or on the web, "Line 1 Line 2"
+ test 2
- Line 1\n
^ Line 2

// this setting will automatically imply the \n character
// for all ^Continues after this line
! local concat = newline

// Output: "Line 1\nLine 2"
+ test 3
- Line 1
^ Line 2

If you run that code in a text-based environment (i.e. node shell.js in this repo, or the express.js json-server example) the line feeds come through, but in HTML they don't:

For this either write the <br> tag in your RiveScript code, or else do a find/replace on the output to convert "\n" to <br> like:

bot.reply(username, message).then(response => {
    response = response.replace(/\n/g, "<br>");
    // now the response can use <br> tags in HTML for line breaks
});
enauman commented 4 years ago

Oh, of course! Thank you that makes sense.