@marimo-team/frontend 0.25.3-dev85 → 0.25.3-dev86
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assets/{JsonOutput-3NheuQEt.js → JsonOutput-DryOywaN.js} +4 -4
- package/dist/assets/{agent-panel-BCCe7-0m.js → agent-panel-6t16-QG0.js} +1 -1
- package/dist/assets/{cell-editor-B6Wvf232.js → cell-editor-CM_kQ_VW.js} +1 -1
- package/dist/assets/{column-preview-BNPxoRnw.js → column-preview-CqFSXTtf.js} +1 -1
- package/dist/assets/{command-palette-CFLLUKbE.js → command-palette-CkDj29px.js} +1 -1
- package/dist/assets/{edit-page-Bh44QoBu.js → edit-page-Bgkov1Jy.js} +4 -4
- package/dist/assets/{file-explorer-panel-CAq2LZFc.js → file-explorer-panel-B9jBSwhj.js} +1 -1
- package/dist/assets/{hooks-BPw6SVXo.js → hooks-yjSoxKTw.js} +1 -1
- package/dist/assets/{index-D2O1B6ZY.js → index-0pnwTVch.js} +3 -3
- package/dist/assets/index-BnhSnwvB.css +2 -0
- package/dist/assets/{layout-NkCTvSyj.js → layout-B2MJFBFp.js} +3 -3
- package/dist/assets/{panels-CiR0Zzhg.js → panels-DI8gAi31.js} +1 -1
- package/dist/assets/{reveal-component-D2xhcDzw.js → reveal-component-BoGgZrdo.js} +1 -1
- package/dist/assets/{run-page-B9Jb5LMt.js → run-page-DnvqkxCy.js} +1 -1
- package/dist/assets/{scratchpad-panel-B_xrdEvJ.js → scratchpad-panel-DZDmNIPw.js} +1 -1
- package/dist/assets/{session-panel-DwuCGJU1.js → session-panel-B1QB5LN7.js} +1 -1
- package/dist/assets/{useNotebookActions-DNuGk3m2.js → useNotebookActions-DJuAUAGI.js} +1 -1
- package/dist/index.html +4 -4
- package/package.json +1 -1
- package/src/components/data-table/__tests__/columns.test.tsx +105 -0
- package/src/components/data-table/__tests__/search-highlight.test.tsx +109 -0
- package/src/components/data-table/__tests__/url-detector.test.tsx +74 -0
- package/src/components/data-table/columns.tsx +43 -45
- package/src/components/data-table/data-table.tsx +7 -1
- package/src/components/data-table/search-highlight.tsx +79 -0
- package/src/components/data-table/types.ts +1 -0
- package/src/components/data-table/url-detector.tsx +49 -9
- package/src/css/globals.css +2 -0
- package/dist/assets/index-BdsOMJac.css +0 -2
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import { fireEvent, render, screen } from "@testing-library/react";
|
|
3
3
|
import { describe, expect, it, vi } from "vitest";
|
|
4
4
|
import { parseContent } from "@/utils/url-parser";
|
|
5
|
+
import { buildHighlightPattern } from "../search-highlight";
|
|
5
6
|
import { isMarkdown, UrlDetector } from "../url-detector";
|
|
6
7
|
|
|
7
8
|
describe("UrlDetector", () => {
|
|
@@ -201,3 +202,76 @@ describe("isMarkdown", () => {
|
|
|
201
202
|
expect(isMarkdown("Visit [marimo](https://marimo.io)")).toBe(true);
|
|
202
203
|
});
|
|
203
204
|
});
|
|
205
|
+
|
|
206
|
+
describe("UrlDetector with highlightPattern", () => {
|
|
207
|
+
it("wraps a text match in a mark", () => {
|
|
208
|
+
const { container } = render(
|
|
209
|
+
<UrlDetector
|
|
210
|
+
parts={parseContent("Alice Smith")}
|
|
211
|
+
highlightPattern={buildHighlightPattern("smi")}
|
|
212
|
+
/>,
|
|
213
|
+
);
|
|
214
|
+
const marks = container.querySelectorAll("mark");
|
|
215
|
+
expect(marks).toHaveLength(1);
|
|
216
|
+
expect(marks[0].textContent).toBe("Smi");
|
|
217
|
+
expect(container.textContent).toBe("Alice Smith");
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it("wraps a match inside a link label", () => {
|
|
221
|
+
const { container } = render(
|
|
222
|
+
<UrlDetector
|
|
223
|
+
parts={parseContent("see https://marimo.io/docs")}
|
|
224
|
+
highlightPattern={buildHighlightPattern("marimo")}
|
|
225
|
+
/>,
|
|
226
|
+
);
|
|
227
|
+
const link = container.querySelector("a");
|
|
228
|
+
expect(link).not.toBeNull();
|
|
229
|
+
const mark = link?.querySelector("mark");
|
|
230
|
+
expect(mark?.textContent).toBe("marimo");
|
|
231
|
+
expect(link).toHaveAttribute("href", "https://marimo.io/docs");
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it("renders no mark for an empty query", () => {
|
|
235
|
+
const { container } = render(
|
|
236
|
+
<UrlDetector
|
|
237
|
+
parts={parseContent("Alice Smith")}
|
|
238
|
+
highlightPattern={buildHighlightPattern("")}
|
|
239
|
+
/>,
|
|
240
|
+
);
|
|
241
|
+
expect(container.querySelector("mark")).toBeNull();
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
describe("UrlDetector highlight across part boundaries", () => {
|
|
246
|
+
it("marks a query that spans a text part and a link", () => {
|
|
247
|
+
const { container } = render(
|
|
248
|
+
<UrlDetector
|
|
249
|
+
parts={parseContent("see https://marimo.io/docs")}
|
|
250
|
+
highlightPattern={buildHighlightPattern("see https")}
|
|
251
|
+
/>,
|
|
252
|
+
);
|
|
253
|
+
const marks = [...container.querySelectorAll("mark")].map(
|
|
254
|
+
(m) => m.textContent,
|
|
255
|
+
);
|
|
256
|
+
expect(marks).toEqual(["see ", "https"]);
|
|
257
|
+
const link = container.querySelector("a");
|
|
258
|
+
expect(link?.querySelector("mark")?.textContent).toBe("https");
|
|
259
|
+
expect(link).toHaveAttribute("href", "https://marimo.io/docs");
|
|
260
|
+
expect(container.textContent).toBe("see https://marimo.io/docs");
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
it("marks the fallback link when an image fails to load", () => {
|
|
264
|
+
const { container } = render(
|
|
265
|
+
<UrlDetector
|
|
266
|
+
parts={parseContent("https://example.com/image.png")}
|
|
267
|
+
highlightPattern={buildHighlightPattern("image")}
|
|
268
|
+
/>,
|
|
269
|
+
);
|
|
270
|
+
const img = container.querySelector("img");
|
|
271
|
+
expect(img).not.toBeNull();
|
|
272
|
+
fireEvent.error(img as HTMLImageElement);
|
|
273
|
+
const link = container.querySelector("a");
|
|
274
|
+
expect(link).toHaveAttribute("href", "https://example.com/image.png");
|
|
275
|
+
expect(link?.querySelector("mark")?.textContent).toBe("image");
|
|
276
|
+
});
|
|
277
|
+
});
|
|
@@ -41,6 +41,7 @@ import {
|
|
|
41
41
|
isNumericType,
|
|
42
42
|
} from "./types";
|
|
43
43
|
import { SentinelCell, WhitespaceMarkers } from "./sentinel-cell";
|
|
44
|
+
import { highlightMatches } from "./search-highlight";
|
|
44
45
|
import { detectSentinel, splitLeadingTrailingWhitespace } from "./utils";
|
|
45
46
|
import { uniformSample } from "./uniformSample";
|
|
46
47
|
import { MarkdownUrlDetector, UrlDetector } from "./url-detector";
|
|
@@ -278,7 +279,7 @@ export function generateColumns<T>({
|
|
|
278
279
|
);
|
|
279
280
|
},
|
|
280
281
|
|
|
281
|
-
cell: ({ column, renderValue, getValue, cell }) => {
|
|
282
|
+
cell: ({ column, renderValue, getValue, cell, table }) => {
|
|
282
283
|
function selectCell() {
|
|
283
284
|
if (selection !== "single-cell" && selection !== "multi-cell") {
|
|
284
285
|
return;
|
|
@@ -310,6 +311,7 @@ export function generateColumns<T>({
|
|
|
310
311
|
getValue,
|
|
311
312
|
selectCell,
|
|
312
313
|
cellStyles,
|
|
314
|
+
highlightPattern: table.options.meta?.highlightPattern,
|
|
313
315
|
});
|
|
314
316
|
|
|
315
317
|
// Row headers are bold
|
|
@@ -586,12 +588,14 @@ function renderDate({
|
|
|
586
588
|
dtype,
|
|
587
589
|
displayValue,
|
|
588
590
|
locale,
|
|
591
|
+
highlightPattern,
|
|
589
592
|
}: {
|
|
590
593
|
value: Date;
|
|
591
594
|
dataType?: DataType;
|
|
592
595
|
dtype?: string;
|
|
593
596
|
displayValue?: string;
|
|
594
597
|
locale: string;
|
|
598
|
+
highlightPattern?: RegExp;
|
|
595
599
|
}): React.ReactNode {
|
|
596
600
|
const type = dataType === "date" ? "date" : "datetime";
|
|
597
601
|
const timezone = extractTimezone(dtype);
|
|
@@ -600,7 +604,7 @@ function renderDate({
|
|
|
600
604
|
|
|
601
605
|
return (
|
|
602
606
|
<DatePopover date={value} type={type}>
|
|
603
|
-
{exactValue}
|
|
607
|
+
{highlightMatches(exactValue, highlightPattern)}
|
|
604
608
|
</DatePopover>
|
|
605
609
|
);
|
|
606
610
|
}
|
|
@@ -611,12 +615,14 @@ export function renderCellValue<TData, TValue>({
|
|
|
611
615
|
getValue,
|
|
612
616
|
selectCell,
|
|
613
617
|
cellStyles,
|
|
618
|
+
highlightPattern,
|
|
614
619
|
}: {
|
|
615
620
|
column: Column<TData, TValue>;
|
|
616
621
|
renderValue: () => TValue | null;
|
|
617
622
|
getValue: () => TValue;
|
|
618
623
|
selectCell?: () => void;
|
|
619
624
|
cellStyles?: string;
|
|
625
|
+
highlightPattern?: RegExp;
|
|
620
626
|
}) {
|
|
621
627
|
const value = getValue();
|
|
622
628
|
const format = column.getColumnFormatting?.();
|
|
@@ -626,6 +632,28 @@ export function renderCellValue<TData, TValue>({
|
|
|
626
632
|
|
|
627
633
|
const isWrapped = column.getColumnWrapping?.() === "wrap";
|
|
628
634
|
|
|
635
|
+
const textCell = (text: string) => (
|
|
636
|
+
<div onClick={selectCell} className={cellStyles}>
|
|
637
|
+
{highlightMatches(text, highlightPattern)}
|
|
638
|
+
</div>
|
|
639
|
+
);
|
|
640
|
+
const dateCell = (date: Date, displayValue?: string) => (
|
|
641
|
+
<div onClick={selectCell} className={cellStyles}>
|
|
642
|
+
<WithLocale>
|
|
643
|
+
{(locale) =>
|
|
644
|
+
renderDate({
|
|
645
|
+
value: date,
|
|
646
|
+
dataType,
|
|
647
|
+
dtype,
|
|
648
|
+
displayValue,
|
|
649
|
+
locale,
|
|
650
|
+
highlightPattern,
|
|
651
|
+
})
|
|
652
|
+
}
|
|
653
|
+
</WithLocale>
|
|
654
|
+
</div>
|
|
655
|
+
);
|
|
656
|
+
|
|
629
657
|
// Sentinel values (null, whitespace, NaN, Infinity, NaT) rendered specially.
|
|
630
658
|
// Empty strings are left as-is
|
|
631
659
|
const sentinel = detectSentinel(value, dataType, dtype);
|
|
@@ -641,23 +669,10 @@ export function renderCellValue<TData, TValue>({
|
|
|
641
669
|
try {
|
|
642
670
|
const date = new Date(value);
|
|
643
671
|
if (!format || !isValid(date)) {
|
|
644
|
-
return (
|
|
645
|
-
<div onClick={selectCell} className={cellStyles}>
|
|
646
|
-
{value}
|
|
647
|
-
</div>
|
|
648
|
-
);
|
|
672
|
+
return textCell(value);
|
|
649
673
|
}
|
|
650
674
|
|
|
651
|
-
|
|
652
|
-
return (
|
|
653
|
-
<div onClick={selectCell} className={cellStyles}>
|
|
654
|
-
<WithLocale>
|
|
655
|
-
{(locale) =>
|
|
656
|
-
renderDate({ value: date, dataType, dtype, displayValue, locale })
|
|
657
|
-
}
|
|
658
|
-
</WithLocale>
|
|
659
|
-
</div>
|
|
660
|
-
);
|
|
675
|
+
return dateCell(date, String(column.applyColumnFormatting(value)));
|
|
661
676
|
} catch (error) {
|
|
662
677
|
Logger.error("Error parsing datetime, fallback to string", error);
|
|
663
678
|
}
|
|
@@ -673,17 +688,9 @@ export function renderCellValue<TData, TValue>({
|
|
|
673
688
|
}
|
|
674
689
|
|
|
675
690
|
// e.g. 2010-10-07 17:15:00
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
: undefined
|
|
679
|
-
return (
|
|
680
|
-
<div onClick={selectCell} className={cellStyles}>
|
|
681
|
-
<WithLocale>
|
|
682
|
-
{(locale) =>
|
|
683
|
-
renderDate({ value, dataType, dtype, displayValue, locale })
|
|
684
|
-
}
|
|
685
|
-
</WithLocale>
|
|
686
|
-
</div>
|
|
691
|
+
return dateCell(
|
|
692
|
+
value,
|
|
693
|
+
format ? String(column.applyColumnFormatting(value)) : undefined,
|
|
687
694
|
);
|
|
688
695
|
}
|
|
689
696
|
|
|
@@ -729,7 +736,7 @@ export function renderCellValue<TData, TValue>({
|
|
|
729
736
|
className={cn(cellStyles, isWrapped && COLUMN_WRAPPING_STYLES)}
|
|
730
737
|
>
|
|
731
738
|
<WhitespaceMarkers value={leading} />
|
|
732
|
-
<UrlDetector parts={parts} />
|
|
739
|
+
<UrlDetector parts={parts} highlightPattern={highlightPattern} />
|
|
733
740
|
<WhitespaceMarkers value={trailing} />
|
|
734
741
|
</div>
|
|
735
742
|
);
|
|
@@ -755,11 +762,7 @@ export function renderCellValue<TData, TValue>({
|
|
|
755
762
|
}
|
|
756
763
|
|
|
757
764
|
if (format) {
|
|
758
|
-
return (
|
|
759
|
-
<div onClick={selectCell} className={cellStyles}>
|
|
760
|
-
{column.applyColumnFormatting(value)}
|
|
761
|
-
</div>
|
|
762
|
-
);
|
|
765
|
+
return textCell(String(column.applyColumnFormatting(value)));
|
|
763
766
|
}
|
|
764
767
|
|
|
765
768
|
// Format to the correct locale
|
|
@@ -769,26 +772,19 @@ export function renderCellValue<TData, TValue>({
|
|
|
769
772
|
<LocaleNumber
|
|
770
773
|
value={value}
|
|
771
774
|
minFractionDigits={column.columnDef.meta?.minFractionDigits}
|
|
775
|
+
highlightPattern={highlightPattern}
|
|
772
776
|
/>
|
|
773
777
|
</div>
|
|
774
778
|
);
|
|
775
779
|
}
|
|
776
780
|
|
|
777
781
|
if (typeof value === "boolean") {
|
|
778
|
-
return (
|
|
779
|
-
<div onClick={selectCell} className={cellStyles}>
|
|
780
|
-
{value ? "True" : "False"}
|
|
781
|
-
</div>
|
|
782
|
-
);
|
|
782
|
+
return textCell(value ? "True" : "False");
|
|
783
783
|
}
|
|
784
784
|
|
|
785
785
|
if (isPrimitiveOrNullish(value)) {
|
|
786
786
|
const rendered = renderValue();
|
|
787
|
-
return (
|
|
788
|
-
<div onClick={selectCell} className={cellStyles}>
|
|
789
|
-
{rendered == null ? "" : String(rendered)}
|
|
790
|
-
</div>
|
|
791
|
-
);
|
|
787
|
+
return textCell(rendered == null ? "" : String(rendered));
|
|
792
788
|
}
|
|
793
789
|
|
|
794
790
|
const mimeValues = getMimeValues(value);
|
|
@@ -826,14 +822,16 @@ export function renderCellValue<TData, TValue>({
|
|
|
826
822
|
export const LocaleNumber = ({
|
|
827
823
|
value,
|
|
828
824
|
minFractionDigits,
|
|
825
|
+
highlightPattern,
|
|
829
826
|
}: {
|
|
830
827
|
value: number;
|
|
831
828
|
minFractionDigits?: number;
|
|
829
|
+
highlightPattern?: RegExp;
|
|
832
830
|
}) => {
|
|
833
831
|
const { locale } = useLocale();
|
|
834
832
|
const format = useNumberFormatter({
|
|
835
833
|
minimumFractionDigits: minFractionDigits,
|
|
836
834
|
maximumFractionDigits: maxFractionalDigits(locale),
|
|
837
835
|
});
|
|
838
|
-
return format.format(value);
|
|
836
|
+
return highlightMatches(format.format(value), highlightPattern);
|
|
839
837
|
};
|
|
@@ -58,6 +58,7 @@ import { useColumnPinning } from "./hooks/use-column-pinning";
|
|
|
58
58
|
import { useScrollContainerHeight } from "./hooks/use-scroll-container-height";
|
|
59
59
|
import { CellSelectionProvider } from "./range-focus/provider";
|
|
60
60
|
import { DataTableBody, renderTableHeader } from "./renderers";
|
|
61
|
+
import { buildHighlightPattern } from "./search-highlight";
|
|
61
62
|
import { TableBottomBar } from "./TableBottomBar";
|
|
62
63
|
import { TableTopBar } from "./TableTopBar";
|
|
63
64
|
import {
|
|
@@ -231,6 +232,11 @@ const DataTableInternal = <TData,>({
|
|
|
231
232
|
return idx + offset;
|
|
232
233
|
}
|
|
233
234
|
|
|
235
|
+
const highlightPattern = React.useMemo(
|
|
236
|
+
() => buildHighlightPattern(searchQuery),
|
|
237
|
+
[searchQuery],
|
|
238
|
+
);
|
|
239
|
+
|
|
234
240
|
const table = useReactTable<TData>({
|
|
235
241
|
_features: [
|
|
236
242
|
ColumnPinning,
|
|
@@ -245,7 +251,7 @@ const DataTableInternal = <TData,>({
|
|
|
245
251
|
],
|
|
246
252
|
data,
|
|
247
253
|
columns,
|
|
248
|
-
meta: { rawData },
|
|
254
|
+
meta: { rawData, highlightPattern },
|
|
249
255
|
getCoreRowModel: getCoreRowModel(),
|
|
250
256
|
// pagination
|
|
251
257
|
rowCount: totalRows === "too_many" ? undefined : totalRows,
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
|
+
|
|
3
|
+
import type { ReactNode } from "react";
|
|
4
|
+
|
|
5
|
+
/** Half-open `[start, end)` offsets into a string. */
|
|
6
|
+
export type MatchRange = readonly [start: number, end: number];
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Build the pattern that finds `query` in a display string. Returns
|
|
10
|
+
* `undefined` for an empty query. The query is matched as a literal,
|
|
11
|
+
* case-insensitive substring, which mirrors how the backend selects rows.
|
|
12
|
+
* The `u` flag selects Unicode simple case folding, the same folding the
|
|
13
|
+
* backend regex engines use.
|
|
14
|
+
*/
|
|
15
|
+
export function buildHighlightPattern(query?: string): RegExp | undefined {
|
|
16
|
+
if (!query) {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
const escaped = query.replace(/[$()*+.?[\\\]^{|}]/g, "\\$&");
|
|
20
|
+
return new RegExp(escaped, "giu");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Offsets of every match of `pattern` in `text`. */
|
|
24
|
+
export function findMatchRanges(text: string, pattern?: RegExp): MatchRange[] {
|
|
25
|
+
if (!pattern) {
|
|
26
|
+
return [];
|
|
27
|
+
}
|
|
28
|
+
const ranges: MatchRange[] = [];
|
|
29
|
+
for (const match of text.matchAll(pattern)) {
|
|
30
|
+
ranges.push([match.index, match.index + match[0].length]);
|
|
31
|
+
}
|
|
32
|
+
return ranges;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Wrap the parts of `text` covered by `ranges` in `<mark>` elements.
|
|
37
|
+
* `ranges` are offsets into a larger string in which `text` starts at
|
|
38
|
+
* `offset`, so one set of ranges can be shared across adjacent segments.
|
|
39
|
+
* Returns `text` unchanged when no range overlaps it.
|
|
40
|
+
*/
|
|
41
|
+
export function highlightRanges(
|
|
42
|
+
text: string,
|
|
43
|
+
ranges: readonly MatchRange[],
|
|
44
|
+
offset = 0,
|
|
45
|
+
): ReactNode {
|
|
46
|
+
const segments: ReactNode[] = [];
|
|
47
|
+
let cursor = 0;
|
|
48
|
+
for (const [rangeStart, rangeEnd] of ranges) {
|
|
49
|
+
const start = Math.max(rangeStart - offset, cursor);
|
|
50
|
+
const end = Math.min(rangeEnd - offset, text.length);
|
|
51
|
+
if (end <= start) {
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
if (start > cursor) {
|
|
55
|
+
segments.push(text.slice(cursor, start));
|
|
56
|
+
}
|
|
57
|
+
segments.push(
|
|
58
|
+
<mark key={start} className="bg-search-highlight text-inherit">
|
|
59
|
+
{text.slice(start, end)}
|
|
60
|
+
</mark>,
|
|
61
|
+
);
|
|
62
|
+
cursor = end;
|
|
63
|
+
}
|
|
64
|
+
if (segments.length === 0) {
|
|
65
|
+
return text;
|
|
66
|
+
}
|
|
67
|
+
if (cursor < text.length) {
|
|
68
|
+
segments.push(text.slice(cursor));
|
|
69
|
+
}
|
|
70
|
+
return segments;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Wrap every match of `pattern` in `text` in a `<mark>` element. Returns
|
|
75
|
+
* `text` unchanged when `pattern` is absent or does not match.
|
|
76
|
+
*/
|
|
77
|
+
export function highlightMatches(text: string, pattern?: RegExp): ReactNode {
|
|
78
|
+
return highlightRanges(text, findMatchRanges(text, pattern));
|
|
79
|
+
}
|
|
@@ -6,6 +6,7 @@ import type { DataType } from "@/core/kernel/messages";
|
|
|
6
6
|
declare module "@tanstack/react-table" {
|
|
7
7
|
interface TableMeta<TData extends RowData> {
|
|
8
8
|
rawData?: TData[]; // raw data for filtering/copying (present only if format_mapping is provided)
|
|
9
|
+
highlightPattern?: RegExp; // built from the committed search query that produced the current rows
|
|
9
10
|
}
|
|
10
11
|
}
|
|
11
12
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
2
|
|
|
3
3
|
import { marked } from "marked";
|
|
4
|
-
import { useState } from "react";
|
|
4
|
+
import { Fragment, type ReactNode, useState } from "react";
|
|
5
5
|
import { MarkdownRenderer } from "@/components/markdown/markdown-renderer";
|
|
6
6
|
import {
|
|
7
7
|
Popover,
|
|
@@ -10,13 +10,20 @@ import {
|
|
|
10
10
|
} from "@/components/ui/popover";
|
|
11
11
|
import { Events } from "@/utils/events";
|
|
12
12
|
import type { ContentPart } from "@/utils/url-parser";
|
|
13
|
+
import { findMatchRanges, highlightRanges } from "./search-highlight";
|
|
13
14
|
|
|
14
|
-
const ImageWithFallback = ({
|
|
15
|
+
const ImageWithFallback = ({
|
|
16
|
+
url,
|
|
17
|
+
fallbackLabel,
|
|
18
|
+
}: {
|
|
19
|
+
url: string;
|
|
20
|
+
fallbackLabel?: ReactNode;
|
|
21
|
+
}) => {
|
|
15
22
|
const [error, setError] = useState(false);
|
|
16
23
|
const [open, setOpen] = useState(false);
|
|
17
24
|
|
|
18
25
|
if (error) {
|
|
19
|
-
return <URLAnchor url={url}
|
|
26
|
+
return <URLAnchor url={url}>{fallbackLabel}</URLAnchor>;
|
|
20
27
|
}
|
|
21
28
|
|
|
22
29
|
return (
|
|
@@ -93,21 +100,54 @@ export const MarkdownUrlDetector = ({
|
|
|
93
100
|
return <UrlDetector parts={parts} />;
|
|
94
101
|
};
|
|
95
102
|
|
|
96
|
-
export const UrlDetector = ({
|
|
103
|
+
export const UrlDetector = ({
|
|
104
|
+
parts,
|
|
105
|
+
highlightPattern,
|
|
106
|
+
}: {
|
|
107
|
+
parts: ContentPart[];
|
|
108
|
+
highlightPattern?: RegExp;
|
|
109
|
+
}) => {
|
|
110
|
+
// Match against the whole string so a query that spans a text part and
|
|
111
|
+
// a link is still marked. Each part then takes its slice of the ranges.
|
|
112
|
+
const ranges = highlightPattern
|
|
113
|
+
? findMatchRanges(parts.map(partText).join(""), highlightPattern)
|
|
114
|
+
: [];
|
|
115
|
+
|
|
116
|
+
let offset = 0;
|
|
97
117
|
const markup = parts.map((part, idx) => {
|
|
118
|
+
const text = partText(part);
|
|
119
|
+
const label = highlightRanges(text, ranges, offset);
|
|
120
|
+
offset += text.length;
|
|
121
|
+
|
|
98
122
|
if (part.type === "url") {
|
|
99
|
-
return
|
|
123
|
+
return (
|
|
124
|
+
<URLAnchor key={idx} url={part.url}>
|
|
125
|
+
{label}
|
|
126
|
+
</URLAnchor>
|
|
127
|
+
);
|
|
100
128
|
}
|
|
101
129
|
if (part.type === "image") {
|
|
102
|
-
return
|
|
130
|
+
return (
|
|
131
|
+
<ImageWithFallback key={idx} url={part.url} fallbackLabel={label} />
|
|
132
|
+
);
|
|
103
133
|
}
|
|
104
|
-
return
|
|
134
|
+
return <Fragment key={idx}>{label}</Fragment>;
|
|
105
135
|
});
|
|
106
136
|
|
|
107
137
|
return markup;
|
|
108
138
|
};
|
|
109
139
|
|
|
110
|
-
|
|
140
|
+
function partText(part: ContentPart): string {
|
|
141
|
+
return part.type === "text" ? part.value : part.url;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const URLAnchor = ({
|
|
145
|
+
url,
|
|
146
|
+
children,
|
|
147
|
+
}: {
|
|
148
|
+
url: string;
|
|
149
|
+
children?: ReactNode;
|
|
150
|
+
}) => {
|
|
111
151
|
return (
|
|
112
152
|
<a
|
|
113
153
|
href={url}
|
|
@@ -116,7 +156,7 @@ const URLAnchor = ({ url }: { url: string }) => {
|
|
|
116
156
|
onClick={Events.stopPropagation()}
|
|
117
157
|
className="text-link hover:underline"
|
|
118
158
|
>
|
|
119
|
-
{url}
|
|
159
|
+
{children ?? url}
|
|
120
160
|
</a>
|
|
121
161
|
);
|
|
122
162
|
};
|
package/src/css/globals.css
CHANGED
|
@@ -92,6 +92,7 @@
|
|
|
92
92
|
--action-hover: var(--yellow-10, #ffdc00);
|
|
93
93
|
--action-border: var(--yellow-11, #9e6c00);
|
|
94
94
|
--action-foreground: var(--yellow-12, #473b1f);
|
|
95
|
+
--search-highlight: var(--yellow-4, #fff394);
|
|
95
96
|
--link: light-dark(hsl(211deg 90% 42%), hsl(211deg 90% 62%));
|
|
96
97
|
--link-visited: light-dark(hsl(272deg 51% 54%), hsl(272deg 51% 74%));
|
|
97
98
|
--stale: hsl(42deg 56% 44% / 25%);
|
|
@@ -140,6 +141,7 @@
|
|
|
140
141
|
--action-hover: var(--yellow-7, #665417);
|
|
141
142
|
--action-border: var(--yellow-11, #f5e147);
|
|
142
143
|
--action-foreground: var(--yellow-12, #f6eeb4);
|
|
144
|
+
--search-highlight: var(--yellow-6, #524202);
|
|
143
145
|
--stale: hsl(54deg 100% 86.7% / 25%);
|
|
144
146
|
}
|
|
145
147
|
}
|