diff --git a/src/features/tech-log/presentation/public/components/search-dialog.tsx b/src/features/tech-log/presentation/public/components/search-dialog.tsx index c16439d..43ecc06 100644 --- a/src/features/tech-log/presentation/public/components/search-dialog.tsx +++ b/src/features/tech-log/presentation/public/components/search-dialog.tsx @@ -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({
  • {entry.contentType} - {entry.title} -

    {entry.summary}

    + + + +

    + +

  • ))} diff --git a/src/features/tech-log/presentation/public/pages/release-page.tsx b/src/features/tech-log/presentation/public/pages/release-page.tsx index fb3de3c..de9616c 100644 --- a/src/features/tech-log/presentation/public/pages/release-page.tsx +++ b/src/features/tech-log/presentation/public/pages/release-page.tsx @@ -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 ; return ( diff --git a/src/features/tech-log/presentation/shared/highlight-match.tsx b/src/features/tech-log/presentation/shared/highlight-match.tsx new file mode 100644 index 0000000..4b23630 --- /dev/null +++ b/src/features/tech-log/presentation/shared/highlight-match.tsx @@ -0,0 +1,37 @@ +import { Fragment } from "react"; + +/** + * 검색 결과에서 찾은 말을 표시한다. + * + *

    결과 목록은 제목과 요약을 그대로 보여 주기만 했다. "session" 으로 찾아도 어느 대목이 + * 걸린 것인지 눈으로 다시 찾아야 했고, 요약이 세 줄로 잘리므로 걸린 대목이 잘린 뒤쪽에 있으면 + * 왜 이 결과가 나왔는지조차 알 수 없었다. + * + *

    대소문자는 구분하지 않는다 — 이 저장소의 글은 `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 ? ( + {part} + ) : ( + {part} + ), + )} + + ); +} diff --git a/src/features/tech-log/presentation/styles/globals.css b/src/features/tech-log/presentation/styles/globals.css index 67ec74e..8f957c7 100644 --- a/src/features/tech-log/presentation/styles/globals.css +++ b/src/features/tech-log/presentation/styles/globals.css @@ -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); } diff --git a/tests/component/highlight-match.test.tsx b/tests/component/highlight-match.test.tsx new file mode 100644 index 0000000..f750bd1 --- /dev/null +++ b/tests/component/highlight-match.test.tsx @@ -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( + , + ); + 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(); + expect(container.querySelectorAll("mark")).toHaveLength(0); + expect(container.textContent).toBe("토큰 경계"); + }); + + it("정규식 문자가 들어가도 깨지지 않는다", () => { + const { container } = render(); + expect(container.querySelectorAll("mark")).toHaveLength(2); + }); + + it("빈 검색어면 아무것도 표시하지 않는다", () => { + const { container } = render(); + expect(container.querySelectorAll("mark")).toHaveLength(0); + }); +});