2019-12-19 21:50:50 -08:00
|
|
|
|
|
|
|
let ProcessText = module.exports = {}
|
|
|
|
|
|
|
|
ProcessText.removeHtml = (string) => {
|
2020-01-02 17:26:55 -08:00
|
|
|
|
|
|
|
if(string == undefined || string == null || string.length == 0){
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
|
2019-12-19 21:50:50 -08:00
|
|
|
return string
|
2020-02-01 14:21:22 -08:00
|
|
|
.replace(/&[[#A-Za-z0-9]+A-Za-z0-9]+;/g,' ') //Rip out all HTML entities
|
2019-12-19 21:50:50 -08:00
|
|
|
.replace(/<[^>]+>/g, ' ') //Rip out all HTML tags
|
|
|
|
.replace(/\s+/g, ' ') //Remove all whitespace
|
2020-01-02 17:26:55 -08:00
|
|
|
.trim()
|
|
|
|
}
|
|
|
|
|
2020-02-26 23:14:29 -08:00
|
|
|
|
|
|
|
//Remove Empty HTML lines from a string
|
|
|
|
ProcessText.stripBlankHtmlLines = (string) => {
|
|
|
|
|
|
|
|
if(string == undefined || string == null || string.length == 0){
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
|
|
|
|
//Blank lines look like this -> <p><br></p>
|
|
|
|
return string.replace(/\<p\>\<br\>\<\/p\>/g,'')
|
|
|
|
}
|
|
|
|
|
2020-03-25 21:45:23 -07:00
|
|
|
//Remove Double Empty HTML lines from a string
|
|
|
|
ProcessText.stripDoubleBlankLines = (string) => {
|
|
|
|
|
|
|
|
if(string == undefined || string == null || string.length == 0){
|
|
|
|
return ''
|
|
|
|
}
|
|
|
|
|
|
|
|
//Blank lines look like this -> <p><br></p>
|
|
|
|
return string.replace(/\<p\>\<br\>\<\/p\>\<p\>\<br\>\<\/p\>/g,'')
|
|
|
|
}
|
|
|
|
|
2020-01-02 17:26:55 -08:00
|
|
|
ProcessText.getUrlsFromString = (string) => {
|
|
|
|
const urlPattern = /(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#/%=~_|$?!:,.]*\)|[A-Z0-9+&@#/%=~_|$])/igm
|
|
|
|
return string.match(urlPattern)
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Pulls out title and subtext of note
|
|
|
|
+ Title is always first line
|
|
|
|
+ Empty lines are skipped
|
|
|
|
+ URLs are turned into links
|
|
|
|
+ All URLs are givent the target="_blank" property
|
2020-02-01 14:21:22 -08:00
|
|
|
+ Lists are given extra display characters
|
|
|
|
+ If note starts as a list, skip the title
|
2020-01-02 17:26:55 -08:00
|
|
|
*/
|
|
|
|
|
2020-03-25 21:45:23 -07:00
|
|
|
ProcessText.deduceNoteTitle = (inTitle, inString) => {
|
2020-01-02 17:26:55 -08:00
|
|
|
|
2020-03-25 21:45:23 -07:00
|
|
|
let title = inTitle //Title of note
|
2020-01-02 17:26:55 -08:00
|
|
|
let sub = '' //sub text below note
|
|
|
|
|
2020-03-25 21:45:23 -07:00
|
|
|
//Always return a title as a String
|
|
|
|
if(title == null){
|
|
|
|
title = ''
|
|
|
|
}
|
|
|
|
|
2020-01-02 17:26:55 -08:00
|
|
|
if(!inString || inString == null || inString.length == 0){
|
|
|
|
return {title, sub}
|
|
|
|
}
|
|
|
|
|
2020-02-01 14:21:22 -08:00
|
|
|
//Remove inline styles that may be added by editor
|
2020-04-14 23:28:58 -07:00
|
|
|
// inString = inString.replace(/style=".*?"/g,'')
|
2020-02-26 23:14:29 -08:00
|
|
|
|
|
|
|
const tagFreeLength = ProcessText.removeHtml(inString).length
|
|
|
|
|
2020-03-25 21:45:23 -07:00
|
|
|
//
|
|
|
|
// Simplified attempt!
|
|
|
|
// Remove tags, push caret if greater than 200 chars...thats it
|
|
|
|
// Still needs, links to open in a new window.
|
2020-02-26 23:14:29 -08:00
|
|
|
|
2020-03-25 21:45:23 -07:00
|
|
|
sub = ProcessText.stripDoubleBlankLines(inString)
|
2020-04-14 23:28:58 -07:00
|
|
|
// if(tagFreeLength > 200){
|
|
|
|
// sub += '... <i class="green caret down icon"></i>'
|
|
|
|
// }
|
|
|
|
inString += '</end>'
|
2020-02-22 22:27:49 -08:00
|
|
|
|
2020-03-25 21:45:23 -07:00
|
|
|
return {title, sub}
|
|
|
|
|
2020-02-22 22:27:49 -08:00
|
|
|
//Emergency ending tag if truncated. This will help regex find all the lines
|
2020-04-14 23:28:58 -07:00
|
|
|
|
2020-02-01 14:21:22 -08:00
|
|
|
|
|
|
|
//Match full line and closing tag or just closing tag
|
2020-02-22 22:27:49 -08:00
|
|
|
let lines = inString.match(/[<[a-zA-Z0-9]+>(.*?)<\/[a-zA-Z0-9]+>|<\/[a-zA-Z0-9>]+?>/gms)
|
2020-02-01 14:21:22 -08:00
|
|
|
if(lines == null){ lines = [inString] }
|
|
|
|
//.match(/[^\r\n]+/g) //Match return or newline
|
|
|
|
|
2020-02-22 22:27:49 -08:00
|
|
|
// console.log('----------------')
|
2020-02-01 14:21:22 -08:00
|
|
|
// console.log(lines)
|
2020-02-22 22:27:49 -08:00
|
|
|
// console.log('----------------')
|
2020-02-01 14:21:22 -08:00
|
|
|
|
2020-01-02 17:26:55 -08:00
|
|
|
let finalLines = []
|
|
|
|
|
|
|
|
const startTags = ['<ol','<li','<ul']
|
|
|
|
const endTags = ['</o','</l','</u']
|
|
|
|
|
|
|
|
let totalLines = Math.min(lines.length, 6)
|
2020-02-26 23:14:29 -08:00
|
|
|
let charLimit = 400
|
2020-01-02 17:26:55 -08:00
|
|
|
let listStart = false
|
2020-02-01 14:21:22 -08:00
|
|
|
let noTitleJustList = false
|
2020-03-25 21:45:23 -07:00
|
|
|
let appendCaret = false
|
2020-01-02 17:26:55 -08:00
|
|
|
|
|
|
|
for(let i=0; i < totalLines; i++){
|
|
|
|
|
|
|
|
//Just in case 'i' gets bigger than array
|
|
|
|
if(lines[i] === undefined){
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-02-01 14:21:22 -08:00
|
|
|
//Various empty chars are possible
|
|
|
|
const cleanLine = ProcessText.removeHtml(lines[i])
|
|
|
|
.replace('<br>','')
|
|
|
|
.trim()
|
2020-01-02 17:26:55 -08:00
|
|
|
const lineStart = lines[i].trim().substring(0, 3)
|
|
|
|
charLimit -= cleanLine.length
|
|
|
|
|
|
|
|
//Close out list if char limit is hit
|
|
|
|
if(charLimit <= 0 && listStart){
|
|
|
|
finalLines.push(lines[i])
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
//Images appear as empty, push em!
|
|
|
|
if(cleanLine.length == 0 && lines[i].indexOf('<img') != -1){
|
|
|
|
finalLines.push(lines[i])
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-02-01 14:21:22 -08:00
|
|
|
//Check if note starts with a list, don't include title, just show list
|
|
|
|
if(finalLines.length == 0 && startTags.includes(lineStart)){
|
|
|
|
noTitleJustList = true
|
|
|
|
}
|
|
|
|
|
2020-01-02 17:26:55 -08:00
|
|
|
//Empty line, may be a list open or close
|
|
|
|
if(cleanLine.length == 0 && (startTags.includes(lineStart) || endTags.includes(lineStart) )){
|
|
|
|
if(listStart == false){
|
2020-02-18 14:52:12 -08:00
|
|
|
//charLimit = 400 //Double size for list notes
|
2020-01-02 17:26:55 -08:00
|
|
|
}
|
|
|
|
finalLines.push(lines[i])
|
|
|
|
totalLines++
|
|
|
|
listStart = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
//If line is part of a list, up counter, we want the whole list
|
|
|
|
if(startTags.includes(lineStart)){
|
|
|
|
totalLines++
|
|
|
|
}
|
|
|
|
|
|
|
|
//Skip empty lines
|
2020-02-01 14:21:22 -08:00
|
|
|
if(!cleanLine || cleanLine.length == 0){
|
2020-01-02 17:26:55 -08:00
|
|
|
totalLines++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
//turn urls into links, don't process if its already an <a href=
|
|
|
|
const containsUrls = ProcessText.getUrlsFromString(cleanLine)
|
|
|
|
if(containsUrls && containsUrls.length == 1 && lines[i].indexOf('</a>') == -1){
|
|
|
|
const url = containsUrls[0]
|
|
|
|
lines[i] = lines[i].replace(url, `<a href="${url}">${url}</a>`)
|
|
|
|
}
|
|
|
|
|
|
|
|
//Insert target=_blank into links if set, do it for every link in line
|
|
|
|
if(lines[i].indexOf('</a>') > 0){
|
|
|
|
lines[i] = lines[i].replace(/<a /g, '<a target="_blank" ')
|
|
|
|
}
|
|
|
|
|
|
|
|
//Limit output characters
|
|
|
|
//Check character limit
|
|
|
|
if(charLimit <= 0 && listStart == false){
|
|
|
|
|
|
|
|
//Cut the string down to character limit
|
|
|
|
const cutString = lines[i].substring(0, lines[i].length+charLimit)
|
2020-02-22 22:27:49 -08:00
|
|
|
|
2020-01-02 17:26:55 -08:00
|
|
|
//Find last space and cut off everything after it
|
|
|
|
let cleanCutString = cutString.substring(0, cutString.lastIndexOf(' '))
|
|
|
|
|
|
|
|
//Some strings may not contain a space resulting in no string
|
|
|
|
if(cleanCutString.length == 0){
|
|
|
|
cleanCutString = cutString
|
|
|
|
}
|
2020-03-25 21:45:23 -07:00
|
|
|
appendCaret = true
|
|
|
|
|
2020-01-02 17:26:55 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
finalLines.push(lines[i])
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-03-25 21:45:23 -07:00
|
|
|
if(tagFreeLength.length >= 300 || appendCaret){
|
|
|
|
finalLines.push('... <i class="green caret down icon"></i>')
|
|
|
|
}
|
|
|
|
|
2020-01-02 17:26:55 -08:00
|
|
|
//Pull out title if its not an empty string
|
2020-03-25 21:45:23 -07:00
|
|
|
if(!noTitleJustList && title == ''){
|
|
|
|
title = ProcessText.removeHtml( finalLines.shift() ).replace(' ','')
|
2020-01-02 17:26:55 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
sub = finalLines.join('')
|
|
|
|
|
|
|
|
//Return final display lengths
|
|
|
|
let titleLength = ProcessText.removeHtml(title).trim().replace(' ','').length
|
|
|
|
let subtextLength = ProcessText.removeHtml(sub).trim().replace(' ','').length
|
|
|
|
|
|
|
|
|
|
|
|
return { title, sub, titleLength, subtextLength }
|
2019-12-19 21:50:50 -08:00
|
|
|
}
|