Ali
Ali

Reputation: 10453

Uncaught SyntaxError: Unexpected token ILLEGAL multiple string

First of all: I know there are over hundres of articles/questions with the same titile or similar to this one, but let me explain what I'm trying to achieve and what is my problem here

I have a Markdown editor where I will store the user's content and if they wish they can edit that content later.

So, in my case I use elasticsearch to store all the user's content in there.

Storing information isn't a problem for me at all, but having to retrieve it and redisplay is a huge problem and I'm trying to solve this for many hours now.


With this Markdown editor, the user can enter something with no restriction at all, and I'm not sure if it is the way how my Markdown editor handle all these stuff incorrectly, but the newlines is a huge problem here.

If the user enter something like this

#Hello World

#Here you see I have two extra lines

#How about a video element here as well?


<iframe width="420" height="315" src="//www.youtube.com/embed/Uogdn7zWDmY" frameborder="0" allowfullscreen></iframe>

#Damn cool!!

Storing it obviously no problem, but when having to redisplay on the editor

I'm getting this error.

Uncaught SyntaxError: Unexpected token ILLEGAL

Fortunately I can fix this problem using something from my question that I asked here previously, but one problem fixed the other problem occurs.

The solution to the problem was to do something like this

var your_content = '    #Hello World
    
    #Here you see I have two extra lines

    #How about a video element here as well?


    <iframe width="420" height="315" src="//www.youtube.com/embed/Uogdn7zWDmY" frameborder="0" allowfullscreen></iframe>
    
    #Damn cool!!
';
var replaced_text = your_content.replace(/\n|\s/g, "");

But with the above solution everything would just be display like this:

#Hello World #Here you see I have two extra lines #How about a video element here as well? <iframe width="420" height="315" src="//www.youtube.com/embed/Uogdn7zWDmY" frameborder="0" allowfullscreen></iframe> #Damn cool!

So, the formatting of the code really messed up and this is another problem because the user obviously wants to be able to edit their code and with a nice formatting.

I'm using this Markdown editor http://dillinger.io

My server is : Nodejs, so everything I'm using is JavaScript

UPDATE

This is the snipped of the code:

  var editor
    , converter
    , autoInterval
    , paperImgPath = '/img/notebook_paper_200x200.gif'
    , profile = 
      {
        theme: 'ace/theme/idle_fingers'
      , showPaper: false
      , currentMd: '{{contentMd}}' <---- this is the problem
      , autosave: 
        {
          enabled: true
        , interval: 3000 // might be too aggressive; don't want to block UI for large saves.
        }
      , current_filename : 'Filename'
      }

And it doesn't matter where I store that {{contentMd}} I still get that error. If I store them like this

var abcd = '{{contentMd}}';

So the contentMd it is something being passed from the server and it is contain all whatever I posted on the top.

Upvotes: 0

Views: 3965

Answers (1)

akonsu
akonsu

Reputation: 29536

I think this is what happens:

> var s = 'long string
with carriage return';
SyntaxError: Unexpected token ILLEGAL

this is from Chrome's console window.

Try replacing all \n characters on the server with '\n' before pasting the string into javascript:

> var s = 'long string\nwith carriage return';
undefined
> s
"long string
with carriage return"

Upvotes: 2

Related Questions