From b6978bad9eb0ec03494987f85aa43fd6a9baa34b Mon Sep 17 00:00:00 2001 From: Alvie Rahman Date: Fri, 20 Aug 2021 14:31:34 +0100 Subject: [PATCH] Allow for searching in headers --- notes2web.py | 16 +++++++++++++--- search.js | 37 ++++++++++++++++++++++++++++++++++++- styles.css | 4 ++++ 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/notes2web.py b/notes2web.py index d16db82..625369a 100755 --- a/notes2web.py +++ b/notes2web.py @@ -136,11 +136,19 @@ def main(args): 'path': str(pathlib.Path(*pathlib.Path(output_filename).parts[1:])), 'title': fm.get('title') } ] + with open(filename) as fp: + lines = fp.read().split('\n') + header_lines = [] + for line in lines: + if re.match('^#{1,6} \S', line): + header_lines.append(" ".join(line.split(" ")[1:])) + print(f"{output_filename=}") all_entries.append({ 'path': str(pathlib.Path(*pathlib.Path(output_filename).parts[1:])), 'title': fm.get('title'), - 'tags': fm.get('tags') + 'tags': fm.get('tags'), + 'headers': header_lines }) if update_required(filename, output_filename) or args.force: @@ -167,7 +175,8 @@ def main(args): all_entries.append({ 'path': str(pathlib.Path(*pathlib.Path(output_filename).parts[1:])), 'title': title, - 'tags': [] + 'tags': [], + 'headers': [] }) print(f"{other_files=}") @@ -177,7 +186,8 @@ def main(args): all_entries.append({ 'path': str(pathlib.Path(*pathlib.Path(output_filename).parts[1:])), 'title': str(pathlib.Path(*pathlib.Path(output_filename).parts[1:])), - 'tags': [] + 'tags': [], + 'headers': [] }) shutil.copyfile(filename, output_filename) diff --git a/search.js b/search.js index abb657d..21623d2 100644 --- a/search.js +++ b/search.js @@ -1,5 +1,6 @@ const fuse = new Fuse(data, { - keys: ['path', 'title', 'tags'] + keys: ['path', 'title', 'tags', 'headers'], + includeMatches: true }) const searchBar = document.getElementById('search') @@ -10,6 +11,7 @@ function callback() { console.log(searchBar.value) results.innerHTML = '' fuse.search(searchBar.value).forEach(r => { + console.log(r) wrapper = document.createElement('div') wrapper.className = "article" @@ -23,12 +25,45 @@ function callback() { } extra_info.innerHTML += ' path: ' + r.item.path + header_matches_p = document.createElement('p') + header_matches_p.className = "smallText" + header_matches = [] + + r.matches.every(match => { + if (header_matches.length > 3) { + header_matches.push('...') + return false + } + + if (match.key === "headers") { + display_match = match.value + if (match.indices.length >= 1) { + match.indices.sort((a, b) => (b[1]-b[0])-(a[1]-a[0])) + indexPair = match.indices[0] + matching_slice = match.value.slice(indexPair[0], indexPair[1]+1) + console.log(matching_slice) + console.log(display_match) + display_match = display_match.replace( + matching_slice, + '' + matching_slice + '' + ) + } + header_matches.push(display_match) + } + return true + }) + + header_matches_p.innerHTML += "header_matches: [" + header_matches.join(', ') + ']' + content = document.createElement('a') content.innerHTML = r.item.title content.href = r.item.path wrapper.appendChild(content) wrapper.appendChild(extra_info) + if (header_matches.length > 0) { + wrapper.appendChild(header_matches_p) + } results.appendChild(wrapper) }) diff --git a/styles.css b/styles.css index 34f51e2..7afae1c 100644 --- a/styles.css +++ b/styles.css @@ -91,3 +91,7 @@ blockquote * { .article .smallText { margin: 0 } + +.matchHighlight { + background-color: #86c1b9; +}