Understanding Node.js Special Character Processing When Writing Files

Asked 1 years ago, Updated 1 years ago, 59 views

I am creating a Latex editor, and I am struggling to write files there, so I would like to ask you to teach me.
I would like to post the text data you entered to the server in json format and write it to the .tex file by converting the special character \\ contained in the text data to \.

Some of the json data to post (line feed is made for easy viewing)

"\\ documentclass {jsarticle}\n
\\uspackage [dvipdfmx] {graphicx}\n
\\setlength {\\textheight}{24cm}\n
\\setlength{\topmargin}{-1.5cm}\n
\\setlength{\\textwidth}{17cm}\n
\\setlength {\\oddsidemargin}{-.5cm}\n
\\uspackage {here}\n
......

Server-side .tex file

"\\ documentclass {jsarticle}
\\uspackage [dvipdfmx] {graphicx}
\\setlength {\\textheight}{24cm}
US>\setlength {\\topmargin} {-1.5cm}
US>\\setlength {\\textwidth} {17cm}
US>\setlength {\\oddsidemargin} {-.5cm}
\\uspackage {here}
....

The created tex file will have \\ written as above, which is different from the original latex writing method...

Ideally, I would like to write in the text file as follows:

"\ documentclass {jsarticle}
\uspackage [dvipdfmx] {graphicx}
\setlength{24cm}
\setlength
\setlength
\setlength {-.5cm}
\uspackage {here}
....

The following processing codes are currently being processed by the server.

jsonData=JSON.stringify(req.body.msg);
  console.log(jsonData);
  vararr=jsonData.split(/\\n/);
  for (vari=0;i<arr.length;i++) {
    console.log(arr[i]);
    //arr[i]=arr[i].replace(/\\/g, ');
    fs.appendFileSync('sample.tex',arr[i]+'\n',function(err){
      console.log(err);
    });
  }

msg contains all the text data. I am commenting out because I am not good at it.
Would it be possible to write \ in the text file?
Thank you <(__)>


In the editor, you type as follows:

\ documentclass {jsarticle}
\uspackage [dvipdfmx] {graphicx}
\setlength{24cm}
\setlength
\setlength
\setlength {-.5cm}
\uspackage {here}
....

The client sent the text data using the following code.
*We are creating an editor using Ace.

$('#save').click(function(e){
      // alert(editor.getValue());
      $.ajax({
        url: 'http://localhost:3000/editor',
        type: 'post',
        data: {'msg':editor.getValue(),
        dataType: 'json'
      });
    });

The data entered in editor.getValue() is as follows.

"\\ documentclass {jsarticle}
\\uspackage [dvipdfmx] {graphicx}
\\setlength {\\textheight}{24cm}
US>\setlength {\\topmargin} {-1.5cm}
US>\\setlength {\\textwidth} {17cm}
US>\setlength {\\oddsidemargin} {-.5cm}
\\uspackage {here}
......"

I am sending this as json data, so I believe that you will create a tex file with \\ on it.

javascript node.js json regular-expression

2022-09-30 17:52

1 Answers

Create a tex file on the server side

"Regarding this, is it correct that this text file ""I want to save what the user entered as a file?"""

And if what you enter is still in req.body.msg in JSON format, you can use JSON.parse to restore the original user-entered string from the JSON format string.

In other words, I think this is enough.

jsonData=JSON.parse(req.body.msg);
// jsonData contains user-entered string
console.log(jsonData);

fs.writeFileSync('sample.tex', jsonData, function(err){
  console.log(err);
});

However, the user may actually send strange data that is not in JSON format, causing an error in JSON.parse, or the result of JSON.parse may not be a string, so you need to take action as well.


2022-09-30 17:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.