feat: 검색 결과에 찾은 말을 표시하고 릴리스 탭 제목을 구분한다
검색 결과가 제목과 요약을 그대로 보여 주기만 했다. "session" 으로 찾아도 어느 대목이 걸렸는지 눈으로 다시 찾아야 했고, 요약은 목록에서 세 줄로 잘리므로 걸린 대목이 잘린 뒤쪽이면 왜 이 결과가 나왔는지조차 알 수 없었다. 대소문자는 구분하지 않는다 — 이 저장소의 글은 Session 과 session 을 섞어 쓴다. 배경을 칠하는 대신 밑줄과 굵기로 표시해 목록의 회색 톤을 지킨다. 릴리스 탭 제목이 판마다 「변경 기록 · Tech Log」로 같았다. 이 화면은 공개 문서 렌더러를 타지 않아 앞서 붙인 훅이 닿지 않았다. 훅은 이른 return 보다 앞에 둔다. 처음에 데이터를 꺼낸 뒤에 불렀더니 렌더마다 호출 순서가 달라져 lint 가 잡았다. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_011nAjrqNHGPNngN4aUbcyvJ
This commit is contained in:
co-authored by
Claude Opus 5
parent
a936444dfc
commit
833943a286
@@ -2,6 +2,7 @@ import { useId, useRef, useState } from "react";
|
||||
import { Link } from "react-router-dom";
|
||||
|
||||
import { usePublicContent } from "../use-public-content.tsx";
|
||||
import { HighlightMatch } from "../../shared/highlight-match.tsx";
|
||||
|
||||
type SearchDialogProps = {
|
||||
className?: string;
|
||||
@@ -105,8 +106,12 @@ export function SearchDialog({
|
||||
<li key={entry.path}>
|
||||
<Link to={entry.path} onClick={close}>
|
||||
<span>{entry.contentType}</span>
|
||||
<strong>{entry.title}</strong>
|
||||
<p>{entry.summary}</p>
|
||||
<strong>
|
||||
<HighlightMatch text={entry.title} query={query} />
|
||||
</strong>
|
||||
<p>
|
||||
<HighlightMatch text={entry.summary} query={query} />
|
||||
</p>
|
||||
</Link>
|
||||
</li>
|
||||
))}
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
} from "../../../../../presentation/routes/route-input.tsx";
|
||||
import { usePublicContent } from "../use-public-content.tsx";
|
||||
import { ProseText } from "../../shared/public-render/prose-text.tsx";
|
||||
import { useDocumentMetadata } from "../../shared/use-document-metadata.ts";
|
||||
|
||||
export function ReleasePage() {
|
||||
const { params } = useRouteInput<"TECH_LOG_RELEASE">();
|
||||
@@ -13,9 +14,17 @@ export function ReleasePage() {
|
||||
const view = usePublicContent(["tech-log", "release", version], async (queries) => ({
|
||||
release: await queries.getRelease(version),
|
||||
}));
|
||||
if (!view.ready) return view.fallback;
|
||||
/*
|
||||
탭 제목이 릴리스마다 「변경 기록 · Tech Log」로 같아 어느 판인지 알 수 없었다.
|
||||
훅은 이른 return 보다 앞에 있어야 한다 — 렌더마다 같은 순서로 불려야 하므로.
|
||||
*/
|
||||
const release = view.ready ? view.data.release : undefined;
|
||||
useDocumentMetadata(
|
||||
release ? `v${release.version} ${release.title}` : undefined,
|
||||
release?.summary,
|
||||
);
|
||||
|
||||
const { release } = view.data;
|
||||
if (!view.ready) return view.fallback;
|
||||
if (!release) return <RegisteredNotFoundRoute />;
|
||||
|
||||
return (
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
import { Fragment } from "react";
|
||||
|
||||
/**
|
||||
* 검색 결과에서 찾은 말을 표시한다.
|
||||
*
|
||||
* <p>결과 목록은 제목과 요약을 그대로 보여 주기만 했다. "session" 으로 찾아도 어느 대목이
|
||||
* 걸린 것인지 눈으로 다시 찾아야 했고, 요약이 세 줄로 잘리므로 걸린 대목이 잘린 뒤쪽에 있으면
|
||||
* 왜 이 결과가 나왔는지조차 알 수 없었다.
|
||||
*
|
||||
* <p>대소문자는 구분하지 않는다 — 이 저장소의 글은 `Session` 과 `session` 을 섞어 쓴다.
|
||||
*/
|
||||
function escapeForRegExp(value: string): string {
|
||||
return value.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&");
|
||||
}
|
||||
|
||||
export function HighlightMatch({ text, query }: { text: string; query: string }) {
|
||||
const needle = query.trim();
|
||||
if (!needle) return <>{text}</>;
|
||||
|
||||
const pattern = new RegExp(`(${escapeForRegExp(needle)})`, "giu");
|
||||
const parts = text.split(pattern);
|
||||
// 걸린 곳이 없으면 원문 그대로 — 쪼갠 조각을 다시 붙이지 않는다.
|
||||
if (parts.length === 1) return <>{text}</>;
|
||||
|
||||
return (
|
||||
<>
|
||||
{parts.map((part, index) =>
|
||||
// split 이 만든 조각 중 홀수 자리가 잡힌 부분이다.
|
||||
index % 2 === 1 ? (
|
||||
<mark key={index}>{part}</mark>
|
||||
) : (
|
||||
<Fragment key={index}>{part}</Fragment>
|
||||
),
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -1404,6 +1404,9 @@ dialog::backdrop {
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
/* 검색에서 걸린 말. 배경을 칠하는 대신 밑줄과 굵기로 — 목록의 회색 톤을 깨지 않는다. */
|
||||
.search-results mark { background: none; color: var(--ink); font-weight: 650; text-decoration: underline; text-underline-offset: 3px; }
|
||||
|
||||
.search-results a:hover strong {
|
||||
color: var(--signal);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
// @vitest-environment jsdom
|
||||
|
||||
import { render } from "@testing-library/react";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { HighlightMatch } from "../../src/features/tech-log/presentation/shared/highlight-match.tsx";
|
||||
|
||||
/**
|
||||
* 검색 결과가 제목과 요약을 그대로 보여 주기만 해서, 무엇이 걸려 이 결과가 나왔는지 눈으로
|
||||
* 다시 찾아야 했다.
|
||||
*/
|
||||
describe("검색어 강조", () => {
|
||||
it("찾은 말을 표시한다", () => {
|
||||
const { container } = render(
|
||||
<HighlightMatch text="Session Cookie와 session ID는 다르다" query="session" />,
|
||||
);
|
||||
const marks = [...container.querySelectorAll("mark")].map((m) => m.textContent);
|
||||
// 대소문자를 구분하지 않는다 — 이 저장소의 글은 둘을 섞어 쓴다.
|
||||
expect(marks).toEqual(["Session", "session"]);
|
||||
expect(container.textContent).toBe("Session Cookie와 session ID는 다르다");
|
||||
});
|
||||
|
||||
it("걸린 곳이 없으면 원문 그대로 둔다", () => {
|
||||
const { container } = render(<HighlightMatch text="토큰 경계" query="session" />);
|
||||
expect(container.querySelectorAll("mark")).toHaveLength(0);
|
||||
expect(container.textContent).toBe("토큰 경계");
|
||||
});
|
||||
|
||||
it("정규식 문자가 들어가도 깨지지 않는다", () => {
|
||||
const { container } = render(<HighlightMatch text="a+b 는 a+b 다" query="a+b" />);
|
||||
expect(container.querySelectorAll("mark")).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("빈 검색어면 아무것도 표시하지 않는다", () => {
|
||||
const { container } = render(<HighlightMatch text="아무 글" query=" " />);
|
||||
expect(container.querySelectorAll("mark")).toHaveLength(0);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user