Skip to content

Commit

Permalink
Merge pull request #26 from hadynz/hierarchy-tags
Browse files Browse the repository at this point in the history
Hierarchical tags are never highlighted
  • Loading branch information
hadynz authored Feb 24, 2022
2 parents 78776ca + 0b487dd commit 3f01078
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 11 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.4.1",
"version": "1.4.2",
"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.4.1",
"version": "1.4.2",
"description": "A companion to identify hidden connections that match your tags and pages",
"main": "src/index.ts",
"repository": {
Expand Down
11 changes: 2 additions & 9 deletions src/search/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import _ from 'lodash';
import { Trie, Emit } from '@tanishiking/aho-corasick';

import { redactText } from './search.utils';
import type { Indexer } from '../indexing/indexer';

type SearchResult = {
Expand Down Expand Up @@ -33,7 +34,7 @@ export default class Search {
}

public find(text: string): SearchResult[] {
const redactedText = this.redactText(text); // Redact text that we don't want to be searched
const redactedText = redactText(text); // Redact text that we don't want to be searched

const results = this.trie.parseText(redactedText);

Expand Down Expand Up @@ -63,12 +64,4 @@ export default class Search {

return exists;
}

private redactText(text: string): string {
return text
.replace(/```[\s\S]+?```/g, (m) => ' '.repeat(m.length)) // remove code blocks
.replace(/^\n*?---[\s\S]+?---/g, (m) => ' '.repeat(m.length)) // remove yaml front matter
.replace(/#+([a-zA-Z0-9_]+)/g, (m) => ' '.repeat(m.length)) // remove hashtags
.replace(/\[(.*?)\]+/g, (m) => ' '.repeat(m.length)); // remove links
}
}
81 changes: 81 additions & 0 deletions src/search/search.utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { redactText } from './search.utils';

describe('redactText', () => {
it('Hashtags are redacted', () => {
const sentence = 'I love playing #football';
const expected = 'I love playing ';

const actual = redactText(sentence);

expect(actual).toEqual(expected);
});

it('Hierarchial hashtags are redacted', () => {
const sentence = 'I love playing #sport/football';
const expected = 'I love playing ';

const actual = redactText(sentence);

expect(actual).toEqual(expected);
});

it('Links are redacted', () => {
const sentence = 'I love [[sleeping]] and [[https://aoe.com|gaming]]';
const expected = 'I love and ';

const actual = redactText(sentence);

expect(actual).toEqual(expected);
});

it('Code blocks are redacted', () => {
const sentence = '```cs\
code block\
```';
const expected = ' \
\
';

const actual = redactText(sentence);

expect(actual).toEqual(expected);
});

it('Frontmatter is redacted', () => {
const sentence = '---\
tags: [aoe, aoe2]\
---\
# Heading 1\
```';
const expected = ' \
\
\
# Heading 1\
```';

const actual = redactText(sentence);

expect(actual).toEqual(expected);
});

it('Frontmatter with preceding empty lines is redacted', () => {
const sentence = '\
\
---\
tags: [aoe, aoe2]\
---\
# Heading 1\
```';
const expected = '\
\
\
\
\
# Heading 1\
```';

const actual = redactText(sentence);

expect(actual).toEqual(expected);
});
});
7 changes: 7 additions & 0 deletions src/search/search.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const redactText = (text: string): string => {
return text
.replace(/```[\s\S]+?```/g, (m) => ' '.repeat(m.length)) // remove code blocks
.replace(/^\n*?---[\s\S]+?---/g, (m) => ' '.repeat(m.length)) // remove yaml front matter
.replace(/#+([a-zA-Z0-9_/]+)/g, (m) => ' '.repeat(m.length)) // remove hashtags
.replace(/\[(.*?)\]+/g, (m) => ' '.repeat(m.length)); // remove links
};

0 comments on commit 3f01078

Please sign in to comment.