0

I'm working on a Node.js application and need to append newline characters to a file in a way that is valid and recognized by various text editors and tools(Specific for .bib files).

I tried to add \n after each field. This is one case.

const data = 'This is some text that I want to append with a new line';
const newline = '\n';

res.setHeader('Content-Type', 'text/x-bibtex;charset=utf-8');
res.setHeader('Content-Disposition', `attachment;filename=${filename}`);
res.send(data);

However, when I open the file in a text editor, the newline character doesn't seem to be recognized, and everything appears on a single line.

David
  • 208,112
  • 36
  • 198
  • 279
Paris
  • 23
  • 8
  • 2
    ...Where is the newline character used here? – David Jun 27 '23 at 11:26
  • 1
    You're describing different problems/features that don't really belong together: produce the line feed requested by certain file format, produce a line feed recognised by the builtin text editor of some operating system, make your own text editor recognise it... – Álvaro González Jun 27 '23 at 11:27

2 Answers2

1

You can use the built in os module. Its valid and recognized by various text editors and tools, you need to use the appropriate newline sequence based on the operating system's conventions

const os = require('os');
const newline = os.EOL;
fs.appendFileSync(filePath, `${data}${newline}`, { encoding: 'utf8' });
CMartins
  • 3,247
  • 5
  • 38
  • 53
  • 1
    This will emit the server's EOL style, which is pretty much irrelevant for users. It's also unclear whether the file format mandates any specific style. – Álvaro González Jun 27 '23 at 12:00
0

I was doing some sort of

const data = 'This is some text that I want to append with a new line'.trim().replace();

So I just add '/n/ after replace method :

const data = 'This is some text that I want to append with a new line'.trim().replace()+ '/n';
Paris
  • 23
  • 8