diff --git a/js/fallback-renderer.js b/js/fallback-renderer.js index 25e7f64..b3995d6 100644 --- a/js/fallback-renderer.js +++ b/js/fallback-renderer.js @@ -25,7 +25,7 @@ class FallbackRenderer { static processHeaders(html) { return html.replace(/^#{1,6}\s+(.*)$/gim, function(match, content) { - var level = match.match(/^#+/)[0].length; + const level = match.match(/^#+/)[0].length; return '' + content + ''; }); } diff --git a/js/history-manager.js b/js/history-manager.js index 55422f5..443e400 100644 --- a/js/history-manager.js +++ b/js/history-manager.js @@ -10,9 +10,9 @@ class HistoryManager { let updatedHistory; if (existingIndex !== -1) { - var existingItem = history[existingIndex]; + const existingItem = history[existingIndex]; updatedHistory = []; - for (var i = 0; i < history.length; i++) { + for (let i = 0; i < history.length; i++) { if (i !== existingIndex) updatedHistory.push(history[i]); } updatedHistory.unshift({ @@ -24,7 +24,7 @@ class HistoryManager { }); } else { updatedHistory = []; - for (var j = 0; j < history.length; j++) { + for (let j = 0; j < history.length; j++) { if (history[j].id !== section.id) updatedHistory.push(history[j]); } @@ -68,12 +68,12 @@ class HistoryManager { } generateUniqueTitle(originalTitle, content, existingHistory) { - var hasConflict = false; - for (var k = 0; k < existingHistory.length; k++) { + let hasConflict = false; + for (let k = 0; k < existingHistory.length; k++) { if (existingHistory[k].title === originalTitle) { hasConflict = true; break; } } if (!hasConflict) return originalTitle; - var suffix = this.generateContentBasedSuffix(content, existingHistory.filter(function(item) { + const suffix = this.generateContentBasedSuffix(content, existingHistory.filter(function(item) { return item.title === originalTitle || item.title.indexOf(originalTitle + ' (') === 0; })); return originalTitle + ' (' + suffix + ')'; @@ -81,13 +81,13 @@ class HistoryManager { resolveTitleConflicts(history) { if (history.length < 2) return history; - var updatedHistory = history.slice(); - var titleGroups = {}; - var hasMultiples = false; + const updatedHistory = history.slice(); + const titleGroups = {}; + let hasMultiples = false; - for (var m = 0; m < updatedHistory.length; m++) { - var item = updatedHistory[m]; - var baseTitle = this.extractBaseTitle(item.title); + for (let m = 0; m < updatedHistory.length; m++) { + const item = updatedHistory[m]; + const baseTitle = this.extractBaseTitle(item.title); if (!titleGroups[baseTitle]) { titleGroups[baseTitle] = []; } @@ -97,20 +97,20 @@ class HistoryManager { if (!hasMultiples) return updatedHistory; - var groups = Object.values(titleGroups); - for (var g = 0; g < groups.length; g++) { - var group = groups[g]; + const groups = Object.values(titleGroups); + for (let g = 0; g < groups.length; g++) { + const group = groups[g]; if (group.length < 2) continue; group.sort(function(a, b) { return new Date(b.item.viewedAt) - new Date(a.item.viewedAt); }); - for (var t = 0; t < group.length; t++) { - var entry = group[t]; + for (let t = 0; t < group.length; t++) { + const entry = group[t]; if (t === 0) { updatedHistory[entry.index].title = this.extractBaseTitle(entry.item.title); } else { - var otherItems = []; - for (var o = 0; o < t; o++) otherItems.push(group[o].item); - var suffix = this.generateContentBasedSuffix(entry.item.content, otherItems); + const otherItems = []; + for (let o = 0; o < t; o++) otherItems.push(group[o].item); + const suffix = this.generateContentBasedSuffix(entry.item.content, otherItems); updatedHistory[entry.index].title = this.extractBaseTitle(entry.item.title) + ' (' + suffix + ')'; } } @@ -120,7 +120,7 @@ class HistoryManager { } extractBaseTitle(title) { - var match = title.match(/^(.+?)\s*\(/); + const match = title.match(/^(.+?)\s*\(/); return match ? match[1].trim() : title; } @@ -128,7 +128,7 @@ class HistoryManager { if (!conflictingItems || conflictingItems.length === 0) { return content ? content.trim().substring(0, 25) : 'content'; } - var result = this.findDistinctiveHeader(content, conflictingItems); + let result = this.findDistinctiveHeader(content, conflictingItems); if (result) return this.truncateText(result, 25); result = this.findDistinctiveLine(content, conflictingItems); if (result) return this.truncateText(result, 25); @@ -136,46 +136,54 @@ class HistoryManager { } findDistinctiveHeader(content, conflictingItems) { - var headers = this.extractHeaders(content); + const headers = this.extractHeaders(content); if (headers.length <= 1) return null; - for (var h = 0; h < headers.length; h++) { - var header = headers[h]; - var distinct = true; - for (var c = 0; c < conflictingItems.length; c++) { - if (this.extractHeaders(conflictingItems[c].content).indexOf(header) !== -1) { - distinct = false; - break; - } + + const conflictingHeaders = new Set(); + for (let c = 0; c < conflictingItems.length; c++) { + const extHeaders = this.extractHeaders(conflictingItems[c].content); + for (let i = 0; i < extHeaders.length; i++) { + conflictingHeaders.add(extHeaders[i]); + } + } + + for (let h = 0; h < headers.length; h++) { + const header = headers[h]; + if (!conflictingHeaders.has(header)) { + return header; } - if (distinct) return header; } return null; } findDistinctiveLine(content, conflictingItems) { - var contentLines = this.extractContentLines(content); - for (var cl = 0; cl < contentLines.length; cl++) { - var line = contentLines[cl]; - var distinct = true; - for (var ci = 0; ci < conflictingItems.length; ci++) { - if (this.extractContentLines(conflictingItems[ci].content).indexOf(line) !== -1) { - distinct = false; - break; - } + const contentLines = this.extractContentLines(content); + + const conflictingLines = new Set(); + for (let ci = 0; ci < conflictingItems.length; ci++) { + const extLines = this.extractContentLines(conflictingItems[ci].content); + for (let i = 0; i < extLines.length; i++) { + conflictingLines.add(extLines[i]); + } + } + + for (let cl = 0; cl < contentLines.length; cl++) { + const line = contentLines[cl]; + if (!conflictingLines.has(line)) { + return line; } - if (distinct) return line; } return null; } extractHeaders(content) { if (!content) return []; - var headers = []; - var lines = content.split('\n'); - for (var i = 0; i < lines.length; i++) { - var trimmed = lines[i].trim(); + const headers = []; + const lines = content.split('\n'); + for (let i = 0; i < lines.length; i++) { + const trimmed = lines[i].trim(); if (trimmed.charAt(0) === '#') { - var match = trimmed.match(/^#{1,6}\s+(.+)$/); + const match = trimmed.match(/^#{1,6}\s+(.+)$/); if (match) headers.push(match[1].trim()); } } @@ -184,10 +192,10 @@ class HistoryManager { extractContentLines(content) { if (!content) return []; - var lines = content.split('\n'); - var contentLines = []; - for (var i = 0; i < lines.length; i++) { - var trimmed = lines[i].trim(); + const lines = content.split('\n'); + const contentLines = []; + for (let i = 0; i < lines.length; i++) { + const trimmed = lines[i].trim(); if (trimmed && trimmed.charAt(0) !== '#' && trimmed.indexOf('```') !== 0 && trimmed.length > 5) { contentLines.push(trimmed); if (contentLines.length >= 3) break; diff --git a/js/rendered-page-builder.js b/js/rendered-page-builder.js index fbeed45..11dddfb 100644 --- a/js/rendered-page-builder.js +++ b/js/rendered-page-builder.js @@ -31,10 +31,10 @@ class RenderedPageBuilder { } static build(content, rawMarkdown, title, listItems, flatListItems) { - var katexCSS = CONFIG.CDN.katexCSS; - var katexJS = CONFIG.CDN.katexJS; - var katexAuto = CONFIG.CDN.katexAutoRenderJS; - var h2c = CONFIG.CDN.html2canvas; + const katexCSS = CONFIG.CDN.katexCSS; + const katexJS = CONFIG.CDN.katexJS; + const katexAuto = CONFIG.CDN.katexAutoRenderJS; + const h2c = CONFIG.CDN.html2canvas; return '\n\n\n \n \n ' + title + '\n \n' + this._getStyles() + '\n\n\n' + this._getControls() + '\n
' + content + '
\n \n
Copied to clipboard!
\n