obsidian#SearchMatches TypeScript Examples

The following examples show how to use obsidian#SearchMatches. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: editorFilter.fixture.ts    From obsidian-switcher-plus with GNU General Public License v3.0 6 votes vote down vote up
function makeEditorFilter(
  filterText: string,
  displayText: string,
  matches: SearchMatches,
  score: number,
  metadata?: CachedMetadata,
): EditorFixtureFilter {
  const prepQuery = makePreparedQuery(filterText);
  const fuzzyMatch = makeFuzzyMatch(matches, score);
  const cachedMetadata = metadata ?? getCachedMetadata();

  return {
    inputText: `${editorTrigger}${filterText}`,
    displayText,
    prepQuery,
    fuzzyMatch,
    cachedMetadata,
  };
}
Example #2
Source File: fixtureUtils.ts    From obsidian-switcher-plus with GNU General Public License v3.0 6 votes vote down vote up
export function makeFuzzyMatch(
  matches: SearchMatches = [[0, 5]],
  score = -0.0115,
): SearchResult {
  return {
    matches,
    score,
  };
}
Example #3
Source File: headingsHandler.ts    From obsidian-switcher-plus with GNU General Public License v3.0 5 votes vote down vote up
private adjustMatchIndicesForPath(matches: SearchMatches, pathLen: number): void {
    matches?.forEach((match) => {
      match[0] += pathLen;
      match[1] += pathLen;
    });
  }
Example #4
Source File: modals.ts    From obsidian-citation-plugin with MIT License 5 votes vote down vote up
renderSuggestion(match: FuzzyMatch<Entry>, el: HTMLElement): void {
    el.empty();
    const entry = match.item;
    const entryTitle = entry.title || '';

    const container = el.createEl('div', { cls: 'zoteroResult' });
    const titleEl = container.createEl('span', {
      cls: 'zoteroTitle',
    });
    container.createEl('span', { cls: 'zoteroCitekey', text: entry.id });

    const authorsCls = entry.authorString
      ? 'zoteroAuthors'
      : 'zoteroAuthors zoteroAuthorsEmpty';
    const authorsEl = container.createEl('span', {
      cls: authorsCls,
    });

    // Prepare to highlight string matches for each part of the search item.
    // Compute offsets of each rendered element's content within the string
    // returned by `getItemText`.
    const allMatches = match.match.matches;
    const authorStringOffset = 1 + entryTitle.length;

    // Filter a match list to contain only the relevant matches for a given
    // substring, and with match indices shifted relative to the start of that
    // substring
    const shiftMatches = (
      matches: SearchMatches,
      start: number,
      end: number,
    ) => {
      return matches
        .map((match: SearchMatchPart) => {
          const [matchStart, matchEnd] = match;
          return [
            matchStart - start,
            Math.min(matchEnd - start, end),
          ] as SearchMatchPart;
        })
        .filter((match: SearchMatchPart) => {
          const [matchStart, matchEnd] = match;
          return matchStart >= 0;
        });
    };

    // Now highlight matched strings within each element
    renderMatches(
      titleEl,
      entryTitle,
      shiftMatches(allMatches, 0, entryTitle.length),
    );
    if (entry.authorString) {
      renderMatches(
        authorsEl,
        entry.authorString,
        shiftMatches(
          allMatches,
          authorStringOffset,
          authorStringOffset + entry.authorString.length,
        ),
      );
    }
  }