Allow for searching in headers

This commit is contained in:
Akbar Rahman 2021-08-20 14:31:34 +01:00
parent f9ffd01d7c
commit b6978bad9e
3 changed files with 53 additions and 4 deletions

View File

@ -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)

View File

@ -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,
'<span class="matchHighlight">' + matching_slice + '</span>'
)
}
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)
})

View File

@ -91,3 +91,7 @@ blockquote * {
.article .smallText {
margin: 0
}
.matchHighlight {
background-color: #86c1b9;
}