jens-maus / node-ical

NodeJS class for parsing iCalendar/ICS files
Apache License 2.0
121 stars 52 forks source link

Unable to read DESCRIPTION field with \n characters #217

Open mrkmhny opened 2 years ago

mrkmhny commented 2 years ago

Example...

// import ical
const ical = require('node-ical');

// use the sync function parseFile() to parse this ics file
const events = ical.sync.parseFile('example-calendar.ics');
// loop through events and log them
for (const event of Object.values(events)) {
    console.log(
        'Summary: ' + event.summary +
        '\nDescription: ' + event.description +
        '\nStart Date: ' + event.start.toISOString() +
        '\n'
    );
};
const directEvents = ical.sync.parseICS(`
BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
BEGIN:VEVENT
SUMMARY:Hey look! An example event!
DTSTART;TZID=America/New_York:20130802T103400
DTEND;TZID=America/New_York:20130802T110400
LOCATION:1000 Broadway Ave.\, Brooklyn
DESCRIPTION: Do \nsomething in NY.
STATUS:CONFIRMED
UID:7014-1567468800-1567555199@peterbraden@peterbraden.co.uk
END:VEVENT
END:VCALENDAR
`);
// log the ids of these events
console.log(Object.keys(directEvents));

... will only parse "Do " from the description because node-ical interprets the newline as the start of a new field

wodka commented 1 year ago

Based on how this looks -> you should have escaped the \n - else it will be passed as an actual newline putting "something in NY." into the next line

mrkmhny commented 1 year ago

Based on how this looks -> you should have escaped the \n - else it will be passed as an actual newline putting "something in NY." into the next line

The \n in this case is coming from the Description text box inside of confluence. There isn't any opportunity to escape the newline because it's just a simple plain text box in the UI. Image below

Screenshot 2023-06-12 at 10 55 51 AM

wodka commented 1 year ago

if the ”\n" is part of the ical then it is fine, I'm just referring to the JS you posted.

image

-> the \n is within your string passed as an actual new line to parseICS -> therefor only showing up as " Do ".

whereas when you escape the \n with \n it will also extract the correct " Do \nsomething in NY." image

mrkmhny commented 1 year ago

if the ”\n" is part of the ical then it is fine, I'm just referring to the JS you posted.

image

-> the \n is within your string passed as an actual new line to parseICS -> therefor only showing up as " Do ".

whereas when you escape the \n with \n it will also extract the correct " Do \nsomething in NY." image

Sorry yeah, I hard coded the ical data for the sake of posting a clear, concise, and reproducible example. In reality this data would be coming from an API call to an external service. So in practice, escaping the \n chars would not be an option.

I've tried hacking together a simple function that could parse the DESCRIPTION field and find/replace all the \n characters, but there wasn't really a reliable/safe way to do this, since it's difficult to differentiate the \n chars that are part of the DESCRIPTION plain text vs the actual \n characters that indicate the next field in the ical data. Not to mention, any other appearance of the string "DESCRIPTION:" in the ical data would also throw off this parsing

wodka commented 1 year ago

Can you perhaps just post the lines of the description where you see the actual failure? (and replace all the non \n text with something else?)

with PRODID:Microsoft Exchange Server 2010 we had a problem recently with something along the following:

DESCRIPTION;LANGUAGE=en-US:
\nand here we do have some text
  and here the next line

does that look like your error? (this is invalid syntax for ical -> meaning not parsing it is correct in this case, we work around with appending a space for every \n that is in the beginning of a line)