| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  | function html2notecase(contents, note) { | 
					
						
							| 
									
										
										
										
											2017-08-23 20:35:47 -04:00
										 |  |  |     // remove any possible extra newlines which might be inserted - all relevant new lines should be only in <br> and <p>
 | 
					
						
							|  |  |  |     contents = contents.replace(/(?:\r\n|\r|\n)/, ''); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 18:55:09 -04:00
										 |  |  |     // some editors insert this font stuff which messes up parsing by doubling newlines
 | 
					
						
							|  |  |  |     contents = contents.replace(/<font[^>]*>/g, ''); | 
					
						
							|  |  |  |     contents = contents.replace(/<\/font>/g, ''); | 
					
						
							| 
									
										
										
										
											2017-08-25 20:40:33 -04:00
										 |  |  |     contents = contents.replace(/<br><\/p>/g, '\n'); | 
					
						
							|  |  |  |     contents = contents.replace(/<p><br><\/p>/g, '\n'); | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |     contents = contents.replace(/<br \/>/g, '\n'); | 
					
						
							|  |  |  |     contents = contents.replace(/<br>/g, '\n'); | 
					
						
							|  |  |  |     contents = contents.replace(/<\/p>/g, '\n'); | 
					
						
							|  |  |  |     contents = contents.replace(/<p>/g, ''); | 
					
						
							| 
									
										
										
										
											2017-08-13 21:42:10 -04:00
										 |  |  |     contents = contents.replace(/ /g, ' '); | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     let index = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     note.formatting = []; | 
					
						
							|  |  |  |     note.links = []; | 
					
						
							|  |  |  |     note.images = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     while (index < contents.length) { | 
					
						
							| 
									
										
										
										
											2017-08-21 21:31:23 -04:00
										 |  |  |         let curContent = contents.substr(index); | 
					
						
							| 
									
										
										
										
											2017-08-13 22:35:04 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-21 21:31:23 -04:00
										 |  |  |         if (contents[index] === '<') { | 
					
						
							|  |  |  |             let found = false; | 
					
						
							| 
									
										
										
										
											2017-08-29 22:25:58 -04:00
										 |  |  |             const endOfTag = curContent.indexOf('>'); | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-21 21:31:23 -04:00
										 |  |  |             if (endOfTag === -1) { | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |                 console.log("Can't find the end of the tag"); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-29 22:25:58 -04:00
										 |  |  |             const curTag = curContent.substr(0, endOfTag + 1); | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |             for (tagId in tags) { | 
					
						
							| 
									
										
										
										
											2017-08-29 22:25:58 -04:00
										 |  |  |                 const tag = tags[tagId]; | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-21 21:31:23 -04:00
										 |  |  |                 if (contents.substr(index, tag.length) === tag) { | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |                     found = true; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     note.formatting.push({ | 
					
						
							|  |  |  |                         note_id: note.detail.note_id, | 
					
						
							|  |  |  |                         note_offset: index, | 
					
						
							|  |  |  |                         fmt_tag: tagId, | 
					
						
							|  |  |  |                         fmt_color: '', | 
					
						
							|  |  |  |                         fmt_font: '', | 
					
						
							|  |  |  |                         fmt_value: 100 | 
					
						
							|  |  |  |                     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     contents = contents.substr(0, index) + contents.substr(index + tag.length); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     break; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-21 21:31:23 -04:00
										 |  |  |             if (curTag.substr(0, 4) === "<img") { | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |                 //console.log("Found img tag");
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 let dataImagePos = curTag.indexOf('data:image/'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-21 21:31:23 -04:00
										 |  |  |                 if (dataImagePos !== -1) { | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |                     let imageType = curTag.substr(dataImagePos + 11, 3); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     //console.log("image type: " + imageType);
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     let dataStart = curTag.substr(dataImagePos + 22); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                     let endOfDataPos = dataStart.indexOf('"'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-21 21:31:23 -04:00
										 |  |  |                     if (endOfDataPos !== -1) { | 
					
						
							| 
									
										
										
										
											2017-08-29 22:25:58 -04:00
										 |  |  |                         const imageData = dataStart.substr(0, endOfDataPos); | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |                         note.images.push({ | 
					
						
							|  |  |  |                             note_id: note.detail.note_id, | 
					
						
							|  |  |  |                             note_offset: index, | 
					
						
							| 
									
										
										
										
											2017-08-21 21:31:23 -04:00
										 |  |  |                             is_png: imageType === "png", | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |                             image_data: imageData | 
					
						
							|  |  |  |                         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                         contents = contents.substr(0, index) + contents.substr(index + curTag.length); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                         found = true; | 
					
						
							|  |  |  |                     } | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-03 20:09:17 -04:00
										 |  |  |             const linkMatch = /^<a[^>]+?href="([^"]+?)"[^>]*?>([^<]+?)<\/a>/.exec(curContent); | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-29 22:25:58 -04:00
										 |  |  |             if (linkMatch !== null) { | 
					
						
							|  |  |  |                 const targetUrl = linkMatch[1]; | 
					
						
							|  |  |  |                 const linkText = linkMatch[2]; | 
					
						
							| 
									
										
										
										
											2017-08-27 14:39:26 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 const newLink = { | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |                     note_id: note.detail.note_id, | 
					
						
							|  |  |  |                     note_offset: index, | 
					
						
							| 
									
										
										
										
											2017-08-27 14:39:26 -04:00
										 |  |  |                     lnk_text: linkText | 
					
						
							|  |  |  |                 }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 const noteIdMatch = /app#([A-Za-z0-9]{22})/.exec(targetUrl); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (noteIdMatch !== null) { | 
					
						
							|  |  |  |                     newLink.target_note_id = noteIdMatch[1]; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 else { | 
					
						
							|  |  |  |                     newLink.target_url = targetUrl; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 note.links.push(newLink); | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-29 22:25:58 -04:00
										 |  |  |                 contents = contents.substr(0, index) + linkText + contents.substr(index + linkMatch[0].length); | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-04 18:51:07 -04:00
										 |  |  |                 // we'll skip the link text so that it's not processed twice (second time by link auto detection)
 | 
					
						
							|  |  |  |                 index += linkText.length; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |                 found = true; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-13 22:35:04 -04:00
										 |  |  |             if (!found) { | 
					
						
							|  |  |  |                 contents = contents.substr(0, index) + contents.substr(index + endOfTag + 1); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							| 
									
										
										
										
											2017-08-22 20:19:46 -04:00
										 |  |  |             let linkMatch = /^(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|]/i.exec(curContent); | 
					
						
							| 
									
										
										
										
											2017-08-21 21:31:23 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if (linkMatch !== null) { | 
					
						
							| 
									
										
										
										
											2017-08-29 22:25:58 -04:00
										 |  |  |                 const targetUrl = linkMatch[0]; | 
					
						
							| 
									
										
										
										
											2017-08-27 14:39:26 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-29 22:25:58 -04:00
										 |  |  |                 const newLink = { | 
					
						
							| 
									
										
										
										
											2017-08-21 21:31:23 -04:00
										 |  |  |                     note_id: note.detail.note_id, | 
					
						
							|  |  |  |                     note_offset: index, | 
					
						
							| 
									
										
										
										
											2017-08-27 14:39:26 -04:00
										 |  |  |                     lnk_text: targetUrl | 
					
						
							|  |  |  |                 }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 const noteIdMatch = /app#([A-Za-z0-9]{22})/.exec(targetUrl); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 if (noteIdMatch !== null) { | 
					
						
							|  |  |  |                     newLink.target_note_id = noteIdMatch[1]; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 else { | 
					
						
							|  |  |  |                     newLink.target_url = targetUrl; | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2017-08-21 21:31:23 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-27 14:39:26 -04:00
										 |  |  |                 note.links.push(newLink); | 
					
						
							| 
									
										
										
										
											2017-08-22 20:19:46 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-27 14:39:26 -04:00
										 |  |  |                 index += targetUrl.length; | 
					
						
							| 
									
										
										
										
											2017-08-21 21:31:23 -04:00
										 |  |  |             } | 
					
						
							|  |  |  |             else { | 
					
						
							|  |  |  |                 index++; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-25 20:40:33 -04:00
										 |  |  |     contents = contents.trim(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-11 16:04:07 -04:00
										 |  |  |     note.detail.note_text = contents; | 
					
						
							|  |  |  | } |