# [Phase 3] TipTap Editor & Reflect Implementation

## User Review Required
> [!IMPORTANT]
> **Database Schema Change**:
> - `links` collection will be added to store backlink relationships.
> - Documents will still be stored as Markdown string, but parsed/serialized via TipTap.
> - Existing documents will need to be loaded into TipTap (compatible).

## Proposed Changes

### 1. Dependencies [NEW]
- `@tiptap/react`, `@tiptap/pm`, `@tiptap/starter-kit`
- `@tiptap/extension-link`, `@tiptap/extension-placeholder`
- `tiptap-markdown` (for serialize/parse)

### 2. Frontend Components
#### [NEW] [ReflectEditor.tsx](file:///c:/Users/drumb/.gemini/antigravity/scratch/InsuWiki/nextapp/src/components/ReflectEditor.tsx)
- Replaces simple textarea in `docs/[id]/page.tsx`
- Features: Slash commands, Bubble menu, Floating menu.

#### [NEW] [WikiLinkExtension.ts](file:///c:/Users/drumb/.gemini/antigravity/scratch/InsuWiki/nextapp/src/lib/tiptap/WikiLinkExtension.ts)
- Custom TipTap extension to handle `[[` trigger.
- Shows document suggestion popup.

### 3. Backend (Client-side Logic)
#### [MODIFY] [page.tsx](file:///c:/Users/drumb/.gemini/antigravity/scratch/InsuWiki/nextapp/src/app/docs/[id]/page.tsx)
- Integrate `ReflectEditor`
- On Save: Extract links from content (using regex `\[\[(.*?)\?\]\]` or TipTap JSON) and update `links` collection.

### 4. Firestore Advanced Backlinks
#### [MODIFY] `documents` Collection
- `outgoingLinkIds`: string[] (Explicit links to existing documents)
- `searchKeywords`: string[] (Tokenized content for Unlinked Mentions)

### 5. Advanced Logic
#### [NEW] Unlinked Mentions Search
- On Save: Tokenize `content` into `searchKeywords`.
- On View: 
    - Query `outgoingLinkIds` array-contains `docId` (Linked Mentions).
    - Query `searchKeywords` array-contains `docTitle` (Unlinked Mentions).

## Verification Plan
1.  **Editor Test**: Check typical markdown (bold, list, header) input.
2.  **WikiLink Test**: Type `[[`, select a doc, save.
3.  **Backlink Test**: Check if the target document shows "Linked Mentions".
4.  **ID Link Test**: Rename a document and check if links still work (if using ID).
5.  **Unlinked Mentions Test**: Write "커피" in a doc without `[[ ]]`, and check if it appears in the "커피" document's backlinks.
