Skip to content

feat(content): write and save drafts in the content editor - #1258

Merged
gkhngyk merged 9 commits into
ansvisor:mainfrom
cihan-gyk:feature/content-editor-core
Oct 21, 2026
Merged

gkhngyk merged 9 commits into
ansvisor:mainfrom
cihan-gyk:feature/content-editor-core

Conversation

@cihan-gyk

@cihan-gyk cihan-gyk commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Content Editor (spec v2), PR 2 of the approved plan: CE-004, the editor core.

Stacked on #1256 (Content group + Library, migration 00204). Please merge #1256 first. Until then this PR also shows #1256's commit; its own change is 34aac22.

A draft opens at /dashboard/content/editor/{draftId}. The page is full height, as Chat is.

Header (spec §6). Back to the Library, the draft's title, page type and "New page" tags, the method (Create / Optimize) and status badges, the save state, and Mark ready.

  • The header stays on one line: the title truncates, and the tags hide below lg.
  • The draft's title is always its first H1 (else the first heading or text), so the Library lists what the page says.

Toolbar. H1 H2 H3 · Bold Italic Link · bulleted list, numbered list, quote · table, image.

  • Inside a table: add a row, add a column, delete the table.
  • The counter shows "2,610 / 3,000 characters" against the spec's default Length rule (3,000; product 1,200; category 800) until the Rules tab (CE-009) makes it editable.
  • useEditorState re-reads only what the toolbar shows.

Document. TipTap rich text in the app's prose styles: tables with cell borders, a placeholder while empty, and dark mode via prose-invert. There is no code, code block or horizontal rule, since these are pages.

Autosave (_use-autosave.ts)

  • A save goes out 1.5 s after typing pauses. Saves never overlap: a change made during a save waits for its own pause.
  • A failed save keeps the text and shows "Couldn't save · Retry".
  • Leaving the page with anything unsaved asks first (beforeunload), and leaving the editor inside the app saves what is waiting.

Server actions (lib/actions/content-drafts.ts)

  • getContentDraft returns null for a malformed id.
  • createContentDraft starts a blank draft as the signed-in member. The Library's new New content button uses it until the Start screen (CE-015) replaces it.
  • saveContentDraft checks with Zod: body up to 500k characters, title up to 300. It throws not_found when no row changed, so a save RLS refused isn't reported as saved.
  • markContentDraftReady moves only draft or in_review to ready. A published draft stays published.

Links. safeLinkHref accepts http(s), mailto, or a path on the brand's site, and adds https:// to a bare domain. It refuses javascript:, data:, //host and /\host. TipTap's isAllowedUri uses it too, so a stored link that isn't allowed is dropped when content loads. Images are added by an https address; uploads come with attachments (CE-006).

Sidebar. NavChild.activeFor lets the Library stay current while a draft is open (/dashboard/content/editor/…).

New dependencies (approved in the plan), pinned to 3.31.4 (2026-09-30), because 3.32.0 was a week old: @tiptap/react, @tiptap/pm, @tiptap/core, @tiptap/starter-kit, @tiptap/extension-table, @tiptap/extension-image, @tiptap/extensions. All are MIT.

  • They're pinned exact because their peers require the same version.
  • yarn.lock only gains entries.
  • The @floating-ui/dom peer warning is for TipTap's floating menus, which aren't used.

Next, per the plan:

  • CE-005 + CE-020: paste cleaning, HTML view, and a server-side HTML re-check with sanitize-html.
  • CE-006: attachments.
  • The right panel's tabs come in phase 2.

Related issue

Content Editor spec v2 (CE-004). Stacked on #1256.

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

  1. With 00204 applied (feat(content): add the Content group and its Library of drafts #1256), open Content › Library and click New content. A blank draft opens in the editor. The Library stays marked in the sidebar.
  2. Type an H1 (toolbar or # ), a paragraph and an H2.
    • The header title follows the H1.
    • "Unsaved changes" turns into "Saved"; reload, and the text is there.
  3. Try the formatting:
    • bold, italic, lists and quote;
    • insert a table, then add a row and a column and delete it;
    • add a link, edit it (the dialog shows its address), remove it, and try javascript:alert(1) (refused);
    • insert an image by https address.
  4. Click Mark ready: the status becomes Ready and the button turns off. The Library shows Ready.
  5. Stop the network (DevTools offline) and type: "Couldn't save · Retry". Go back online and Retry. Try closing the tab with unsaved text.
  6. Try light and dark mode, and a 1024px window.

Tests:

  • lib/content/editor.test.ts: title, length target, links.
  • lib/actions/content-drafts.test.ts: validation, not_found, Mark ready.
  • _use-autosave.test.tsx: debounce, no overlap, retry, beforeunload, save on leave.
  • content/editor/[draftId]/page.test.tsx: TipTap in jsdom; header, counter, Mark ready, brand follow, not found.
  • Library New content.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/) (0 errors; the 6 warnings are in untouched files, as on main)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

yarn test (1,939 tests) and yarn build pass.

🤖 Generated with Claude Code

cihan-gyk and others added 2 commits October 20, 2026 17:09
Content Editor CE-001 and CE-002. Content becomes a group in the sidebar,
like Prompts: Opportunities keeps /dashboard/content, and the new Library
(/dashboard/content/library) lists every page a person or the Content Agent
writes or changes in Ansvisor, by status (Drafts, In review, Ready,
Published), with its page type, method, author and AEO score.

- Migration 00203: content_drafts with member RLS (select, insert, update;
  no delete yet). A guard trigger keeps author, created_by, brand and the
  scores server-side, so a member can't pass a draft off as the agent's.
- Sidebar: each submenu remembers its own open state (Prompts keeps its
  key), and only the longest matching entry is current, so Opportunities
  isn't marked on the Library.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Content Editor CE-004, stacked on the Library (ansvisor#1256). A draft opens at
/dashboard/content/editor/{id}: a header with its title, page type,
method and status, whether it's saved, and Mark ready; the toolbar (H1 H2
H3, bold, italic, link, lists, quote, table with add row / add column /
delete, image by address) with the length counter against the page
type's target; and the page as rich text (TipTap 3.31.4, pinned).

- Autosave after a pause in typing; saves never overlap, a failed one
  keeps the text and offers Retry, and leaving with unsaved text asks.
  The draft's title is its first H1.
- saveContentDraft validates the body (500k chars) and title and says
  when RLS matched no row; markContentDraftReady moves draft or in review
  to ready only.
- Links: http(s), mailto or a path on the brand's site; javascript:,
  //host and /\host are refused, in the dialog and when content loads.
- The Library gets New content (a blank draft, until the Start screen),
  and its entry stays current while a draft is open in the editor.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk second Content Editor PR, the editor core (CE-004). It's stacked on #1256, so please merge that first. It adds TipTap pinned to 3.31.4; 3.32.0 was only a week old. Autosave never overlaps saves and says when RLS matched no row. Links refuse javascript:, //host and /\host, also when stored content loads. Server-side HTML sanitizing comes in the next PR with paste cleaning and the HTML view. Could you take a look?

@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 20, 2026
Links in a draft looked like the text around them (dark, barely
underlined), so a link just added seemed not to be there. They're blue
and underlined now. With the cursor in a link, the toolbar shows its
address, which opens it in a new tab; Cmd/Ctrl + click on it in the page
does too. A click alone still puts the cursor in it to edit. A path on
the brand's own site is shown, not opened: it has no address here.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk one more commit here (7ddc8f7) after the product owner tried the editor: links in a draft looked like plain text, so they're blue and underlined now. With the cursor in a link, the toolbar shows its address, which opens it in a new tab; Cmd/Ctrl + click opens it too. A plain click still puts the cursor in the link to edit it.

cihan-gyk and others added 3 commits October 20, 2026 20:49
ansvisor#1257 took 00203 (citation gap rollups) on main, so the content drafts
table moves to the next free number. The file is otherwise unchanged;
schema.sql is rebuilt.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few fixes before merge:

  1. Autosave silently overwrites other writers (web/src/lib/actions/content-drafts.ts:124-140). saveContentDraft writes the whole body_html by id with no version check. Two tabs, two teammates on the same draft, or the Content Agent's server-side writes later in this stack (#1307's content-writer.js, the suggestions in #1303): whoever saves last wins, and the other side never finds out. Keep the updatedAt the editor loaded (and each save returns), send it with the save, and add .eq('updated_at', base) to the update. When no row matches, read the row again: if it's gone, that's not_found; if it still exists, that's a conflict, and the header should say "Changed elsewhere · Reload" instead of overwriting.

  2. Mark ready can mark text that isn't saved (_draft-editor.tsx:97-102, _use-autosave.ts:27-49). await autosave.retry() resolves immediately when a save is already running (if (saving.current …) return). It also resolves after a failed save, because flush catches the error. Either way markContentDraftReady runs. Have flush return the in-flight promise and rethrow (or return false) on failure, and don't mark the draft ready while the state is failed or unsaved.

  3. An over-limit body can never be saved, and nothing says why (content-drafts.ts:130, web/src/lib/content/editor.ts:11). Above 500,000 characters the action throws invalid. In production Next.js hides the message from server action errors, so the client only shows "Couldn't save · Retry", and retrying can't work. This gets easy to hit once #1261 inserts big tables. Return a typed result ({ ok: false, reason: 'too_large' }) and show a specific message. Better still, check the length on the client before scheduling the save.

  4. Nit: web/src/lib/content/editor.ts:13 says the title limit is from 00203. The migration is 00204.

cihan-gyk and others added 2 commits October 21, 2026 15:01
…unsaved text ready

- saveContentDraft takes the version the editor last saw (updated_at and
  a SHA-256 of the page) and writes with .eq('updated_at', base). When
  another write came first, the page tells which kind: the same page means
  only other columns changed (Mark ready, the type), and the save goes over
  them; another page comes back `conflict` and nothing is overwritten. The
  header then says "Changed elsewhere · Reload" and autosave stops.
- Results are typed ({ ok: false, reason }) since Next.js hides server
  action messages in production: a page over 500,000 characters says "Too
  long to save" (and isn't sent at all), not "Couldn't save · Retry".
- Mark ready settles first: it waits for a save under way and isn't run
  when the page can't be saved.
- Nit: the title limit comes from 00204.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk Review fixes for #1258 are in d539e09 and bbfb5b3 (merged up the whole stack to #1310, no rebase):

  1. Autosave no longer overwrites other writers. saveContentDraft takes the version the editor last saw (updated_at plus a SHA-256 of the page) and writes with .eq('updated_at', base).
    • When no row matches, it reads the row again:
      • gone → not_found;
      • the same page under a newer updated_at → only another column changed (Mark ready, the page type, rules, the brief…), so it saves over that version (up to 3 tries);
      • another page → conflict, and nothing is written.
    • The header then says "Changed elsewhere · Reload", and autosave stops until a reload.
    • I compared the page instead of updated_at alone because the editor's own non-body writes also move updated_at. On updated_at alone, typing after Mark ready or a type pick would read as a foreign write, and the user would lose their typing on reload. The hash is Web Crypto, the same in the browser and on the server: the editor hashes the page it loaded, and every save returns the hash of what it stored (after sanitizeDraftHtml).
    • History's restore (feat(content): keep a draft's earlier versions in History and restore any of them #1301) hands the restored version to the autosave as its new base.
  2. Mark ready waits for the page to be saved. It calls settle() (moved down from feat(content): keep a draft's earlier versions in History and restore any of them #1301): it saves what's waiting, waits for a save under way, and throws on a failed, conflicting or too-large page. In that case Mark ready doesn't run and says why. While the page is in conflict or too large, the button is off.
  3. Over-limit bodies get a typed answer. Results are typed ({ ok: false, reason: 'conflict' | 'too_large' | 'not_found' | 'invalid' }), since production hides server-action messages. A page over 500,000 characters isn't sent at all: the header says "Too long to save" with a hint, instead of "Couldn't save · Retry".
  4. The nit: the title limit comment now says 00204.

Tests: content-drafts.test.ts (save over the version, over a non-body change, conflict, not_found, too_large/invalid) and _use-autosave.test.tsx (base per save, conflict stops saving, too large isn't sent, settle, discard to a restored version).

Checked on the stack's top (#1310): web 2,343 tests, tsc, lint and Prettier, yarn build. In the browser I checked a conflict from a direct body write (nothing overwritten; Reload shows the other version), a non-body change (saves on), and too large (nothing sent; it saves again after an undo).

gkhngyk
gkhngyk previously approved these changes Oct 21, 2026

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes check out.

  • The conflict check looks right. Saves are guarded by updated_at, a write that only changed other columns is recognised by the page hash and goes through, and a different page comes back as conflict and isn't overwritten. Mark ready now waits for settle(), and results are typed, so "too large" no longer arrives as a hidden error.

One small thing the fix introduces:

  1. A save whose response is lost reads as someone else's write (web/src/lib/actions/content-drafts.ts:170). Suppose a save commits but the client never gets the answer (network drop, function timeout after the write). The client keeps the old base. The next save then finds its own previous text under a newer updated_at, the hashes differ, and it reports conflict, which stops autosave and makes the user reload, losing whatever they typed since. Fix: send the hash of the last unconfirmed save along with base, and treat a match on either hash as your own write.

@github-actions github-actions Bot removed the needs-migration Adds or edits a migration — apply it to the hosted database label Oct 21, 2026

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fixes check out. Merging with the stack.

@gkhngyk
gkhngyk merged commit 98a6cb8 into ansvisor:main Oct 21, 2026
4 checks passed
gkhngyk added a commit to cihan-gyk/ansvisor that referenced this pull request Oct 21, 2026
gkhngyk added a commit that referenced this pull request Oct 21, 2026
* feat(content): add the Content group and its Library of drafts

Content Editor CE-001 and CE-002. Content becomes a group in the sidebar,
like Prompts: Opportunities keeps /dashboard/content, and the new Library
(/dashboard/content/library) lists every page a person or the Content Agent
writes or changes in Ansvisor, by status (Drafts, In review, Ready,
Published), with its page type, method, author and AEO score.

- Migration 00203: content_drafts with member RLS (select, insert, update;
  no delete yet). A guard trigger keeps author, created_by, brand and the
  scores server-side, so a member can't pass a draft off as the agent's.
- Sidebar: each submenu remembers its own open state (Prompts keeps its
  key), and only the longest matching entry is current, so Opportunities
  isn't marked on the Library.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* feat(content): write and save drafts in the content editor

Content Editor CE-004, stacked on the Library (#1256). A draft opens at
/dashboard/content/editor/{id}: a header with its title, page type,
method and status, whether it's saved, and Mark ready; the toolbar (H1 H2
H3, bold, italic, link, lists, quote, table with add row / add column /
delete, image by address) with the length counter against the page
type's target; and the page as rich text (TipTap 3.31.4, pinned).

- Autosave after a pause in typing; saves never overlap, a failed one
  keeps the text and offers Retry, and leaving with unsaved text asks.
  The draft's title is its first H1.
- saveContentDraft validates the body (500k chars) and title and says
  when RLS matched no row; markContentDraftReady moves draft or in review
  to ready only.
- Links: http(s), mailto or a path on the brand's site; javascript:,
  //host and /\host are refused, in the dialog and when content loads.
- The Library gets New content (a blank draft, until the Start screen),
  and its entry stays current while a draft is open in the editor.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* feat(content): clean pasted content and edit a draft as HTML

Content Editor CE-005 and CE-020, stacked on the editor core (#1258).

- Paste: from Word, Google Docs, Notion or a web page, headings (H4-H6
  as H3), paragraphs, bold, italic, lists, links, tables and quotes stay;
  fonts, colors, classes, inline styles, scripts, iframes and utm_*,
  gclid, fbclid on links go. Google Docs' styled bold and italic become
  strong and em first. Pasted images wait for attachments. Cmd/Ctrl +
  Shift + V pastes plain text.
- </> HTML shows the page's HTML as a draft stores it, one block per
  line; Back to visual cleans it with the same allow-list and says how
  many unsafe tags it removed. Schema JSON-LD is kept: a block in the
  editor, written again from its JSON so it can't carry markup.
- The server checks every save against the allow-list again
  (sanitize-html), so a request made without the editor can't store a
  script, a handler or a javascript: link.
- dompurify 3.4.16 and sanitize-html 2.18.0, pinned.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(content): use a sanitize-html that runs on Node 20

sanitize-html 2.18.0 needs Node 22.12 or later, and CI and the web
Docker image run Node 20, so yarn install refused it. 2.17.0 has no
engine limit and the same API; @types/sanitize-html 2.16.0 matches it
(both on htmlparser2 8).

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(content): show links in the editor as links, and open them

Links in a draft looked like the text around them (dark, barely
underlined), so a link just added seemed not to be there. They're blue
and underlined now. With the cursor in a link, the toolbar shows its
address, which opens it in a new tab; Cmd/Ctrl + click on it in the page
does too. A click alone still puts the cursor in it to edit. A path on
the brand's own site is shown, not opened: it has no address here.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* chore(content): renumber the content drafts migration to 00204

#1257 took 00203 (citation gap rollups) on main, so the content drafts
table moves to the next free number. The file is otherwise unchanged;
schema.sql is rebuilt.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(content): save only over the page the editor saw, and never mark unsaved text ready

- saveContentDraft takes the version the editor last saw (updated_at and
  a SHA-256 of the page) and writes with .eq('updated_at', base). When
  another write came first, the page tells which kind: the same page means
  only other columns changed (Mark ready, the type), and the save goes over
  them; another page comes back `conflict` and nothing is overwritten. The
  header then says "Changed elsewhere · Reload" and autosave stops.
- Results are typed ({ ok: false, reason }) since Next.js hides server
  action messages in production: a page over 500,000 characters says "Too
  long to save" (and isn't sent at all), not "Couldn't save · Retry".
- Mark ready settles first: it waits for a save under way and isn't run
  when the page can't be saved.
- Nit: the title limit comes from 00204.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(content): find JSON-LD scripts with a linear scan, not a backtracking regex

The JSON-LD pattern had two [^>]* runs around \btype, so input with many
unclosed `<script type=application/ld+json ` tags took cubic time (about
16 s at 33k characters), and the server runs it on every save. It is now
a scan (indexOf for the tag, its end and </script>, the type tested on the
tag alone), linear in the length; a 500,000-character page built to
backtrack takes milliseconds. The paste cleaner uses the same scan.

sanitize-draft-html.ts now says stored HTML is untrusted: a member can
write body_html through PostgREST past this check, so readers parse or
clean it again.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(content): keep Mark ready off while the page can't be saved

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(content): find JSON-LD scripts by ASCII lower case, so a Turkish İ before them keeps them

`toLowerCase()` turns 'İ' into two UTF-16 units, so every İ before a JSON-LD
script shifted the positions taken from the lower-cased copy: the schema was
dropped on save, with as many characters of the text after it. Only ASCII
letters are lower-cased now, which keeps the length and stays linear.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Cihan <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Gökhan Geyik <[email protected]>
gkhngyk added a commit that referenced this pull request Oct 21, 2026
…ick (#1260)

* feat(content): add the Content group and its Library of drafts

Content Editor CE-001 and CE-002. Content becomes a group in the sidebar,
like Prompts: Opportunities keeps /dashboard/content, and the new Library
(/dashboard/content/library) lists every page a person or the Content Agent
writes or changes in Ansvisor, by status (Drafts, In review, Ready,
Published), with its page type, method, author and AEO score.

- Migration 00203: content_drafts with member RLS (select, insert, update;
  no delete yet). A guard trigger keeps author, created_by, brand and the
  scores server-side, so a member can't pass a draft off as the agent's.
- Sidebar: each submenu remembers its own open state (Prompts keeps its
  key), and only the longest matching entry is current, so Opportunities
  isn't marked on the Library.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* feat(content): write and save drafts in the content editor

Content Editor CE-004, stacked on the Library (#1256). A draft opens at
/dashboard/content/editor/{id}: a header with its title, page type,
method and status, whether it's saved, and Mark ready; the toolbar (H1 H2
H3, bold, italic, link, lists, quote, table with add row / add column /
delete, image by address) with the length counter against the page
type's target; and the page as rich text (TipTap 3.31.4, pinned).

- Autosave after a pause in typing; saves never overlap, a failed one
  keeps the text and offers Retry, and leaving with unsaved text asks.
  The draft's title is its first H1.
- saveContentDraft validates the body (500k chars) and title and says
  when RLS matched no row; markContentDraftReady moves draft or in review
  to ready only.
- Links: http(s), mailto or a path on the brand's site; javascript:,
  //host and /\host are refused, in the dialog and when content loads.
- The Library gets New content (a blank draft, until the Start screen),
  and its entry stays current while a draft is open in the editor.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* feat(content): clean pasted content and edit a draft as HTML

Content Editor CE-005 and CE-020, stacked on the editor core (#1258).

- Paste: from Word, Google Docs, Notion or a web page, headings (H4-H6
  as H3), paragraphs, bold, italic, lists, links, tables and quotes stay;
  fonts, colors, classes, inline styles, scripts, iframes and utm_*,
  gclid, fbclid on links go. Google Docs' styled bold and italic become
  strong and em first. Pasted images wait for attachments. Cmd/Ctrl +
  Shift + V pastes plain text.
- </> HTML shows the page's HTML as a draft stores it, one block per
  line; Back to visual cleans it with the same allow-list and says how
  many unsafe tags it removed. Schema JSON-LD is kept: a block in the
  editor, written again from its JSON so it can't carry markup.
- The server checks every save against the allow-list again
  (sanitize-html), so a request made without the editor can't store a
  script, a handler or a javascript: link.
- dompurify 3.4.16 and sanitize-html 2.18.0, pinned.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(content): use a sanitize-html that runs on Node 20

sanitize-html 2.18.0 needs Node 22.12 or later, and CI and the web
Docker image run Node 20, so yarn install refused it. 2.17.0 has no
engine limit and the same API; @types/sanitize-html 2.16.0 matches it
(both on htmlparser2 8).

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(content): show links in the editor as links, and open them

Links in a draft looked like the text around them (dark, barely
underlined), so a link just added seemed not to be there. They're blue
and underlined now. With the cursor in a link, the toolbar shows its
address, which opens it in a new tab; Cmd/Ctrl + click on it in the page
does too. A click alone still puts the cursor in it to edit. A path on
the brand's own site is shown, not opened: it has no address here.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* feat(content): add HTML blocks to a draft and copy its HTML in one click

On the user's request, as Webflow's custom code in rich text: as many
HTML blocks as a page needs, each carrying code as written (an embed, a
widget, a form), and one-click Copy HTML for a block or the whole page.

- An HTML block is added from the toolbar where the cursor is, and edited
  or removed from its own Edit. The editor shows its code as text; it
  never runs in Ansvisor. A draft stores it as UTF-8 base64 in an empty
  element, so no markup in it can act, and the server checks each one
  decodes and fits 50,000 characters.
- Copy HTML (header) puts the page on the clipboard as HTML to publish:
  each block as its own code, schema JSON-LD as its script. A block's
  own Copy HTML copies just its code. A refused Clipboard API falls back
  to the browser's copy command.
- The HTML view shows each block between <!-- html-block --> markers,
  kept as written; code typed between new markers becomes a block. A
  paste from elsewhere can't bring blocks in.
- The link, image and block dialogs focus their field when they open.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* test(content): measure ranges in jsdom for the editor's scroll

Inserting an HTML block scrolls the cursor into view, and ProseMirror
measures a Range to do it. jsdom's ranges have no getClientRects, so on
CI's Node 20 the measuring threw on a timer after the test and failed the
run as an unhandled error. The editor test gives ranges an empty
measurement, as jsdom has no layout anyway.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* chore(content): renumber the content drafts migration to 00204

#1257 took 00203 (citation gap rollups) on main, so the content drafts
table moves to the next free number. The file is otherwise unchanged;
schema.sql is rebuilt.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(content): save only over the page the editor saw, and never mark unsaved text ready

- saveContentDraft takes the version the editor last saw (updated_at and
  a SHA-256 of the page) and writes with .eq('updated_at', base). When
  another write came first, the page tells which kind: the same page means
  only other columns changed (Mark ready, the type), and the save goes over
  them; another page comes back `conflict` and nothing is overwritten. The
  header then says "Changed elsewhere · Reload" and autosave stops.
- Results are typed ({ ok: false, reason }) since Next.js hides server
  action messages in production: a page over 500,000 characters says "Too
  long to save" (and isn't sent at all), not "Couldn't save · Retry".
- Mark ready settles first: it waits for a save under way and isn't run
  when the page can't be saved.
- Nit: the title limit comes from 00204.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(content): find JSON-LD scripts with a linear scan, not a backtracking regex

The JSON-LD pattern had two [^>]* runs around \btype, so input with many
unclosed `<script type=application/ld+json ` tags took cubic time (about
16 s at 33k characters), and the server runs it on every save. It is now
a scan (indexOf for the tag, its end and </script>, the type tested on the
tag alone), linear in the length; a 500,000-character page built to
backtrack takes milliseconds. The paste cleaner uses the same scan.

sanitize-draft-html.ts now says stored HTML is untrusted: a member can
write body_html through PostgREST past this check, so readers parse or
clean it again.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(content): keep Mark ready off while the page can't be saved

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(content): find JSON-LD scripts by ASCII lower case, so a Turkish İ before them keeps them

`toLowerCase()` turns 'İ' into two UTF-16 units, so every İ before a JSON-LD
script shifted the positions taken from the lower-cased copy: the schema was
dropped on save, with as many characters of the text after it. Only ASCII
letters are lower-cased now, which keeps the length and stays linear.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* test(content): read the Turkish İ case through extractKeptBlocks

The test merged up from #1259 used extractJsonLd, which this branch
replaces with extractKeptBlocks.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Cihan <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Gökhan Geyik <[email protected]>
gkhngyk added a commit that referenced this pull request Oct 21, 2026
…es to a draft (#1261)

* feat(content): add the Content group and its Library of drafts

Content Editor CE-001 and CE-002. Content becomes a group in the sidebar,
like Prompts: Opportunities keeps /dashboard/content, and the new Library
(/dashboard/content/library) lists every page a person or the Content Agent
writes or changes in Ansvisor, by status (Drafts, In review, Ready,
Published), with its page type, method, author and AEO score.

- Migration 00203: content_drafts with member RLS (select, insert, update;
  no delete yet). A guard trigger keeps author, created_by, brand and the
  scores server-side, so a member can't pass a draft off as the agent's.
- Sidebar: each submenu remembers its own open state (Prompts keeps its
  key), and only the longest matching entry is current, so Opportunities
  isn't marked on the Library.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* feat(content): write and save drafts in the content editor

Content Editor CE-004, stacked on the Library (#1256). A draft opens at
/dashboard/content/editor/{id}: a header with its title, page type,
method and status, whether it's saved, and Mark ready; the toolbar (H1 H2
H3, bold, italic, link, lists, quote, table with add row / add column /
delete, image by address) with the length counter against the page
type's target; and the page as rich text (TipTap 3.31.4, pinned).

- Autosave after a pause in typing; saves never overlap, a failed one
  keeps the text and offers Retry, and leaving with unsaved text asks.
  The draft's title is its first H1.
- saveContentDraft validates the body (500k chars) and title and says
  when RLS matched no row; markContentDraftReady moves draft or in review
  to ready only.
- Links: http(s), mailto or a path on the brand's site; javascript:,
  //host and /\host are refused, in the dialog and when content loads.
- The Library gets New content (a blank draft, until the Start screen),
  and its entry stays current while a draft is open in the editor.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* feat(content): clean pasted content and edit a draft as HTML

Content Editor CE-005 and CE-020, stacked on the editor core (#1258).

- Paste: from Word, Google Docs, Notion or a web page, headings (H4-H6
  as H3), paragraphs, bold, italic, lists, links, tables and quotes stay;
  fonts, colors, classes, inline styles, scripts, iframes and utm_*,
  gclid, fbclid on links go. Google Docs' styled bold and italic become
  strong and em first. Pasted images wait for attachments. Cmd/Ctrl +
  Shift + V pastes plain text.
- </> HTML shows the page's HTML as a draft stores it, one block per
  line; Back to visual cleans it with the same allow-list and says how
  many unsafe tags it removed. Schema JSON-LD is kept: a block in the
  editor, written again from its JSON so it can't carry markup.
- The server checks every save against the allow-list again
  (sanitize-html), so a request made without the editor can't store a
  script, a handler or a javascript: link.
- dompurify 3.4.16 and sanitize-html 2.18.0, pinned.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(content): use a sanitize-html that runs on Node 20

sanitize-html 2.18.0 needs Node 22.12 or later, and CI and the web
Docker image run Node 20, so yarn install refused it. 2.17.0 has no
engine limit and the same API; @types/sanitize-html 2.16.0 matches it
(both on htmlparser2 8).

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(content): show links in the editor as links, and open them

Links in a draft looked like the text around them (dark, barely
underlined), so a link just added seemed not to be there. They're blue
and underlined now. With the cursor in a link, the toolbar shows its
address, which opens it in a new tab; Cmd/Ctrl + click on it in the page
does too. A click alone still puts the cursor in it to edit. A path on
the brand's own site is shown, not opened: it has no address here.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* feat(content): add HTML blocks to a draft and copy its HTML in one click

On the user's request, as Webflow's custom code in rich text: as many
HTML blocks as a page needs, each carrying code as written (an embed, a
widget, a form), and one-click Copy HTML for a block or the whole page.

- An HTML block is added from the toolbar where the cursor is, and edited
  or removed from its own Edit. The editor shows its code as text; it
  never runs in Ansvisor. A draft stores it as UTF-8 base64 in an empty
  element, so no markup in it can act, and the server checks each one
  decodes and fits 50,000 characters.
- Copy HTML (header) puts the page on the clipboard as HTML to publish:
  each block as its own code, schema JSON-LD as its script. A block's
  own Copy HTML copies just its code. A refused Clipboard API falls back
  to the browser's copy command.
- The HTML view shows each block between <!-- html-block --> markers,
  kept as written; code typed between new markers becomes a block. A
  paste from elsewhere can't bring blocks in.
- The link, image and block dialogs focus their field when they open.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* test(content): measure ranges in jsdom for the editor's scroll

Inserting an HTML block scrolls the cursor into view, and ProseMirror
measures a Range to do it. jsdom's ranges have no getClientRects, so on
CI's Node 20 the measuring threw on a timer after the test and failed the
run as an unhandled error. The editor test gives ranges an empty
measurement, as jsdom has no layout anyway.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* feat(content): attach spreadsheets, Word, Markdown, HTML and text files to a draft

A paperclip tool and drag-and-drop on the editor (CE-006). Excel and CSV
open a preview (sheet picker, first 10 rows, header row) and insert as a
table, asking first above 200 rows. Word (.docx, via mammoth), Markdown,
HTML and plain text import as content through the same cleaning as a
paste. Images, old .xls/.doc and files over 10 MB get a clear message.
Imports land under a table when the cursor is inside one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* chore(content): renumber the content drafts migration to 00204

#1257 took 00203 (citation gap rollups) on main, so the content drafts
table moves to the next free number. The file is otherwise unchanged;
schema.sql is rebuilt.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(content): save only over the page the editor saw, and never mark unsaved text ready

- saveContentDraft takes the version the editor last saw (updated_at and
  a SHA-256 of the page) and writes with .eq('updated_at', base). When
  another write came first, the page tells which kind: the same page means
  only other columns changed (Mark ready, the type), and the save goes over
  them; another page comes back `conflict` and nothing is overwritten. The
  header then says "Changed elsewhere · Reload" and autosave stops.
- Results are typed ({ ok: false, reason }) since Next.js hides server
  action messages in production: a page over 500,000 characters says "Too
  long to save" (and isn't sent at all), not "Couldn't save · Retry".
- Mark ready settles first: it waits for a save under way and isn't run
  when the page can't be saved.
- Nit: the title limit comes from 00204.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(content): find JSON-LD scripts with a linear scan, not a backtracking regex

The JSON-LD pattern had two [^>]* runs around \btype, so input with many
unclosed `<script type=application/ld+json ` tags took cubic time (about
16 s at 33k characters), and the server runs it on every save. It is now
a scan (indexOf for the tag, its end and </script>, the type tested on the
tag alone), linear in the length; a 500,000-character page built to
backtrack takes milliseconds. The paste cleaner uses the same scan.

sanitize-draft-html.ts now says stored HTML is untrusted: a member can
write body_html through PostgREST past this check, so readers parse or
clean it again.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(content): insert only the sheet rows that fit in the page's size limit

"Insert all N rows" had no upper bound: the editor writes each cell as
<td colspan="1" rowspan="1"><p>…</p></td>, so a wide sheet passed the
500,000-character body limit after a few hundred rows, and from then on
nothing typed was saved. The preview now measures the table as the editor
saves it against the room left in the page (rowsThatFit): a table that
doesn't fit offers "Insert the first N rows" after saying why, and one with
no room at all can't be inserted.

The sheet picker passes `items` to Select, as the repo's Base UI selects do.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(content): keep Mark ready off while the page can't be saved

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(content): find JSON-LD scripts by ASCII lower case, so a Turkish İ before them keeps them

`toLowerCase()` turns 'İ' into two UTF-16 units, so every İ before a JSON-LD
script shifted the positions taken from the lower-cased copy: the schema was
dropped on save, with as many characters of the text after it. Only ASCII
letters are lower-cased now, which keeps the length and stays linear.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* test(content): read the Turkish İ case through extractKeptBlocks

The test merged up from #1259 used extractJsonLd, which this branch
replaces with extractKeptBlocks.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Cihan <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Gökhan Geyik <[email protected]>
gkhngyk added a commit that referenced this pull request Oct 21, 2026
…pe (#1262)

* feat(content): add the Content group and its Library of drafts

Content Editor CE-001 and CE-002. Content becomes a group in the sidebar,
like Prompts: Opportunities keeps /dashboard/content, and the new Library
(/dashboard/content/library) lists every page a person or the Content Agent
writes or changes in Ansvisor, by status (Drafts, In review, Ready,
Published), with its page type, method, author and AEO score.

- Migration 00203: content_drafts with member RLS (select, insert, update;
  no delete yet). A guard trigger keeps author, created_by, brand and the
  scores server-side, so a member can't pass a draft off as the agent's.
- Sidebar: each submenu remembers its own open state (Prompts keeps its
  key), and only the longest matching entry is current, so Opportunities
  isn't marked on the Library.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* feat(content): write and save drafts in the content editor

Content Editor CE-004, stacked on the Library (#1256). A draft opens at
/dashboard/content/editor/{id}: a header with its title, page type,
method and status, whether it's saved, and Mark ready; the toolbar (H1 H2
H3, bold, italic, link, lists, quote, table with add row / add column /
delete, image by address) with the length counter against the page
type's target; and the page as rich text (TipTap 3.31.4, pinned).

- Autosave after a pause in typing; saves never overlap, a failed one
  keeps the text and offers Retry, and leaving with unsaved text asks.
  The draft's title is its first H1.
- saveContentDraft validates the body (500k chars) and title and says
  when RLS matched no row; markContentDraftReady moves draft or in review
  to ready only.
- Links: http(s), mailto or a path on the brand's site; javascript:,
  //host and /\host are refused, in the dialog and when content loads.
- The Library gets New content (a blank draft, until the Start screen),
  and its entry stays current while a draft is open in the editor.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* feat(content): clean pasted content and edit a draft as HTML

Content Editor CE-005 and CE-020, stacked on the editor core (#1258).

- Paste: from Word, Google Docs, Notion or a web page, headings (H4-H6
  as H3), paragraphs, bold, italic, lists, links, tables and quotes stay;
  fonts, colors, classes, inline styles, scripts, iframes and utm_*,
  gclid, fbclid on links go. Google Docs' styled bold and italic become
  strong and em first. Pasted images wait for attachments. Cmd/Ctrl +
  Shift + V pastes plain text.
- </> HTML shows the page's HTML as a draft stores it, one block per
  line; Back to visual cleans it with the same allow-list and says how
  many unsafe tags it removed. Schema JSON-LD is kept: a block in the
  editor, written again from its JSON so it can't carry markup.
- The server checks every save against the allow-list again
  (sanitize-html), so a request made without the editor can't store a
  script, a handler or a javascript: link.
- dompurify 3.4.16 and sanitize-html 2.18.0, pinned.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(content): use a sanitize-html that runs on Node 20

sanitize-html 2.18.0 needs Node 22.12 or later, and CI and the web
Docker image run Node 20, so yarn install refused it. 2.17.0 has no
engine limit and the same API; @types/sanitize-html 2.16.0 matches it
(both on htmlparser2 8).

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(content): show links in the editor as links, and open them

Links in a draft looked like the text around them (dark, barely
underlined), so a link just added seemed not to be there. They're blue
and underlined now. With the cursor in a link, the toolbar shows its
address, which opens it in a new tab; Cmd/Ctrl + click on it in the page
does too. A click alone still puts the cursor in it to edit. A path on
the brand's own site is shown, not opened: it has no address here.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* feat(content): add HTML blocks to a draft and copy its HTML in one click

On the user's request, as Webflow's custom code in rich text: as many
HTML blocks as a page needs, each carrying code as written (an embed, a
widget, a form), and one-click Copy HTML for a block or the whole page.

- An HTML block is added from the toolbar where the cursor is, and edited
  or removed from its own Edit. The editor shows its code as text; it
  never runs in Ansvisor. A draft stores it as UTF-8 base64 in an empty
  element, so no markup in it can act, and the server checks each one
  decodes and fits 50,000 characters.
- Copy HTML (header) puts the page on the clipboard as HTML to publish:
  each block as its own code, schema JSON-LD as its script. A block's
  own Copy HTML copies just its code. A refused Clipboard API falls back
  to the browser's copy command.
- The HTML view shows each block between <!-- html-block --> markers,
  kept as written; code typed between new markers becomes a block. A
  paste from elsewhere can't bring blocks in.
- The link, image and block dialogs focus their field when they open.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* test(content): measure ranges in jsdom for the editor's scroll

Inserting an HTML block scrolls the cursor into view, and ProseMirror
measures a Range to do it. jsdom's ranges have no getClientRects, so on
CI's Node 20 the measuring threw on a timer after the test and failed the
run as an unhandled error. The editor test gives ranges an empty
measurement, as jsdom has no layout anyway.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* feat(content): attach spreadsheets, Word, Markdown, HTML and text files to a draft

A paperclip tool and drag-and-drop on the editor (CE-006). Excel and CSV
open a preview (sheet picker, first 10 rows, header row) and insert as a
table, asking first above 200 rows. Word (.docx, via mammoth), Markdown,
HTML and plain text import as content through the same cleaning as a
paste. Images, old .xls/.doc and files over 10 MB get a clear message.
Imports land under a table when the cursor is inside one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* feat(content): detect a draft's page type and set the brand's site type

Page types follow the kind of site (CE-023). Brand settings get a Site
type (B2B SaaS, E-commerce, B2C; guessed from Shopping mode and the
industry until picked), stored in brands.site_type (migration 00205). A
draft's page type is detected from its URL, JSON-LD schema and title,
and kept in step as the page changes, until the person picks one from
the editor header's page type menu (content_drafts.page_type_source).

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* chore(content): renumber the content drafts migration to 00204

#1257 took 00203 (citation gap rollups) on main, so the content drafts
table moves to the next free number. The file is otherwise unchanged;
schema.sql is rebuilt.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(content): save only over the page the editor saw, and never mark unsaved text ready

- saveContentDraft takes the version the editor last saw (updated_at and
  a SHA-256 of the page) and writes with .eq('updated_at', base). When
  another write came first, the page tells which kind: the same page means
  only other columns changed (Mark ready, the type), and the save goes over
  them; another page comes back `conflict` and nothing is overwritten. The
  header then says "Changed elsewhere · Reload" and autosave stops.
- Results are typed ({ ok: false, reason }) since Next.js hides server
  action messages in production: a page over 500,000 characters says "Too
  long to save" (and isn't sent at all), not "Couldn't save · Retry".
- Mark ready settles first: it waits for a save under way and isn't run
  when the page can't be saved.
- Nit: the title limit comes from 00204.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(content): find JSON-LD scripts with a linear scan, not a backtracking regex

The JSON-LD pattern had two [^>]* runs around \btype, so input with many
unclosed `<script type=application/ld+json ` tags took cubic time (about
16 s at 33k characters), and the server runs it on every save. It is now
a scan (indexOf for the tag, its end and </script>, the type tested on the
tag alone), linear in the length; a 500,000-character page built to
backtrack takes milliseconds. The paste cleaner uses the same scan.

sanitize-draft-html.ts now says stored HTML is untrusted: a member can
write body_html through PostgREST past this check, so readers parse or
clean it again.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(content): insert only the sheet rows that fit in the page's size limit

"Insert all N rows" had no upper bound: the editor writes each cell as
<td colspan="1" rowspan="1"><p>…</p></td>, so a wide sheet passed the
500,000-character body limit after a few hundred rows, and from then on
nothing typed was saved. The preview now measures the table as the editor
saves it against the room left in the page (rowsThatFit): a table that
doesn't fit offers "Insert the first N rows" after saying why, and one with
no room at all can't be inserted.

The sheet picker passes `items` to Select, as the repo's Base UI selects do.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(content): keep Mark ready off while the page can't be saved

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(content): find JSON-LD scripts by ASCII lower case, so a Turkish İ before them keeps them

`toLowerCase()` turns 'İ' into two UTF-16 units, so every İ before a JSON-LD
script shifted the positions taken from the lower-cased copy: the schema was
dropped on save, with as many characters of the text after it. Only ASCII
letters are lower-cased now, which keeps the length and stays linear.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* test(content): read the Turkish İ case through extractKeptBlocks

The test merged up from #1259 used extractJsonLd, which this branch
replaces with extractKeptBlocks.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Cihan <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Gökhan Geyik <[email protected]>
gkhngyk added a commit that referenced this pull request Oct 21, 2026