Skip to content

feat(content): add the Content group and its Library of drafts - #1256

Merged
gkhngyk merged 3 commits into
ansvisor:mainfrom
cihan-gyk:feature/content-library
Oct 21, 2026
Merged

gkhngyk merged 3 commits into
ansvisor:mainfrom
cihan-gyk:feature/content-library

Conversation

@cihan-gyk

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

Copy link
Copy Markdown
Collaborator

Summary

Content Editor (spec v2), PR 1 of the approved plan: CE-001 (navigation) and CE-002 (Library).

Sidebar (CE-001). In Execution, Content is now a group, like Prompts: clicking it only opens or closes it. Its entries are Opportunities (today's page, unchanged, still /dashboard/content and /dashboard/content/{id}) and Library (/dashboard/content/library). Plan joins the group with CE-014.

  • Each submenu remembers its own open state (ansvisor.sidebar.<name>Open). Prompts keeps its existing key, so nobody's choice resets.
  • activeChildHref marks only the longest matching entry. Opportunities' route is the start of the Library's, so both would otherwise be marked on the Library. Desktop and mobile menus use it.

Library (CE-002). "Everything you and the agents wrote or changed."

  • Tabs: All · Drafts · In review · Ready · Published, with counts. The tab is in the URL (?tab=in_review), so a later "Library › In review" link can open it.
  • Columns: Content · Type · Method (Create / Optimize) · By (You, a teammate, or Content Agent) · Status · AEO score (before → after).
  • Empty, loading and error (retry) states. Status and method badges use Action Center's colors, with dark variants.
  • A row links to the editor route /dashboard/content/editor/{id}, which comes with the next PR (CE-004, editor core), together with the "New content" button. Until then nothing in the app creates drafts, so the Library is empty for everyone.

Migration 00204: content_drafts

  • Columns: title, page_type (a key from the web's list, not an enum), method, status (draft / in_review / ready / published), body_html, author (user / agent), created_by, score_before / score_after, timestamps.
  • RLS: members of the brand's organization can select, insert and update, as with reports and site audits. Insert requires author = 'user' and created_by = auth.uid(); the agent writes through the service role. Update checks the organization in both using and with check. There is no delete policy yet, because nothing in the app deletes a draft.
  • Guard trigger guard_content_draft_server_columns, modelled on 00062: author, created_by, brand_id, created_at and the scores can only be set server-side. A member can't pass a draft off as the agent's or score it.
  • Applied on the dev DB. In a rolled-back transaction as the demo user:
    • Allowed: inserting and updating their own draft; changing an agent draft's status.
    • Denied (42501): inserting as the agent, without created_by, with a score, or into another organization; changing the author or a score; moving a draft to another organization.
    • Other organization: its draft is invisible, and an update of it touches 0 rows.
    • Delete touches 0 rows.

Names for teammates come from readAssignees, the same reader Action Center uses, because a profile is only readable by its owner.

Related issue

Content Editor spec v2 (CE-001, CE-002). No GitHub issue.

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. Apply supabase/migrations/00204_content_drafts.sql.
  2. Open the dashboard. Under Execution, Content opens and closes, and lists Opportunities and Library. Prompts opens and closes on its own.
  3. Opportunities works exactly as before, and only it is marked.
  4. Open Library: with no drafts it shows the empty state. To see rows, insert a few content_drafts rows for your brand with the service role, using different statuses, an author = 'agent' row and scores. Then:
    • check the tab counts;
    • switch tabs (the URL gets ?tab=…) and reload, which keeps the tab;
    • check that "By" shows You, a teammate's name or e-mail, or Content Agent;
    • check the AEO score as 62 → 78, 54 or —.
  5. Try light and dark mode, and a 1024px window.

Tests: src/lib/content/drafts.test.ts, content/library/page.test.tsx, and activeChildHref / Content group cases in src/lib/dashboard-navigation.test.ts.

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,910 tests) and yarn build pass; supabase/schema.sql was rebuilt with build-schema.sh.

🤖 Generated with Claude Code

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]>
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk this is the first PR of the Content Editor spec v2 (CE-001 Content group in the sidebar, CE-002 Library). Migration 00203 adds content_drafts: members read and write their brand's drafts, as with reports and site audits. Who wrote a draft and its scores can only be set server-side (guard trigger, as 00062). There's no delete policy yet. The editor (CE-004) comes next and makes the Library's rows open; until then nothing creates drafts. Could you take a look?

cihan-gyk and others added 2 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]>
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk #1257 took 00203 on main, so I merged main into this PR and renumbered the content drafts migration to 00204_content_drafts.sql (a67a059; file otherwise unchanged, schema.sql rebuilt). Merged forward through #1258 → #1259 → #1260 → #1261 → #1262 with no other conflicts; web (1,915 / 2,046 tests + build at the bottom / top) and server (1,919 tests) checks pass. #1262 keeps 00205.

@gkhngyk

gkhngyk commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

@cihan-gyk I reviewed the whole Content stack (#1256 … #1308) on top of current main. Each PR has its own review.

The top of the stack (#1308) merges cleanly with main and passes:

  • web: 2,326 tests, tsc, lint and Prettier;
  • server: tests, lint and format;
  • schema.sql rebuilds unchanged.

We'll hold the merge until the fix-before-merge items are in. A merge deploys at once, and #1256 alone would put an empty Content group in the nav.

The ones that matter most

Patterns that repeat across the stack (fix once)

Fix each medium-or-higher item in the PR where it starts, and merge it up the chain. The low ones can go in one follow-up PR on top.

Deploy order once it's ready

  1. Apply 00204 … 00220 in order before the web merges.
  2. feat(content): import a brand page by its URL into a draft #1288, feat(content): choose who does the page in Start, and send it to the team #1289 and feat(content): plan the page in the editor's Brief tab and rebuild it from the sources kept #1293 also need the server deployed before the web.

Main has moved (#1296–#1299 are merged), but the stack still merges cleanly, and main still ends at 00203.

@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.

Looks good. RLS on content_drafts matches the reports / site audits pattern, the insert policy pins author = 'user' and created_by = auth.uid(), the update policy checks the organization on both sides, and the guard trigger keeps author, brand and scores server-side. One small thing:

  1. Tab counts stop being right past 500 drafts (web/src/lib/actions/content-drafts.ts:21, :35). libraryTabCounts counts the rows getContentDrafts returned, and that read is capped at LIBRARY_LIMIT = 500. Once agents write drafts, a busy brand gets there, and then "All" reads 500 and the status tabs undercount. Either count per status with a separate select('status', { count: 'exact', head: true }) per tab (or a small grouped RPC), or label the cap ("latest 500").

Approving this PR. We'll hold the merge until the stack above it is ready; see the overview comment.

@gkhngyk

gkhngyk commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

@cihan-gyk we're merging the whole stack (#1256 … #1310) now. Please don't push to its branches until it's done; I'll say here when it is. I'm adding the one-line İ fix to #1259 myself. The other open items (#1287, #1292, #1302, #1307, #1308, #1309, #1310) go into your follow-up PR on top of main, with new migration files: 00204–00221 will be applied on production and shouldn't be edited after that.

@gkhngyk
gkhngyk merged commit 604455e into ansvisor:main Oct 21, 2026
5 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
* 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]>

* feat(content): start a draft from New content and Start

New content (CE-003) starts a page from an open Content Opportunity, a
tracked prompt or a topic, or brings content to optimize by pasting
(cleaned like the editor's paste) or uploading a Word, Markdown, HTML or
text file. Start (CE-015) offers the site type's page types, the one the
title or content shows picked first, and opens the editor.

The draft keeps the opportunity it came from and the prompts it should
answer (migration 00206: opportunity_id, prompt_ids, and a trigger that
refuses another brand's opportunity or prompts). A new page starts with
its title as the H1; content to optimize is checked against the draft
allow-list. The editor's file readers move to lib/content/import-document
so both use them.

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): keep drafts writable after a prompt is deleted, and store Start's guess as detected

- check_content_draft_links checks only the prompt ids a write adds, and a
  statement-level trigger on prompts removes deleted ids from every draft
  (prompt_ids has no foreign key), so the Brief tab's append no longer
  fails with 23514 once a tracked prompt is gone (00206).
- Start sends whether the person picked the type: a pick is stored as
  'user', Start's guess (or the Blog post fallback) as 'detected', so it
  follows the page. A save that finds no type keeps the one the draft had.

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
…1287)

* 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]>

* feat(content): start a draft from New content and Start

New content (CE-003) starts a page from an open Content Opportunity, a
tracked prompt or a topic, or brings content to optimize by pasting
(cleaned like the editor's paste) or uploading a Word, Markdown, HTML or
text file. Start (CE-015) offers the site type's page types, the one the
title or content shows picked first, and opens the editor.

The draft keeps the opportunity it came from and the prompts it should
answer (migration 00206: opportunity_id, prompt_ids, and a trigger that
refuses another brand's opportunity or prompts). A new page starts with
its title as the H1; content to optimize is checked against the draft
allow-list. The editor's file readers move to lib/content/import-document
so both use them.

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

* feat(content): check for overlapping pages before a draft is started

Start (CE-015) now shows which of the brand's own pages AI already cites
for the prompts a new page would answer: prompts in common, citations in
the last 30 days and the Google position from Search Console, through a
new security-definer RPC, content_page_overlap (migration 00207). It
recommends one option: optimize a page that already answers half the
prompts or more, otherwise create the page and link the others to it,
or say it is safe to create.

Picking an existing page makes the draft that page's: optimize, its
title as H1, the page type read from its address, and its URL kept in
content_drafts.source_url, which the editor shows beside the page type.

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): keep drafts writable after a prompt is deleted, and store Start's guess as detected

- check_content_draft_links checks only the prompt ids a write adds, and a
  statement-level trigger on prompts removes deleted ids from every draft
  (prompt_ids has no foreign key), so the Brief tab's append no longer
  fails with 23514 once a tracked prompt is gone (00206).
- Start sends whether the person picked the type: a pick is stored as
  'user', Start's guess (or the Blog post fallback) as 'detected', so it
  follows the page. A save that finds no type keeps the one the draft had.

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

* fix(content): read the overlap check's answers from the cover index, and leave ChatGPT Shopping out

content_page_overlap took 8.9 s cold on the brand with the most citations,
mostly reading prompt_results' wide rows one citation at a time for
prompt_id. The window's answers to the prompts now come from
idx_prompt_results_brand_created_cover alone (brand_id, created_at, with
id and prompt_id included), materialized and hash-joined to the brand's
citations and its own pages. ChatGPT Shopping answers are left out, as
Impact does, so Start and Impact count the same citations. The function is
planned for each call's values (plan_cache_mode = force_custom_plan).

Checked against the old version on a fixture: same pages and prompts in
common; the only difference is the Shopping citations.

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]>