The rendered Markdown preview had no way to search: the Electron desktop shell implements no find-in-page at all, and CodeMirror's search panel only exists in edit mode, so Ctrl/Cmd+F in the preview was a dead shortcut (web browsers happen to find plain-DOM text natively, but desktop does not). Adds a compact find bar for the rendered preview (Ctrl/Cmd+F or the search button): case-insensitive match highlighting with a live count, Enter / Shift+Enter and arrow buttons to navigate matches, Esc to close. Matches are wrapped in <mark> elements and re-applied via MutationObserver when the markdown renderer re-morphs the container (theme/content changes); svg (mermaid) and script/style text is skipped. The pure match-range logic is unit-tested. Fixes #2401
42 lines
1.3 KiB
TypeScript
42 lines
1.3 KiB
TypeScript
import { describe, expect, test } from 'bun:test';
|
|
|
|
import { findMatchRanges } from './markdownPreviewFind';
|
|
|
|
describe('findMatchRanges', () => {
|
|
test('returns no ranges for an empty or whitespace-only query', () => {
|
|
expect(findMatchRanges('hello world', '')).toEqual([]);
|
|
expect(findMatchRanges('hello world', ' ')).toEqual([]);
|
|
});
|
|
|
|
test('returns no ranges when the query does not occur', () => {
|
|
expect(findMatchRanges('hello world', 'nope')).toEqual([]);
|
|
});
|
|
|
|
test('finds all non-overlapping occurrences', () => {
|
|
expect(findMatchRanges('the quick brown fox jumps over the lazy dog', 'the')).toEqual([
|
|
{ start: 0, end: 3 },
|
|
{ start: 31, end: 34 },
|
|
]);
|
|
});
|
|
|
|
test('matches case-insensitively', () => {
|
|
expect(findMatchRanges('Hello HELLO hello', 'hello')).toEqual([
|
|
{ start: 0, end: 5 },
|
|
{ start: 6, end: 11 },
|
|
{ start: 12, end: 17 },
|
|
]);
|
|
});
|
|
|
|
test('scans non-overlapping matches like standard find-in-page', () => {
|
|
expect(findMatchRanges('aaaa', 'aaa')).toEqual([{ start: 0, end: 3 }]);
|
|
});
|
|
|
|
test('trims the query before matching', () => {
|
|
expect(findMatchRanges('alpha beta', ' beta ')).toEqual([{ start: 6, end: 10 }]);
|
|
});
|
|
|
|
test('handles a query longer than the text', () => {
|
|
expect(findMatchRanges('abc', 'abcdef')).toEqual([]);
|
|
});
|
|
});
|