Skip to content

Commit

Permalink
Merge pull request #11 from hadynz/no-index-warn
Browse files Browse the repository at this point in the history
Gracefully degrade and warn if bad index
  • Loading branch information
hadynz authored Feb 17, 2022
2 parents a3236aa + 019ab74 commit b6a2f9d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"id": "obsidian-sidekick",
"name": "Sidekick",
"description": "A companion to identify hidden connections that match your tags and pages",
"version": "1.1.0",
"version": "1.1.1",
"minAppVersion": "0.13.8",
"author": "Hady Osman",
"authorUrl": "https://hady.geek.nz",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-sidekick",
"version": "1.1.0",
"version": "1.1.1",
"description": "A companion to identify hidden connections that match your tags and pages",
"main": "src/index.ts",
"repository": {
Expand Down
18 changes: 16 additions & 2 deletions src/search/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ export default class Search {
const indexHits = results[0].matchData.metadata;

return Object.keys(indexHits)
.reduce((acc: SearchResult[], indexHit: string) => {
const positions = indexHits[indexHit][DocumentKey].position;
.filter((indexHit) => this.existsInIndex(indexHit, indices))
.reduce((acc: SearchResult[], indexHit) => {
const positions: number[][] = indexHits[indexHit][DocumentKey].position;

const searchResults = positions.map(
(position): SearchResult => ({
Expand All @@ -62,6 +63,19 @@ export default class Search {
.sort((a, b) => a.start - b.start); // Must sort by start position to prepare for highlighting
}

private existsInIndex(index: string, indices: Index): boolean {
const exists = indices[index] != null;

if (!exists) {
console.warn(
`Search hit "${index}" was not found in Obsidian index. This could be a bug. Report on https://github.com/hadynz/obsidian-sidekick/issues`,
indices
);
}

return exists;
}

private redactText(text: string): string {
return text
.replace(/```[\s\S]+?```/g, (m) => ' '.repeat(m.length)) // remove code blocks
Expand Down

0 comments on commit b6a2f9d

Please sign in to comment.