Skip to content

Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.

feat(content): export the page ready to paste, with its page fields (CE-057) (#1423)

Cihanb05f0d31300b
diff --git a/web/messages/en.json b/web/messages/en.json
index bf1c3869a..9914f7c15 100644
--- a/web/messages/en.json
+++ b/web/messages/en.json
@@ -4149,6 +4149,103 @@
},
"qualityChip": "Quality {score}",
"qualityChipHint": "An Ansvisor estimate of this page's quality. Open Quality.",
+ "exportDialog": {
+ "title": "Export",
+ "untitled": "Untitled",
+ "destination": "Where it goes",
+ "destinations": {
+ "webflow": "Webflow",
+ "wordpress": "WordPress",
+ "shopify": "Shopify",
+ "docs": "Google Docs",
+ "docx": "Word",
+ "md": "Markdown",
+ "html": "HTML"
+ },
+ "hints": {
+ "webflow": "Copy, then paste into the post's Rich Text field: headings, links, lists and images keep their format. The H1 is left out: the title goes in the Name field.",
+ "wordpress": "Copy, then paste into the block editor: each heading, paragraph and list becomes its own block. The H1 is left out: the title goes in the title field.",
+ "shopify": "Copy, then paste into the blog post or description editor. The H1 is left out: the title goes in the title field.",
+ "docs": "Copy, then paste into a new Google Doc.",
+ "docx": "A Word file, with the page fields in a table on page 1 to delete or keep.",
+ "md": "A Markdown file, with the page fields as front matter (the title there, not as an H1).",
+ "html": "The page's HTML only, for an HTML field or a code view. The page fields are below."
+ },
+ "placeholders": {
+ "open": "{count, plural, one {# placeholder is} other {# placeholders are}} still open. Exports never contain [ ] text.",
+ "fill": "Fill now",
+ "skip": "Export without them",
+ "left": "{count, plural, one {The sentence with the placeholder is} other {The sentences with a placeholder are}} left out of this export.",
+ "undo": "Put them back",
+ "headings": "{count, plural, one {# placeholder is} other {# placeholders are}} in the title or a heading: fill {count, plural, one {it} other {them}} first."
+ },
+ "checks": {
+ "label": "Content · ready to paste",
+ "ownCode": {
+ "title": "HTML blocks keep their own code",
+ "detail": "{count, plural, one {# element keeps} other {# elements keep}} a class, a style or a script, and goes out as written."
+ },
+ "clean": {
+ "title": "Clean HTML",
+ "detail": "Headings, lists, tables and FAQ. No editor marks, Page info strip or underlines."
+ },
+ "links": {
+ "ok": "{count, plural, one {# link answers} other {All # links answer}}",
+ "okDetail": "Each was read in the last day.",
+ "broken": "{broken} of {count} links don't answer",
+ "brokenDetail": "Fix or remove them first: Quality shows which.",
+ "reading": "{count, plural, one {# link} other {# links}}, still being read",
+ "readingDetail": "Their read comes a few seconds after the page is saved.",
+ "unread": "{unread} of {count} links couldn't be read",
+ "unreadDetail": "Open them yourself before you publish."
+ },
+ "images": {
+ "ok": "{count, plural, one {# image, with alt text} other {All # images have alt text}}",
+ "okDetail": "Screen readers and search engines read what each shows.",
+ "missing": "{missing} of {count} images have no alt text",
+ "missingDetail": "Add it in the HTML view: a few words on what the image shows."
+ }
+ },
+ "states": {
+ "ok": "done",
+ "missing": "missing",
+ "check": "check it"
+ },
+ "fields": {
+ "label": "Page fields",
+ "where": "for your CMS's own fields, not the content",
+ "note": "Page fields: these go in your CMS's own fields, not into the content. Delete this table or keep it.",
+ "metaTitle": "Meta title",
+ "metaDescription": "Meta description",
+ "slug": "Slug",
+ "author": "Author",
+ "authorValue": "{name} → your CMS author field",
+ "featuredImage": "Featured image",
+ "noImage": "None in the page",
+ "noSlug": "None from the title: set it in your CMS",
+ "noAuthor": "Not read yet: set it in your CMS",
+ "copy": "Copy",
+ "copyLabel": "Copy {field}",
+ "copied": "{field} copied"
+ },
+ "actions": {
+ "webflow": "Copy for Webflow",
+ "wordpress": "Copy for WordPress",
+ "shopify": "Copy for Shopify",
+ "docs": "Copy for Google Docs",
+ "docx": "Download Word",
+ "md": "Download Markdown",
+ "html": "Copy HTML",
+ "downloadHtml": "Download HTML"
+ },
+ "copied": "Copied for {destination}: paste it in.",
+ "copiedHtml": "HTML copied.",
+ "copiedAsHtml": "Copied as HTML: paste it into the editor's HTML or code view.",
+ "inHtmlBlock": "The placeholders are in an HTML block: open it with {html} to fill them.",
+ "copyFailed": "Couldn't copy. Open the HTML view and copy it from there.",
+ "failed": "Couldn't export the page. Try again.",
+ "cancel": "Cancel"
+ },
"beforePublish": {
"title": "Before you publish",
"warning": "Trust {trust}, below 60. {reason}. You can mark it ready, but {count, plural, one {fixing it} other {fixing them}} first raises Trust to about {raised}.",
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsx
index f380fd015..d1911cecc 100644
--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsx
+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsx
@@ -9,7 +9,7 @@ import StarterKit from '@tiptap/starter-kit';
import { TableKit } from '@tiptap/extension-table';
import Image from '@tiptap/extension-image';
import { CharacterCount, Placeholder } from '@tiptap/extensions';
-import { ArrowLeft, Bot, Check, Copy, PanelRight, Target, X } from 'lucide-react';
+import { ArrowLeft, Bot, Check, Copy, Download, PanelRight, Target, X } from 'lucide-react';
import { Link } from '@/i18n/navigation';
import { Button } from '@/components/ui/button';
import { buttonVariants } from '@/components/ui/button-variants';
@@ -75,7 +75,8 @@ import {
import { PAGE_READ_DELAY_MS, draftTitleFromDoc, safeLinkHref } from '@/lib/content/editor';
import { JsonLd } from '@/lib/content/json-ld-node';
import { firstImage } from '@/lib/content/page-schema';
-import { PLACEHOLDER, pagePlaceholders } from '@/lib/content/placeholders';
+import { PLACEHOLDER, pagePlaceholders, placeholderIn } from '@/lib/content/placeholders';
+import { exportPlaceholders, pasteChecks } from '@/lib/content/ready-to-publish';
import { detectPageType } from '@/lib/content/page-types';
import { BAND_CLASSES, qualityAverage, qualityBand, qualityPhase } from '@/lib/content/quality';
import { eeat, linksToCheck, trust, trustBlocks } from '@/lib/content/quality-checks';
@@ -102,6 +103,7 @@ import {
lockedPassageCount,
type HeldEdit,
} from './_cited-passages';
+import { ExportDialog, type ExportDestination } from './_export-dialog';
import { ExportMenu } from './_export-menu';
import { sourceDetail } from './_sources-tab';
import { HistoryMenu } from './_history-menu';
@@ -935,11 +937,11 @@ export function DraftEditor({
/**
* The page's first placeholder (CE-044), selected to type over: in order,
- * across the marks in its line, its block flashed as Show's are.
+ * across the marks in its line, its block flashed as Show's are. A
+ * heading's are the agent's own forms only (AGENT_PLACEHOLDER).
*/
- const showFirstPlaceholder = () => {
- if (!editor) return;
- const one = new RegExp(PLACEHOLDER.source, 'iu');
+ const showFirstPlaceholder = (): boolean => {
+ if (!editor) return false;
let found: { from: number; to: number; block: { from: number; to: number } } | null = null;
editor.state.doc.descendants((block, pos) => {
if (found) return false;
@@ -952,7 +954,7 @@ export function DraftEditor({
pieces.push({ at: text.length, pos: pos + 1 + offset });
text += child.text;
});
- const m = one.exec(text);
+ const m = new RegExp(placeholderIn(block.type.name === 'heading').source, 'iu').exec(text);
if (!m) return false;
const posOf = (i: number) => {
const piece = pieces.findLast((p) => p.at <= i)!;
@@ -965,7 +967,7 @@ export function DraftEditor({
};
return false;
});
- if (!found) return;
+ if (!found) return false;
const { from, to, block } = found;
editor
.chain()
@@ -977,6 +979,26 @@ export function DraftEditor({
setTimeout(() => {
if (!editor.isDestroyed) editor.view.dispatch(editor.state.tr.setMeta(flashKey, null));
}, 1200);
+ return true;
+ };
+
+ /**
+ * Fill now (Export, CE-057): the first placeholder, in the HTML view's
+ * text while it is open, else in the page; one only an HTML block holds is
+ * said to be there.
+ */
+ const htmlSourceRef = useRef<HTMLTextAreaElement>(null);
+ const fillFirstPlaceholder = () => {
+ if (htmlSource !== null) {
+ const m = new RegExp(PLACEHOLDER.source, 'iu').exec(htmlSource);
+ const area = htmlSourceRef.current;
+ if (m && area) {
+ area.focus();
+ area.setSelectionRange(m.index, m.index + m[0].length);
+ }
+ return;
+ }
+ if (!showFirstPlaceholder()) toast(t('exportDialog.inHtmlBlock', { html: '</>' }));
};
/**
@@ -1316,14 +1338,27 @@ export function DraftEditor({
}, [htmlEdited, htmlSource]);
// The page as HTML to publish; in the HTML view, the source on screen.
+ const pageToPublish = () =>
+ publishableHtml(
+ htmlSource !== null ? htmlFromSource(htmlSource) : (editor?.getHTML() ?? draft.bodyHtml),
+ );
+ // Export (CE-057), once Quality is live: where it goes, its checks and fields.
+ // The page as it was when Export opened: what it shows stays still while open.
+ const [exportOpen, setExportOpen] = useState<{
+ html: string;
+ destination?: ExportDestination;
+ } | null>(null);
+ const openExport = (destination?: ExportDestination) =>
+ setExportOpen({ html: pageToPublish(), destination });
const copyPageHtml = () => {
if (!editor) return;
- const html = htmlSource !== null ? htmlFromSource(htmlSource) : editor.getHTML();
- void copyToClipboard(
- publishableHtml(html),
- t('copyHtmlDone'),
- t('copyHtmlFailed', { html: '</> HTML' }),
- );
+ const html = pageToPublish();
+ // No copy carries a placeholder: Export says so, and offers to leave them out.
+ if (quality && exportPlaceholders(html) > 0) {
+ setExportOpen({ html, destination: 'html' });
+ return;
+ }
+ void copyToClipboard(html, t('copyHtmlDone'), t('copyHtmlFailed', { html: '</> HTML' }));
};
/** Export: the page as Copy HTML gives it, as a Word, HTML or Markdown file. */
@@ -1593,7 +1628,21 @@ export function DraftEditor({
<Copy className="h-3.5 w-3.5" />
<span className="hidden xl:inline">{t('copyHtml')}</span>
</Button>
- <ExportMenu disabled={!editor} onExport={(format) => void exportPage(format)} />
+ {quality ? (
+ <Button
+ variant="outline"
+ size="sm"
+ disabled={!editor}
+ title={t('export.hint')}
+ aria-label={t('export.button')}
+ onClick={() => openExport()}
+ >
+ <Download className="h-3.5 w-3.5" />
+ <span className="hidden sm:inline">{t('export.button')}</span>
+ </Button>
+ ) : (
+ <ExportMenu disabled={!editor} onExport={(format) => void exportPage(format)} />
+ )}
<Button
size="sm"
// A page that can't be saved can't be marked ready; the save state says why.
@@ -1810,6 +1859,7 @@ export function DraftEditor({
<div className="h-full overflow-y-auto">
{htmlSource !== null && (
<textarea
+ ref={htmlSourceRef}
value={htmlSource}
onChange={(e) => {
setHtmlSource(e.target.value);
@@ -1873,6 +1923,26 @@ export function DraftEditor({
)}
</div>
+ {quality && exportOpen && (
+ <ExportDialog
+ onClose={() => setExportOpen(null)}
+ title={title}
+ html={exportOpen.html}
+ isNew={draft.method === 'create'}
+ sourceUrl={draft.sourceUrl}
+ author={pageAuthor}
+ // Until the authors are read, who it is by isn't known.
+ teamByline={authorsRead ? t('author.organization', { brand: brandName }) : ''}
+ checks={pasteChecks(exportOpen.html, linkChecks)}
+ linksPending={linksPending}
+ initialDestination={exportOpen.destination}
+ onFillNow={() => {
+ setExportOpen(null);
+ fillFirstPlaceholder();
+ }}
+ />
+ )}
+
{quality && publishOpen && (
<BeforePublish
onClose={() => setPublishOpen(false)}
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_export-dialog.test.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_export-dialog.test.tsx
new file mode 100644
index 000000000..a071761ad
--- /dev/null
+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_export-dialog.test.tsx
@@ -0,0 +1,305 @@
+import { act } from 'react';
+import { createRoot, type Root } from 'react-dom/client';
+import { NextIntlClientProvider } from 'next-intl';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import messages from '../../../../../../../../messages/en.json';
+import type { ContentAuthor } from '@/lib/content/authors';
+import type { PasteChecks } from '@/lib/content/ready-to-publish';
+
+const toast = vi.hoisted(() => Object.assign(vi.fn(), { success: vi.fn(), error: vi.fn() }));
+vi.mock('sonner', () => ({ toast }));
+const saveBlob = vi.hoisted(() => vi.fn());
+vi.mock('@/lib/download', () => ({ saveBlob }));
+const draftExportBlob = vi.hoisted(() => vi.fn());
+vi.mock('@/lib/content/draft-export', async (importOriginal) => ({
+ ...(await importOriginal<typeof import('@/lib/content/draft-export')>()),
+ draftExportBlob,
+}));
+
+const { ExportDialog } = await import('./_export-dialog');
+
+/**
+ * Export (Quality spec §7b, CE-057): the page ready to paste or save for the
+ * destination picked, its placeholders first, its checks, its page fields.
+ */
+
+(globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT =
+ true;
+
+const HTML =
+ '<h1>Pour-over</h1><p>Use [figure, source] g of coffee. Brew it slowly.</p><p>See <a href="https://ok.example/">the guide</a>.</p>';
+const ada = { id: 'a1', name: 'Ada Lovelace' } as ContentAuthor;
+const checks: PasteChecks = {
+ links: { count: 1, read: 1, broken: 0 },
+ images: { count: 1, withoutAlt: 1 },
+ ownCode: 0,
+};
+
+// An in-memory localStorage: the test environment has none.
+const store = new Map<string, string>();
+Object.defineProperty(window, 'localStorage', {
+ configurable: true,
+ value: {
+ getItem: (key: string) => store.get(key) ?? null,
+ setItem: (key: string, value: string) => void store.set(key, value),
+ removeItem: (key: string) => void store.delete(key),
+ clear: () => store.clear(),
+ },
+});
+
+let container: HTMLDivElement;
+let root: Root;
+const onFillNow = vi.fn();
+const writeText = vi.fn();
+const write = vi.fn();
+
+async function render(over: Partial<Parameters<typeof ExportDialog>[0]> = {}) {
+ await act(async () => {
+ root.render(
+ <NextIntlClientProvider locale="en" messages={messages}>
+ <ExportDialog
+ onClose={() => {}}
+ title="Pour-over"
+ html={HTML}
+ isNew
+ sourceUrl={null}
+ author={ada}
+ teamByline="Acme Team"
+ checks={checks}
+ linksPending={0}
+ onFillNow={onFillNow}
+ {...over}
+ />
+ </NextIntlClientProvider>,
+ );
+ });
+}
+const dialog = () => document.querySelector('[role="dialog"]')!;
+const button = (name: string) =>
+ [...dialog().querySelectorAll<HTMLButtonElement>('button')].find(
+ (b) => b.textContent === name || b.getAttribute('aria-label') === name,
+ );
+const destination = (name: string) =>
+ [...dialog().querySelectorAll<HTMLElement>('[role="radio"]')].find(
+ (b) => b.textContent === name,
+ )!;
+
+/** A Blob's text (jsdom's has no text()). */
+const textOf = (blob: Blob) =>
+ new Promise<string>((resolve) => {
+ const reader = new FileReader();
+ reader.onload = () => resolve(String(reader.result));
+ reader.readAsText(blob);
+ });
+
+class FakeClipboardItem {
+ constructor(public items: Record<string, Blob>) {}
+}
+
+beforeEach(() => {
+ localStorage.clear();
+ onFillNow.mockReset();
+ saveBlob.mockReset();
+ toast.success.mockReset();
+ draftExportBlob.mockReset().mockResolvedValue(new Blob(['x']));
+ writeText.mockReset().mockResolvedValue(undefined);
+ write.mockReset().mockResolvedValue(undefined);
+ Object.defineProperty(navigator, 'clipboard', {
+ value: { writeText, write },
+ configurable: true,
+ });
+ vi.stubGlobal('ClipboardItem', FakeClipboardItem);
+ container = document.createElement('div');
+ document.body.appendChild(container);
+ root = createRoot(container);
+});
+afterEach(() => {
+ act(() => root.unmount());
+ container.remove();
+ vi.unstubAllGlobals();
+});
+
+describe('ExportDialog', () => {
+ it('holds the export while a placeholder is open, and fills it or leaves its sentence out', async () => {
+ await render();
+ expect(dialog().querySelector('[role="alert"]')!.textContent).toContain(
+ '1 placeholder is still open. Exports never contain [ ] text.',
+ );
+ expect(button('Copy for Webflow')!.disabled).toBe(true);
+ await act(async () => button('Fill now')!.click());
+ expect(onFillNow).toHaveBeenCalled();
+
+ await act(async () => button('Export without them')!.click());
+ expect(dialog().querySelector('[role="status"]')!.textContent).toContain(
+ 'The sentence with the placeholder is left out of this export.',
+ );
+ await act(async () => button('Copy for Webflow')!.click());
+ // As rich text, for the CMS's editor to paste, without the placeholder's sentence.
+ const [[[item]]] = write.mock.calls as [[[FakeClipboardItem]]];
+ const html = await textOf(item.items['text/html']);
+ // The H1 goes in the CMS's title field, the placeholder's sentence nowhere.
+ expect(html).toBe(
+ '<p>Brew it slowly.</p><p>See <a href="https://ok.example/">the guide</a>.</p>',
+ );
+ expect(await textOf(item.items['text/plain'])).toContain('Brew it slowly.');
+ expect(toast.success).toHaveBeenCalledWith('Copied for Webflow: paste it in.');
+ // The fields are read from what goes out: no placeholder in them either.
+ expect(dialog().textContent).toContain('Meta descriptionBrew it slowly.Copy');
+ });
+
+ it('holds a placeholder in the title or a heading until it is filled', async () => {
+ await render({
+ html: '<h2>What [name of a real person] uses</h2><p>Use [figure, source] g.</p>',
+ });
+ await act(async () => button('Export without them')!.click());
+ expect(dialog().querySelector('[role="status"]')!.textContent).toContain(
+ '1 placeholder is in the title or a heading: fill it first.',
+ );
+ expect(button('Copy for Webflow')!.disabled).toBe(true);
+ // A new dialog: one with the placeholder in its title.
+ act(() => root.unmount());
+ root = createRoot(container);
+ await render({ title: 'Pour-over by [name of a real person]', html: '<p>Done.</p>' });
+ expect(dialog().querySelector('[role="alert"]')!.textContent).toContain(
+ '1 placeholder is still open',
+ );
+ });
+
+ it("exports a title's and a heading's own brackets as written", async () => {
+ const html = '<h1>Best CRM tools [2026 Update]</h1><h2>The checklist [PDF]</h2><p>Done.</p>';
+ await render({ title: 'Best CRM tools [2026 Update]', html, initialDestination: 'html' });
+ expect(dialog().querySelector('[role="alert"]')).toBeNull();
+ expect(button('Copy HTML')!.disabled).toBe(false);
+ await act(async () => button('Copy HTML')!.click());
+ expect(writeText).toHaveBeenCalledWith(html);
+ });
+
+ it('copies as HTML, and says so, where rich text is refused', async () => {
+ vi.stubGlobal('ClipboardItem', undefined);
+ document.execCommand = vi.fn(() => false);
+ await render({ html: '<h1>Pour-over</h1><p>Done.</p>' });
+ await act(async () => button('Copy for Webflow')!.click());
+ expect(writeText).toHaveBeenCalledWith('<p>Done.</p>');
+ expect(toast.success).toHaveBeenCalledWith(
+ "Copied as HTML: paste it into the editor's HTML or code view.",
+ );
+ });
+
+ it('lists what it checks: clean HTML, the links, the images', async () => {
+ await render({ html: '<p>Done.</p>' });
+ const rows = [
+ ...dialog().querySelectorAll('[aria-label="Content · ready to paste"] li > div'),
+ ].map((row) => row.textContent);
+ expect(rows).toEqual([
+ 'Clean HTML · doneHeadings, lists, tables and FAQ. No editor marks, Page info strip or underlines.',
+ '1 link answers · doneEach was read in the last day.',
+ '1 of 1 images have no alt text · missingAdd it in the HTML view: a few words on what the image shows.',
+ ]);
+ await render({
+ html: '<p>Done.</p>',
+ checks: {
+ links: { count: 3, read: 1, broken: 0 },
+ images: { count: 0, withoutAlt: 0 },
+ ownCode: 2,
+ },
+ linksPending: 2,
+ });
+ expect(dialog().textContent).toContain('3 links, still being read');
+ expect(dialog().textContent).not.toContain('alt text');
+ expect(dialog().textContent).toContain(
+ 'HTML blocks keep their own code · check it2 elements keep a class, a style or a script',
+ );
+ // Read for none and nothing coming: no word that they answer.
+ await render({
+ html: '<p>Done.</p>',
+ checks: {
+ links: { count: 1, read: 0, broken: 0 },
+ images: { count: 0, withoutAlt: 0 },
+ ownCode: 0,
+ },
+ });
+ expect(dialog().textContent).toContain("1 of 1 links couldn't be read");
+ expect(dialog().textContent).not.toContain('answers');
+ });
+
+ it('gives the page fields, each with Copy', async () => {
+ await render({ html: '<p>Brew it slowly.</p>' });
+ const fieldRows = [...dialog().querySelectorAll('[aria-label="Page fields"] dl > div')].map(
+ (row) => row.textContent,
+ );
+ expect(fieldRows).toEqual([
+ 'Meta titlePour-overCopy',
+ 'Meta descriptionBrew it slowly.Copy',
+ 'Slugpour-overCopy',
+ 'AuthorAda Lovelace → your CMS author fieldCopy',
+ 'Featured imageNone in the page',
+ ]);
+ await act(async () => button('Copy Author')!.click());
+ expect(writeText).toHaveBeenCalledWith('Ada Lovelace');
+ });
+
+ it('downloads Word and Markdown with the page fields, and remembers the destination', async () => {
+ await render({ html: '<p>Done.</p>' });
+ // Arrow keys move along the destinations.
+ destination('Webflow').focus();
+ await act(async () => {
+ destination('Webflow').dispatchEvent(
+ new KeyboardEvent('keydown', { key: 'ArrowLeft', bubbles: true }),
+ );
+ });
+ expect(destination('HTML').getAttribute('aria-checked')).toBe('true');
+ expect(document.activeElement).toBe(destination('HTML'));
+ await act(async () => destination('Word').click());
+ expect(dialog().textContent).toContain(
+ 'A Word file, with the page fields in a table on page 1',
+ );
+ await act(async () => button('Download Word')!.click());
+ expect(draftExportBlob).toHaveBeenCalledWith(
+ 'docx',
+ 'Pour-over',
+ '<p>Done.</p>',
+ expect.objectContaining({
+ table: expect.objectContaining({
+ rows: expect.arrayContaining([['Slug', 'pour-over']]),
+ }),
+ front: expect.objectContaining({ slug: 'pour-over', author: 'Ada Lovelace' }),
+ }),
+ );
+ expect(saveBlob).toHaveBeenCalledWith(expect.any(Blob), 'ansvisor_content_pour-over.docx');
+ expect(localStorage.getItem('aeo:content-export-destination')).toBe('docx');
+ // A new page's front matter dates it today.
+ await act(async () => destination('Markdown').click());
+ await act(async () => button('Download Markdown')!.click());
+ const [, , , fields] = draftExportBlob.mock.calls.at(-1)!;
+ expect(fields.front).toHaveProperty('date', expect.stringMatching(/^\d{4}-\d{2}-\d{2}$/));
+ expect(fields.front).not.toHaveProperty('lastmod');
+ });
+
+ it("dates a live page's Markdown as updated today, keeping its published date", async () => {
+ await render({
+ html: '<p>Done.</p>',
+ isNew: false,
+ sourceUrl: 'https://acme.com/blog/pour-over/',
+ initialDestination: 'md',
+ });
+ await act(async () => button('Download Markdown')!.click());
+ const [format, , , fields] = draftExportBlob.mock.calls.at(-1)!;
+ expect(format).toBe('md');
+ expect(fields.front).not.toHaveProperty('date');
+ expect(fields.front).toHaveProperty('lastmod', expect.stringMatching(/^\d{4}-\d{2}-\d{2}$/));
+ });
+
+ it("opens on HTML for Copy HTML, and copies the page's HTML as it is", async () => {
+ await render({ html: '<p>Done.</p>', initialDestination: 'html' });
+ expect(destination('HTML').getAttribute('aria-checked')).toBe('true');
+ await act(async () => button('Copy HTML')!.click());
+ expect(writeText).toHaveBeenCalledWith('<p>Done.</p>');
+ await act(async () => button('Download HTML')!.click());
+ expect(draftExportBlob).toHaveBeenCalledWith(
+ 'html',
+ 'Pour-over',
+ '<p>Done.</p>',
+ expect.anything(),
+ );
+ });
+});
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_export-dialog.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_export-dialog.tsx
new file mode 100644
index 000000000..6147d911c
--- /dev/null
+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_export-dialog.tsx
@@ -0,0 +1,489 @@
+'use client';
+
+import { useMemo, useRef, useState, type KeyboardEvent } from 'react';
+import { useTranslations } from 'next-intl';
+import { toast } from 'sonner';
+import { AlertTriangle, Check } from 'lucide-react';
+import { Button } from '@/components/ui/button';
+import {
+ Dialog,
+ DialogContent,
+ DialogDescription,
+ DialogFooter,
+ DialogHeader,
+ DialogTitle,
+} from '@/components/ui/dialog';
+import type { ContentAuthor } from '@/lib/content/authors';
+import {
+ draftExportBlob,
+ draftExportFileName,
+ draftText,
+ type DraftExportFormat,
+ type ExportFields,
+} from '@/lib/content/draft-export';
+import { localDate } from '@/lib/content/page-schema';
+import {
+ exportPlaceholders,
+ pageFields,
+ titlePlaceholders,
+ withoutLeadingH1,
+ withoutPlaceholders,
+ type PasteChecks,
+} from '@/lib/content/ready-to-publish';
+import { saveBlob } from '@/lib/download';
+import { cn } from '@/lib/utils';
+import { copyToClipboard } from './_html-block';
+
+/** Where the page goes (Quality spec §7b), and how: pasted rich, downloaded, or as HTML. */
+const DESTINATIONS = ['webflow', 'wordpress', 'shopify', 'docs', 'docx', 'md', 'html'] as const;
+export type ExportDestination = (typeof DESTINATIONS)[number];
+const FILES: Partial<Record<ExportDestination, DraftExportFormat>> = { docx: 'docx', md: 'md' };
+/**
+ * Destinations whose title field holds the page's title: its H1 isn't in
+ * the content too (Markdown's front matter has it: draftMarkdown leaves it out).
+ */
+const TITLE_FIELD = new Set<ExportDestination>(['webflow', 'wordpress', 'shopify']);
+
+/** Remembers the destination last picked, across drafts. */
+const DESTINATION_KEY = 'aeo:content-export-destination';
+
+function storedDestination(): ExportDestination {
+ try {
+ const stored = localStorage.getItem(DESTINATION_KEY);
+ if (stored && (DESTINATIONS as readonly string[]).includes(stored)) {
+ return stored as ExportDestination;
+ }
+ } catch {
+ // Storage unavailable: the first one.
+ }
+ return 'webflow';
+}
+
+/**
+ * The page on the clipboard as rich text (and its words as plain text), as
+ * a CMS's editor or Google Docs pastes it: through the Clipboard API, else a
+ * copy event. Says whether it got there as rich text.
+ */
+async function copyRich(html: string, text: string): Promise<boolean> {
+ try {
+ if (typeof ClipboardItem !== 'undefined' && navigator.clipboard?.write) {
+ await navigator.clipboard.write([
+ new ClipboardItem({
+ 'text/html': new Blob([html], { type: 'text/html' }),
+ 'text/plain': new Blob([text], { type: 'text/plain' }),
+ }),
+ ]);
+ return true;
+ }
+ } catch {
+ // Refused (an older browser, no permission): the copy event below.
+ }
+ const put = (event: ClipboardEvent) => {
+ event.clipboardData?.setData('text/html', html);
+ event.clipboardData?.setData('text/plain', text);
+ event.preventDefault();
+ };
+ document.addEventListener('copy', put);
+ try {
+ return document.execCommand('copy');
+ } catch {
+ return false;
+ } finally {
+ document.removeEventListener('copy', put);
+ }
+}
+
+type RowState = 'ok' | 'missing' | 'check';
+
+/**
+ * Export (Content Editor Quality spec §7b, CE-057): the page ready to paste
+ * or to save, for the destination picked. Its placeholders are filled first
+ * or left out with their sentences (no export carries one; the title's and
+ * the headings' are filled first); what it checks is listed (clean HTML,
+ * links that answer, images with alt text); the page fields go next to it,
+ * read from the page as it goes out, each with Copy. Rendered only while
+ * open, on the page as it was when it opened.
+ */
+export function ExportDialog({
+ onClose,
+ title,
+ html,
+ isNew,
+ sourceUrl,
+ author,
+ teamByline,
+ checks,
+ linksPending,
+ initialDestination,
+ onFillNow,
+}: {
+ onClose: () => void;
+ title: string;
+ /** The page as Copy HTML gives it (publishableHtml). */
+ html: string;
+ /** A new page; an optimized one is already live, with its published date. */
+ isNew: boolean;
+ /** The live page an optimized draft is for: its address is the slug. */
+ sourceUrl: string | null;
+ author: ContentAuthor | null;
+ /** The team byline; empty while the authors aren't read. */
+ teamByline: string;
+ checks: PasteChecks;
+ /** The page's links whose read may still come. */
+ linksPending: number;
+ /** Copy HTML's: the HTML destination. */
+ initialDestination?: ExportDestination;
+ /** Goes to the first placeholder, to fill it. */
+ onFillNow: () => void;
+}) {
+ const t = useTranslations('contentEditor.editor.exportDialog');
+ const [destination, setDestination] = useState<ExportDestination>(
+ () => initialDestination ?? storedDestination(),
+ );
+ const [skipPlaceholders, setSkipPlaceholders] = useState(false);
+ const radios = useRef<(HTMLButtonElement | null)[]>([]);
+ const placeholders = useMemo(
+ () => exportPlaceholders(html) + titlePlaceholders(title, html),
+ [html, title],
+ );
+ const content = useMemo(
+ () => (skipPlaceholders && placeholders ? withoutPlaceholders(html) : html),
+ [skipPlaceholders, placeholders, html],
+ );
+ // What is still open once left out: the title's and the headings'.
+ const left = useMemo(
+ () =>
+ skipPlaceholders
+ ? exportPlaceholders(content) + titlePlaceholders(title, content)
+ : placeholders,
+ [skipPlaceholders, content, title, placeholders],
+ );
+ const ready = left === 0;
+ const fields = useMemo(
+ () => pageFields({ title, html: content, sourceUrl, author, teamByline }),
+ [title, content, sourceUrl, author, teamByline],
+ );
+ const out = TITLE_FIELD.has(destination) ? withoutLeadingH1(content) : content;
+
+ const pick = (next: ExportDestination) => {
+ setDestination(next);
+ try {
+ localStorage.setItem(DESTINATION_KEY, next);
+ } catch {
+ // The choice just won't be remembered.
+ }
+ };
+ // Arrow keys move along the destinations, as in any radio group.
+ const onRadioKey = (event: KeyboardEvent, i: number) => {
+ const step = ({ ArrowRight: 1, ArrowDown: 1, ArrowLeft: -1, ArrowUp: -1 } as const)[
+ event.key as 'ArrowRight'
+ ];
+ if (!step) return;
+ event.preventDefault();
+ const next = (i + step + DESTINATIONS.length) % DESTINATIONS.length;
+ pick(DESTINATIONS[next]);
+ radios.current[next]?.focus();
+ };
+ const exportFields: ExportFields = {
+ table: {
+ note: t('fields.note'),
+ rows: [
+ [t('fields.metaTitle'), fields.metaTitle],
+ [t('fields.metaDescription'), fields.metaDescription],
+ ...(fields.slug ? [[t('fields.slug'), fields.slug] as [string, string]] : []),
+ ...(fields.author ? [[t('fields.author'), fields.author] as [string, string]] : []),
+ ...(fields.featuredImage
+ ? [[t('fields.featuredImage'), fields.featuredImage] as [string, string]]
+ : []),
+ ],
+ },
+ front: {
+ title: fields.metaTitle,
+ description: fields.metaDescription,
+ slug: fields.slug,
+ author: fields.author,
+ // As Before you publish's JSON-LD: a live page keeps its published
+ // date (a site's "date"), and today is when it was updated.
+ ...(isNew ? { date: localDate(new Date()) } : { lastmod: localDate(new Date()) }),
+ ...(fields.featuredImage ? { image: fields.featuredImage } : {}),
+ },
+ head: {
+ title: fields.metaTitle,
+ description: fields.metaDescription,
+ author: fields.author,
+ },
+ };
+ const download = async (format: DraftExportFormat) => {
+ const name = title.trim() || t('untitled');
+ try {
+ saveBlob(
+ await draftExportBlob(format, name, content, exportFields),
+ draftExportFileName(name, format),
+ );
+ } catch (err) {
+ console.error('Failed to export the page:', err);
+ toast.error(t('failed'));
+ }
+ };
+ const act = async () => {
+ const file = FILES[destination];
+ if (file) return download(file);
+ if (destination === 'html') return copyToClipboard(out, t('copiedHtml'), t('copyFailed'));
+ if (await copyRich(out, draftText('', out))) {
+ toast.success(t('copied', { destination: t(`destinations.${destination}`) }));
+ return;
+ }
+ // No rich text here: the HTML, said as such, for the editor's code view.
+ await copyToClipboard(out, t('copiedAsHtml'), t('copyFailed'));
+ };
+
+ const rows: { state: RowState; title: string; detail: string }[] = [
+ checks.ownCode
+ ? {
+ state: 'check',
+ title: t('checks.ownCode.title'),
+ detail: t('checks.ownCode.detail', { count: checks.ownCode }),
+ }
+ : { state: 'ok', title: t('checks.clean.title'), detail: t('checks.clean.detail') },
+ ];
+ if (checks.links.count) {
+ const { count, read, broken } = checks.links;
+ rows.push(
+ broken
+ ? {
+ state: 'missing',
+ title: t('checks.links.broken', { broken, count }),
+ detail: t('checks.links.brokenDetail'),
+ }
+ : read < count && linksPending
+ ? {
+ state: 'check',
+ title: t('checks.links.reading', { count }),
+ detail: t('checks.links.readingDetail'),
+ }
+ : read < count
+ ? {
+ // Some couldn't be read: no word on whether they answer.
+ state: 'check',
+ title: t('checks.links.unread', { unread: count - read, count }),
+ detail: t('checks.links.unreadDetail'),
+ }
+ : {
+ state: 'ok',
+ title: t('checks.links.ok', { count }),
+ detail: t('checks.links.okDetail'),
+ },
+ );
+ }
+ if (checks.images.count) {
+ rows.push(
+ checks.images.withoutAlt
+ ? {
+ state: 'missing',
+ title: t('checks.images.missing', {
+ missing: checks.images.withoutAlt,
+ count: checks.images.count,
+ }),
+ detail: t('checks.images.missingDetail'),
+ }
+ : {
+ state: 'ok',
+ title: t('checks.images.ok', { count: checks.images.count }),
+ detail: t('checks.images.okDetail'),
+ },
+ );
+ }
+ const fieldRows: { label: string; value: string | null; shown?: string }[] = [
+ { label: t('fields.metaTitle'), value: fields.metaTitle },
+ { label: t('fields.metaDescription'), value: fields.metaDescription },
+ {
+ label: t('fields.slug'),
+ value: fields.slug || null,
+ shown: fields.slug || t('fields.noSlug'),
+ },
+ {
+ label: t('fields.author'),
+ value: fields.author || null,
+ shown: fields.author
+ ? t('fields.authorValue', { name: fields.author })
+ : t('fields.noAuthor'),
+ },
+ {
+ label: t('fields.featuredImage'),
+ value: fields.featuredImage,
+ shown: fields.featuredImage ?? t('fields.noImage'),
+ },
+ ];
+
+ return (
+ <Dialog open onOpenChange={(open) => !open && onClose()}>
+ <DialogContent className="max-h-[calc(100dvh-2rem)] overflow-y-auto sm:max-w-xl">
+ <DialogHeader>
+ <DialogTitle>{t('title')}</DialogTitle>
+ </DialogHeader>
+
+ <div className="space-y-1.5">
+ <div role="radiogroup" aria-label={t('destination')} className="flex flex-wrap gap-1.5">
+ {DESTINATIONS.map((d, i) => (
+ <button
+ key={d}
+ ref={(el) => void (radios.current[i] = el)}
+ type="button"
+ role="radio"
+ aria-checked={d === destination}
+ tabIndex={d === destination ? 0 : -1}
+ onClick={() => pick(d)}
+ onKeyDown={(event) => onRadioKey(event, i)}
+ className={cn(
+ 'rounded-full border px-2.5 py-1 text-xs font-medium',
+ d === destination
+ ? 'border-foreground bg-foreground text-background'
+ : 'bg-card text-muted-foreground hover:text-foreground',
+ )}
+ >
+ {t(`destinations.${d}`)}
+ </button>
+ ))}
+ </div>
+ <DialogDescription className="text-xs">{t(`hints.${destination}`)}</DialogDescription>
+ </div>
+
+ {placeholders > 0 &&
+ (skipPlaceholders ? (
+ <div
+ role="status"
+ className={cn(
+ 'flex flex-wrap items-center gap-x-2 gap-y-1 rounded-xl border px-3 py-2.5 text-sm',
+ !ready &&
+ 'border-amber-200 bg-amber-50 text-amber-800 dark:border-amber-900 dark:bg-amber-950 dark:text-amber-300',
+ )}
+ >
+ <span className="min-w-0 flex-1">
+ {ready
+ ? t('placeholders.left', { count: placeholders })
+ : t('placeholders.headings', { count: left })}
+ </span>
+ {!ready && (
+ <Button variant="outline" size="sm" className="h-7" onClick={onFillNow}>
+ {t('placeholders.fill')}
+ </Button>
+ )}
+ <button
+ type="button"
+ className="text-xs font-medium underline-offset-2 hover:underline"
+ onClick={() => setSkipPlaceholders(false)}
+ >
+ {t('placeholders.undo')}
+ </button>
+ </div>
+ ) : (
+ <div
+ role="alert"
+ className="flex flex-wrap items-start gap-2 rounded-xl border border-amber-200 bg-amber-50 px-3 py-2.5 text-sm text-amber-800 dark:border-amber-900 dark:bg-amber-950 dark:text-amber-300"
+ >
+ <AlertTriangle aria-hidden className="mt-0.5 h-4 w-4 shrink-0" />
+ <p className="min-w-0 flex-1">{t('placeholders.open', { count: placeholders })}</p>
+ <div className="flex w-full justify-end gap-2">
+ <Button variant="outline" size="sm" className="h-7" onClick={onFillNow}>
+ {t('placeholders.fill')}
+ </Button>
+ <Button
+ variant="outline"
+ size="sm"
+ className="h-7"
+ onClick={() => setSkipPlaceholders(true)}
+ >
+ {t('placeholders.skip')}
+ </Button>
+ </div>
+ </div>
+ ))}
+
+ <section aria-label={t('checks.label')} className="space-y-1.5">
+ <h3 className="text-xs font-medium text-muted-foreground">{t('checks.label')}</h3>
+ <ul className="divide-y rounded-xl border text-sm">
+ {rows.map((row) => (
+ <li key={row.title} className="flex items-start gap-2.5 px-3 py-2.5">
+ <span
+ aria-hidden
+ className={cn(
+ 'mt-0.5 flex h-4 w-4 shrink-0 items-center justify-center rounded-full text-[10px] font-semibold',
+ row.state === 'ok'
+ ? 'bg-emerald-100 text-emerald-700 dark:bg-emerald-950 dark:text-emerald-300'
+ : row.state === 'missing'
+ ? 'bg-amber-100 text-amber-700 dark:bg-amber-950 dark:text-amber-300'
+ : 'bg-muted text-muted-foreground',
+ )}
+ >
+ {row.state === 'ok' ? (
+ <Check className="h-3 w-3" />
+ ) : row.state === 'missing' ? (
+ '!'
+ ) : (
+ '?'
+ )}
+ </span>
+ <div className="min-w-0 flex-1">
+ <p className="font-medium">
+ {row.title}
+ <span className="sr-only">
+ {' · '}
+ {t(`states.${row.state}`)}
+ </span>
+ </p>
+ <p className="text-xs text-muted-foreground">{row.detail}</p>
+ </div>
+ </li>
+ ))}
+ </ul>
+ </section>
+
+ <section aria-label={t('fields.label')} className="space-y-1.5">
+ <h3 className="text-xs font-medium text-muted-foreground">
+ {t('fields.label')} <span className="font-normal">· {t('fields.where')}</span>
+ </h3>
+ <dl className="divide-y rounded-xl border text-sm">
+ {fieldRows.map((row) => (
+ <div key={row.label} className="flex items-start gap-2.5 px-3 py-2">
+ <dt className="w-28 shrink-0 text-xs text-muted-foreground">{row.label}</dt>
+ <dd className="min-w-0 flex-1 text-xs [overflow-wrap:anywhere]">
+ {row.shown ?? row.value}
+ </dd>
+ {row.value && (
+ <button
+ type="button"
+ aria-label={t('fields.copyLabel', { field: row.label })}
+ className="shrink-0 text-xs font-medium text-muted-foreground hover:text-foreground"
+ onClick={() =>
+ void copyToClipboard(
+ row.value!,
+ t('fields.copied', { field: row.label }),
+ t('copyFailed'),
+ )
+ }
+ >
+ {t('fields.copy')}
+ </button>
+ )}
+ </div>
+ ))}
+ </dl>
+ </section>
+
+ <DialogFooter className="gap-2">
+ <Button variant="outline" onClick={onClose}>
+ {t('cancel')}
+ </Button>
+ {destination === 'html' && (
+ <Button variant="outline" disabled={!ready} onClick={() => void download('html')}>
+ {t('actions.downloadHtml')}
+ </Button>
+ )}
+ <Button disabled={!ready} onClick={() => void act()}>
+ {t(`actions.${destination}`)}
+ </Button>
+ </DialogFooter>
+ </DialogContent>
+ </Dialog>
+ );
+}
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/page.test.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/page.test.tsx
index 71a68d1b3..a3adc04ae 100644
--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/page.test.tsx
+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/page.test.tsx
@@ -2822,6 +2822,93 @@ describe('the side panel', () => {
});
});
+ describe('Export (CE-057)', () => {
+ beforeEach(() => {
+ process.env.NEXT_PUBLIC_CONTENT_QUALITY_PHASE = '1';
+ getContentAuthors.mockResolvedValue({
+ authors: [],
+ brands: [{ id: 'b1', name: 'Acme' }],
+ me: { id: 'u1', role: 'analyst' },
+ });
+ });
+ afterEach(() => {
+ delete process.env.NEXT_PUBLIC_CONTENT_QUALITY_PHASE;
+ });
+ const dialog = () => document.querySelector('[role="dialog"]');
+ const inDialog = (name: string) =>
+ [...document.querySelectorAll<HTMLButtonElement>('[role="dialog"] button')].find(
+ (b) => b.textContent === name,
+ )!;
+
+ it('opens Export with the page fields, ready to copy for the destination', async () => {
+ getContentDraft.mockResolvedValue(draft());
+ await renderPage();
+ await act(async () => button('Export')!.click());
+ expect(dialog()!.textContent).toContain('Content · ready to paste');
+ expect(dialog()!.textContent).toContain('Meta titlePipeline forecasting');
+ expect(dialog()!.textContent).toContain('AuthorAcme Team → your CMS author field');
+ expect(inDialog('Copy for Webflow').disabled).toBe(false);
+ });
+
+ it('sends Copy HTML to Export while a placeholder is open, and Fill now goes to it', async () => {
+ const writeText = vi.fn().mockResolvedValue(undefined);
+ Object.defineProperty(navigator, 'clipboard', { value: { writeText }, configurable: true });
+ getContentDraft.mockResolvedValue(
+ draft({ bodyHtml: '<h1>Pipeline forecasting</h1><p>Ask [name of a real person] how.</p>' }),
+ );
+ await renderPage();
+ await act(async () => button('Copy HTML')!.click());
+ expect(writeText).not.toHaveBeenCalled();
+ expect(dialog()!.textContent).toContain('1 placeholder is still open');
+ expect(dialog()!.querySelector('[role="radio"][aria-checked="true"]')!.textContent).toBe(
+ 'HTML',
+ );
+ expect(inDialog('Copy HTML').disabled).toBe(true);
+
+ await act(async () => inDialog('Fill now').click());
+ expect(dialog()).toBeNull();
+ const editor = (container.querySelector('.ProseMirror') as HTMLElement & { editor: Editor })
+ .editor;
+ const { from, to } = editor.state.selection;
+ expect(editor.state.doc.textBetween(from, to)).toBe('[name of a real person]');
+ });
+
+ it('says where a placeholder only an HTML block holds is', async () => {
+ const { htmlBlockElement } = await import('@/lib/content/html-allowlist');
+ getContentDraft.mockResolvedValue(
+ draft({
+ bodyHtml:
+ '<h1>Pipeline forecasting</h1><p>Teams trust it.</p>' +
+ htmlBlockElement('<div class="cta">Call [phone number]</div>'),
+ }),
+ );
+ await renderPage();
+ await act(async () => button('Export')!.click());
+ expect(dialog()!.textContent).toContain('1 placeholder is still open');
+ await act(async () => inDialog('Fill now').click());
+ expect(toast).toHaveBeenCalledWith(
+ 'The placeholders are in an HTML block: open it with </> to fill them.',
+ );
+ });
+
+ it('copies at once with no placeholder, and keeps the menu while Quality is off', async () => {
+ const writeText = vi.fn().mockResolvedValue(undefined);
+ Object.defineProperty(navigator, 'clipboard', { value: { writeText }, configurable: true });
+ getContentDraft.mockResolvedValue(draft());
+ await renderPage();
+ await act(async () => button('Copy HTML')!.click());
+ expect(writeText).toHaveBeenCalled();
+ expect(dialog()).toBeNull();
+
+ delete process.env.NEXT_PUBLIC_CONTENT_QUALITY_PHASE;
+ await act(async () => root.unmount());
+ root = createRoot(container);
+ await renderPage();
+ await act(async () => button('Export')!.click());
+ expect(dialog()).toBeNull();
+ });
+ });
+
describe('Run Agent: fix open rules', () => {
const traced = draft({
pageType: 'blog_post',
diff --git a/web/src/lib/content/draft-export.test.ts b/web/src/lib/content/draft-export.test.ts
index a779e72d6..b5525ce7f 100644
--- a/web/src/lib/content/draft-export.test.ts
+++ b/web/src/lib/content/draft-export.test.ts
@@ -164,6 +164,54 @@ describe('draftDocx', () => {
});
});
+describe('the page fields an export carries (CE-057)', () => {
+ const fields = {
+ table: {
+ note: 'Page fields: these go in your CMS fields.',
+ rows: [
+ ['Meta title', 'Pipeline forecasting: a guide'],
+ ['Slug', 'pipeline-forecasting'],
+ ] as [string, string][],
+ },
+ front: {
+ title: 'Pipeline: "a guide"',
+ description: 'How teams forecast.',
+ slug: 'pipeline',
+ author: '',
+ },
+ head: {
+ title: 'Pipeline forecasting: a guide',
+ description: 'How "teams" <forecast>.',
+ author: 'Ada Lovelace',
+ },
+ };
+
+ it('gives Markdown front matter, each value quoted, an empty one left out, the title not again', () => {
+ expect(draftMarkdown('Pipeline', '<h1>Pipeline</h1><p>Teams trust it.</p>', fields)).toBe(
+ '---\ntitle: "Pipeline: \\"a guide\\""\ndescription: "How teams forecast."\nslug: "pipeline"\n---\n\nTeams trust it.\n',
+ );
+ });
+
+ it('gives HTML its meta title, description and author', () => {
+ const doc = draftHtmlDocument('Pipeline', '<h1>Pipeline</h1>', fields);
+ expect(doc).toContain('<title>Pipeline forecasting: a guide</title>');
+ expect(doc).toContain(
+ '<meta name="description" content="How &quot;teams&quot; &lt;forecast&gt;.">',
+ );
+ expect(doc).toContain('<meta name="author" content="Ada Lovelace">');
+ });
+
+ it('gives Word a table of them on page 1, the page after it', async () => {
+ const files = unzipSync(await read(await draftDocx('Pipeline forecasting', PAGE, fields)));
+ const xml = strFromU8(files['word/document.xml']);
+ const table = xml.indexOf('Pipeline forecasting: a guide');
+ expect(xml.indexOf('Page fields: these go in your CMS fields.')).toBeLessThan(table);
+ expect(xml).toContain('pipeline-forecasting');
+ // The page starts on a page of its own.
+ expect(xml.indexOf('How it works')).toBeGreaterThan(xml.indexOf('<w:sectPr'));
+ });
+});
+
describe('draftExportBlob and draftExportFileName', () => {
it('gives each format its file', async () => {
const md = await draftExportBlob('md', 'T', '<h1>T</h1>');
diff --git a/web/src/lib/content/draft-export.ts b/web/src/lib/content/draft-export.ts
index 388b6ecdf..b73559f95 100644
--- a/web/src/lib/content/draft-export.ts
+++ b/web/src/lib/content/draft-export.ts
@@ -14,6 +14,20 @@ import { safeLinkHref } from './editor';
export type DraftExportFormat = 'docx' | 'html' | 'md';
+/**
+ * The page fields an export carries next to the content (Quality spec §7b,
+ * CE-057): Word's as a table on page 1, Markdown's as front matter, HTML's
+ * in its head.
+ */
+export interface ExportFields {
+ /** Word's table: each field's label and value, and the line above it. */
+ table: { note: string; rows: [label: string, value: string][] };
+ /** Markdown's front matter, in order. */
+ front: Record<string, string>;
+ /** HTML's head. */
+ head: { title: string; description: string; author: string };
+}
+
/** A stretch of text with one format; a link carries its address. */
export interface DraftRun {
text: string;
@@ -179,14 +193,22 @@ export function draftBlocks(
const escapeHtml = (value: string) =>
value.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
+/** A value for an attribute in double quotes: its quotes escaped too. */
+const escapeAttribute = (value: string) => escapeHtml(value).replace(/"/g, '&quot;');
+
/** Export HTML: Copy HTML's page as a document of its own, titled. */
-export function draftHtmlDocument(title: string, html: string): string {
+export function draftHtmlDocument(title: string, html: string, fields?: ExportFields): string {
+ const head = fields?.head;
return [
'<!doctype html>',
'<html lang="en">',
'<head>',
'<meta charset="utf-8">',
- `<title>${escapeHtml(title.trim())}</title>`,
+ `<title>${escapeHtml((head?.title || title).trim())}</title>`,
+ ...(head?.description
+ ? [`<meta name="description" content="${escapeAttribute(head.description)}">`]
+ : []),
+ ...(head?.author ? [`<meta name="author" content="${escapeAttribute(head.author)}">`] : []),
'</head>',
'<body>',
html.trim(),
@@ -240,10 +262,22 @@ function markdownBlock(block: DraftBlock): string {
}
}
+/** Front matter as YAML: each value quoted, so a colon or a quote in it reads as written. */
+function frontMatter(front: Record<string, string>): string {
+ const quoted = (value: string) => `"${value.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`;
+ const lines = Object.entries(front)
+ .filter(([, value]) => value)
+ .map(([key, value]) => `${key}: ${quoted(value)}`);
+ return `---\n${lines.join('\n')}\n---\n\n`;
+}
+
/** Export Markdown: headings, emphasis, links, lists, quotes and tables; an HTML block as HTML. */
-export function draftMarkdown(title: string, html: string): string {
- const blocks = draftBlocks(title, html);
- const parts: string[] = [];
+export function draftMarkdown(title: string, html: string, fields?: ExportFields): string {
+ // With front matter, the title is there: not an H1 as well.
+ const blocks = draftBlocks(title, html).filter(
+ (block, i) => !(fields && i === 0 && block.kind === 'heading' && block.level === 1),
+ );
+ const parts: string[] = fields ? [frontMatter(fields.front)] : [];
blocks.forEach((block, i) => {
// A list's items one under the other; a blank line between other blocks.
const prev = blocks[i - 1];
@@ -348,25 +382,48 @@ function docxDocument(m: Docx, title: string, sections: DocxChild[][]) {
});
}
-/** Export Word: the blocks as a document, the lists numbered and bulleted as on the page. */
-export async function draftDocx(title: string, html: string): Promise<Blob> {
+/**
+ * Export Word: the blocks as a document, the lists numbered and bulleted as
+ * on the page; the page fields, if any, as a table on page 1 to delete or
+ * keep.
+ */
+export async function draftDocx(title: string, html: string, fields?: ExportFields): Promise<Blob> {
// Loaded on the click: the library is only needed to write the file.
const m = await import('docx');
const blocks = draftBlocks(title, html);
const name = title.trim() || text(blocks.find((b) => b.kind === 'heading')?.runs ?? []);
- return m.Packer.toBlob(docxDocument(m, name, [docxBlocks(m, blocks)]));
+ const table: DraftBlock[] = fields
+ ? [
+ { kind: 'paragraph', runs: [{ text: fields.table.note, italics: true }] },
+ {
+ kind: 'table',
+ rows: fields.table.rows.map(([label, value]) => ({
+ header: false,
+ cells: [[{ text: label, bold: true }], [{ text: value }]],
+ })),
+ },
+ ]
+ : [];
+ const sections = table.length
+ ? [docxBlocks(m, table), docxBlocks(m, blocks)]
+ : [docxBlocks(m, blocks)];
+ return m.Packer.toBlob(docxDocument(m, name, sections));
}
-/** The file for a format: Word built on the click, HTML and Markdown as UTF-8 text. */
+/**
+ * The file for a format: Word built on the click, HTML and Markdown as UTF-8
+ * text; with the page fields, each carries them its own way.
+ */
export async function draftExportBlob(
format: DraftExportFormat,
title: string,
html: string,
+ fields?: ExportFields,
): Promise<Blob> {
- if (format === 'docx') return draftDocx(title, html);
+ if (format === 'docx') return draftDocx(title, html, fields);
return format === 'html'
- ? new Blob([draftHtmlDocument(title, html)], { type: 'text/html;charset=utf-8' })
- : new Blob([draftMarkdown(title, html)], { type: 'text/markdown;charset=utf-8' });
+ ? new Blob([draftHtmlDocument(title, html, fields)], { type: 'text/html;charset=utf-8' })
+ : new Blob([draftMarkdown(title, html, fields)], { type: 'text/markdown;charset=utf-8' });
}
/** "ansvisor_content_pipeline-forecasting.docx". */
diff --git a/web/src/lib/content/html-allowlist.ts b/web/src/lib/content/html-allowlist.ts
index 18daa10fe..290516228 100644
--- a/web/src/lib/content/html-allowlist.ts
+++ b/web/src/lib/content/html-allowlist.ts
@@ -93,6 +93,11 @@ export function normalizeJsonLd(text: string): string | null {
return null;
}
if (typeof value !== 'object' || value === null) return null;
+ return jsonLdText(value);
+}
+
+/** A JSON value as a draft's JSON-LD script holds it: <, > and & escaped. */
+export function jsonLdText(value: unknown): string {
return JSON.stringify(value)
.replace(/</g, '\\u003c')
.replace(/>/g, '\\u003e')
diff --git a/web/src/lib/content/placeholders.test.ts b/web/src/lib/content/placeholders.test.ts
index 89d01f0b5..34082aa41 100644
--- a/web/src/lib/content/placeholders.test.ts
+++ b/web/src/lib/content/placeholders.test.ts
@@ -26,4 +26,13 @@ describe('pagePlaceholders', () => {
).toEqual({ count: 0 });
expect(pagePlaceholders('')).toEqual({ count: 0 });
});
+
+ it("reads a heading's brackets only in the agent's own forms", () => {
+ expect(
+ pagePlaceholders(
+ '<h1>Best CRM tools [2026 Update]</h1><h2>How to brew [Step-by-Step Guide]</h2>' +
+ '<h3>The checklist [PDF]</h3><h3>Brew [figure, source, 2025] grams</h3><p>[PDF]</p>',
+ ),
+ ).toEqual({ count: 2 });
+ });
});
diff --git a/web/src/lib/content/placeholders.ts b/web/src/lib/content/placeholders.ts
index 60bd9cd31..a175bb1eb 100644
--- a/web/src/lib/content/placeholders.ts
+++ b/web/src/lib/content/placeholders.ts
@@ -13,6 +13,20 @@
*/
export const PLACEHOLDER = /\[(?!sic\])(?=[^[\]\n]*\p{L})[^[\]\n]{2,}\](?!\()/giu;
+/**
+ * The forms the server's guards write in a page (the number guard's
+ * "[figure, source]", content-people.js's name and quote): in a title or a
+ * heading, only these are placeholders. Brackets a person writes there
+ * ("Best CRM tools [2026 Update]", "[PDF]") are part of the title. The
+ * writer's prompts let the model write others too ("[price]"); in a heading
+ * they read as written.
+ */
+export const AGENT_PLACEHOLDER =
+ /\[(?:figure, source[^[\]\n]*|name of a real person|quote from a real person[^[\]\n]*)\](?!\()/giu;
+
+/** The placeholders a line is read for: a heading's (and a title's) are the agent's forms only. */
+export const placeholderIn = (heading: boolean) => (heading ? AGENT_PLACEHOLDER : PLACEHOLDER);
+
export interface PagePlaceholders {
count: number;
}
@@ -26,7 +40,7 @@ export function pagePlaceholders(html: string): PagePlaceholders {
const doc = new DOMParser().parseFromString(html, 'text/html');
let count = 0;
for (const el of doc.body.querySelectorAll('p, h1, h2, h3, h4, h5, h6')) {
- count += (el.textContent ?? '').match(PLACEHOLDER)?.length ?? 0;
+ count += (el.textContent ?? '').match(placeholderIn(el.tagName !== 'P'))?.length ?? 0;
}
return { count };
}
diff --git a/web/src/lib/content/ready-to-publish.test.ts b/web/src/lib/content/ready-to-publish.test.ts
new file mode 100644
index 000000000..ae7bbf6b3
--- /dev/null
+++ b/web/src/lib/content/ready-to-publish.test.ts
@@ -0,0 +1,263 @@
+import { describe, expect, it } from 'vitest';
+import type { ContentAuthor } from './authors';
+import type { LinkCheck } from './quality-checks';
+import {
+ exportPlaceholders,
+ fragment,
+ pageFields,
+ pasteChecks,
+ titlePlaceholders,
+ withoutLeadingH1,
+ withoutPlaceholders,
+} from './ready-to-publish';
+
+/** Ready to publish (Quality spec §7b, CE-057): what an export gives, and checks first. */
+
+const ada = { id: 'a1', name: 'Ada Lovelace' } as ContentAuthor;
+
+/** A FAQPage's JSON-LD, one line as a draft stores it, with a placeholder in an answer. */
+const FAQ =
+ '{"@context":"https://schema.org","@type":"FAQPage","mainEntity":[' +
+ '{"@type":"Question","name":"How long does it take?","acceptedAnswer":{"@type":"Answer","text":"About four minutes. It costs [price]."}},' +
+ '{"@type":"Question","name":"Which grind?","acceptedAnswer":{"@type":"Answer","text":"Medium-fine."}}]}';
+const jsonLdOf = (html: string) =>
+ JSON.parse(fragment(html).querySelector('script[type="application/ld+json"]')!.textContent!);
+
+describe('fragment', () => {
+ it('parses into a document of its own, and keeps a leading script where it is', () => {
+ const root = fragment(
+ '<script type="application/ld+json">{}</script><p><img src="https://cdn.acme.com/a.jpg" alt="A"></p>',
+ );
+ expect(root.ownerDocument).not.toBe(document);
+ expect(root.firstElementChild?.tagName).toBe('SCRIPT');
+ });
+});
+
+describe('exportPlaceholders', () => {
+ it('counts every placeholder an export would carry: words, alt text, JSON-LD', () => {
+ expect(
+ exportPlaceholders(
+ '<script type="application/ld+json">{"author":"[name of a real person]"}</script>' +
+ '<h2>What [name of a real person] uses</h2><p>Use [figure, source] g.</p>' +
+ '<div class="cta"><span>Call [phone number]</span></div><p>Note [1], [sic].</p>' +
+ '<img src="https://cdn.acme.com/a.jpg" alt="[photo of the founder]">',
+ ),
+ ).toBe(5);
+ });
+
+ it("reads JSON-LD by its strings: an array is no placeholder, one that doesn't parse is read for none", () => {
+ expect(
+ exportPlaceholders(
+ '<script type="application/ld+json">{"@type":["Article","NewsArticle"],"author":{"@type":"Person","name":"Ada","sameAs":["https://x.example/ada","https://acme.com/ada"]}}</script><p>Brew.</p>',
+ ),
+ ).toBe(0);
+ expect(exportPlaceholders(`<script type="application/ld+json">${FAQ}</script>`)).toBe(1);
+ expect(
+ exportPlaceholders(
+ '<script type="application/ld+json">{"sameAs":["https://x.example/"],"name":"[brand]"</script>',
+ ),
+ ).toBe(0);
+ });
+
+ it("holds a title's or a heading's brackets only in the agent's own forms", () => {
+ expect(
+ exportPlaceholders(
+ '<h1>Best CRM tools [2026 Update]</h1><h2>How to brew [Step-by-Step Guide]</h2>' +
+ '<h3>The checklist [PDF]</h3><p>Brew it slowly.</p>',
+ ),
+ ).toBe(0);
+ expect(
+ exportPlaceholders(
+ '<h2>What [name of a real person] uses</h2><h2>[quote from a real person, with its source]</h2>' +
+ '<h3>Brew [figure, source, 2025] grams</h3><p>Costs [price].</p>',
+ ),
+ ).toBe(4);
+ });
+});
+
+describe('titlePlaceholders', () => {
+ it("counts the title's only when the page's H1 isn't it", () => {
+ const title = 'Best [figure, source] grinders';
+ expect(titlePlaceholders(title, '<h1>Best [figure, source] grinders</h1>')).toBe(0);
+ expect(titlePlaceholders(title, '<h1>Best grinders</h1>')).toBe(1);
+ expect(titlePlaceholders(title, '<p>No H1.</p>')).toBe(1);
+ });
+
+ it("reads a person's brackets in the title as part of it", () => {
+ expect(titlePlaceholders('Best CRM tools [2026 Update]', '<p>No H1.</p>')).toBe(0);
+ expect(titlePlaceholders('The checklist [PDF]', '<h1>The checklist</h1>')).toBe(0);
+ });
+});
+
+describe('withoutPlaceholders', () => {
+ it('leaves out the sentence a placeholder is in, and keeps the rest as written', () => {
+ expect(
+ withoutPlaceholders(
+ '<p>Brew it slowly. Use [figure, source] g of <strong>fresh</strong> coffee. See <a href="https://acme.com/">our guide</a>.</p>',
+ ),
+ ).toBe('<p>Brew it slowly. See <a href="https://acme.com/">our guide</a>.</p>');
+ });
+
+ it('reads sentences as a reader does: numbers, abbreviations and addresses end none', () => {
+ expect(
+ withoutPlaceholders(
+ '<p>The kettle holds 1.5 litres and costs [price]. Use a scale, e.g. the Acme one. Visit acme.com for [details]. As [Dr. Name, title] says, pour slowly. Next.</p>',
+ ),
+ ).toBe('<p>Use a scale, e.g. the Acme one. Next.</p>');
+ });
+
+ it('keeps a heading’s placeholder to be filled, and drops what is left empty', () => {
+ expect(
+ withoutPlaceholders(
+ '<h2>What [name of a real person] uses</h2>' +
+ '<blockquote><p>[quote from a real person, with its source]</p></blockquote>' +
+ '<ul><li><p>Ask <a href="https://x.example/">[name of a real person]</a>.</p></li><li><p>Grind fresh.</p></li></ul>' +
+ '<table><tbody><tr><td><p>[price]</p></td></tr><tr><td><p>Ratio</p></td></tr></tbody></table>',
+ ),
+ ).toBe(
+ '<h2>What [name of a real person] uses</h2><ul><li><p>Grind fresh.</p></li></ul>' +
+ '<table><tbody><tr><td><p>Ratio</p></td></tr></tbody></table>',
+ );
+ });
+
+ it('takes one alone where no line holds it, in alt text and JSON-LD, and keeps a leading script', () => {
+ expect(
+ withoutPlaceholders(
+ '<script type="application/ld+json">{"author":"[name of a real person]"}</script>' +
+ '<div class="cta"><span>Call [phone number] today</span></div>' +
+ '<img src="https://cdn.acme.com/a.jpg" alt="[photo of the founder]">',
+ ),
+ ).toBe(
+ '<script type="application/ld+json">{"author":""}</script>' +
+ '<div class="cta"><span>Call today</span></div>' +
+ '<img src="https://cdn.acme.com/a.jpg" alt="">',
+ );
+ });
+
+ it("clears a JSON-LD's placeholders in its strings, and keeps it JSON", () => {
+ const out = withoutPlaceholders(
+ `<script type="application/ld+json">${FAQ}</script>` +
+ '<script type="application/ld+json">{"@type":"Person","name":"Ada","sameAs":["https://x.example/ada"]}</script>',
+ );
+ const faq = jsonLdOf(out);
+ expect(faq.mainEntity).toHaveLength(2);
+ expect(faq.mainEntity[0].acceptedAnswer.text).toBe('About four minutes. It costs .');
+ expect(out).toContain(
+ '<script type="application/ld+json">{"@type":"Person","name":"Ada","sameAs":["https://x.example/ada"]}</script>',
+ );
+ // One that doesn't parse is left as written.
+ const broken = '<script type="application/ld+json">{"name":"[brand]",</script>';
+ expect(withoutPlaceholders(broken)).toBe(broken);
+ });
+
+ it("keeps a heading's own brackets as written", () => {
+ const html = '<h1>Best CRM tools [2026 Update]</h1><p>Brew it slowly.</p>';
+ expect(withoutPlaceholders(html)).toBe(html);
+ });
+});
+
+describe('pageFields', () => {
+ const html =
+ '<h1>How to brew pour-over coffee</h1>' +
+ '<p>Pour-over coffee takes a dripper, a filter and four minutes. You pour 1.5 litres of hot water over the grounds in slow circles, and the coffee drips through. It tastes cleaner than a French press.</p>' +
+ '<p><img src="https://cdn.acme.com/pour.jpg" alt="Pouring"></p>';
+
+ it('reads them from the page: its title, first paragraph, a slug, its author and first image', () => {
+ expect(
+ pageFields({
+ title: 'How to brew pour-over coffee',
+ html,
+ sourceUrl: null,
+ author: ada,
+ teamByline: 'Acme Team',
+ }),
+ ).toEqual({
+ metaTitle: 'How to brew pour-over coffee',
+ metaDescription:
+ 'Pour-over coffee takes a dripper, a filter and four minutes. You pour 1.5 litres of hot water over the grounds in slow circles, and the coffee drips through.',
+ slug: 'how-to-brew-pour-over-coffee',
+ author: 'Ada Lovelace',
+ featuredImage: 'https://cdn.acme.com/pour.jpg',
+ });
+ });
+
+ it("keeps a live page's address as it reads, and the team byline with no author", () => {
+ const fields = (sourceUrl: string) =>
+ pageFields({
+ title: 'Pour-over',
+ html: '<p>Short.</p>',
+ sourceUrl,
+ author: null,
+ teamByline: 'Acme Team',
+ });
+ expect(fields('https://acme.com/blog/pour-over-guide/')).toMatchObject({
+ slug: 'pour-over-guide',
+ author: 'Acme Team',
+ featuredImage: null,
+ metaDescription: 'Short.',
+ });
+ expect(fields('https://acme.com/blog/x/index.html').slug).toBe('x');
+ expect(fields('https://acme.com/tr/%C3%A7ay-rehberi?ref=1').slug).toBe('çay-rehberi');
+ });
+
+ it('gives no slug for a title with none to give', () => {
+ expect(
+ pageFields({ title: '東京ガイド', html: '', sourceUrl: null, author: null, teamByline: '' })
+ .slug,
+ ).toBe('');
+ });
+
+ it('cuts a first sentence too long to fit at a word', () => {
+ const long = `${'Pour slowly '.repeat(20)}now.`;
+ const { metaDescription } = pageFields({
+ title: 'x',
+ html: `<p>${long}</p>`,
+ sourceUrl: null,
+ author: null,
+ teamByline: 'Acme Team',
+ });
+ expect(metaDescription.length).toBeLessThanOrEqual(160);
+ expect(metaDescription.endsWith('slowly…')).toBe(true);
+ });
+});
+
+describe('withoutLeadingH1', () => {
+ it('drops the H1 the title field holds, and nothing else', () => {
+ expect(withoutLeadingH1('<h1>Pour-over</h1><p>Brew.</p><h1>Second</h1>')).toBe(
+ '<p>Brew.</p><h1>Second</h1>',
+ );
+ expect(withoutLeadingH1('<p>Intro.</p><h1>Late</h1>')).toBe('<p>Intro.</p><h1>Late</h1>');
+ });
+});
+
+describe('pasteChecks', () => {
+ it('counts the links read and those that don’t answer, the images with no alt text, and own code', () => {
+ const checks = new Map<string, LinkCheck>([
+ [
+ 'https://ok.example/',
+ { href: 'https://ok.example/', status: 'ok', httpStatus: 200, date: null },
+ ],
+ [
+ 'https://gone.example/',
+ { href: 'https://gone.example/', status: 'broken', httpStatus: 404, date: null },
+ ],
+ // A site that refused the read (a bot wall): no word on it.
+ [
+ 'https://walled.example/',
+ { href: 'https://walled.example/', status: 'unknown', httpStatus: 403, date: null },
+ ],
+ ]);
+ expect(
+ pasteChecks(
+ '<p><a href="https://ok.example/">a</a> <a href="https://gone.example/">b</a> <a href="https://gone.example/">c</a> <a href="https://new.example/">n</a> <a href="https://walled.example/">w</a> <a href="/local">d</a></p>' +
+ '<img src="https://cdn.acme.com/a.jpg" alt="A"><img src="https://cdn.acme.com/b.jpg" alt=" ">' +
+ '<div class="cta" style="color:red">x</div><script type="application/ld+json">{}</script>',
+ checks,
+ ),
+ ).toEqual({
+ links: { count: 4, read: 2, broken: 1 },
+ images: { count: 2, withoutAlt: 1 },
+ ownCode: 1,
+ });
+ });
+});
diff --git a/web/src/lib/content/ready-to-publish.ts b/web/src/lib/content/ready-to-publish.ts
new file mode 100644
index 000000000..92e0fe811
--- /dev/null
+++ b/web/src/lib/content/ready-to-publish.ts
@@ -0,0 +1,337 @@
+import { slugify } from '@/lib/slug';
+import type { ContentAuthor } from './authors';
+import { cleanHref, jsonLdText } from './html-allowlist';
+import { firstImage } from './page-schema';
+import { PLACEHOLDER, placeholderIn } from './placeholders';
+import type { LinkCheck } from './quality-checks';
+
+/**
+ * Ready to publish (Content Editor Quality spec §7b, CE-057): what an export
+ * gives, and what it checks first. The content is the page's clean HTML
+ * (publishableHtml); what goes next to it are the page fields, read from
+ * the page itself, never written for it. No export carries a placeholder:
+ * the person fills them, or the sentences with one are left out (one in a
+ * heading or the title is filled first: a heading has no sentence to drop).
+ */
+
+/**
+ * HTML as a fragment, in a document of its own: nothing in it loads or runs
+ * (an image, an HTML block's code), and a script at its start stays where
+ * it is (DOMParser's body would move it).
+ */
+export function fragment(html: string): HTMLElement {
+ const doc = document.implementation.createHTMLDocument('');
+ const root = doc.createElement('div');
+ root.innerHTML = html;
+ return root;
+}
+
+/** Headings: a placeholder there is filled first, never left out. */
+const HEADINGS = 'h1, h2, h3, h4, h5, h6';
+
+/** The text nodes of a page, in order, outside its code (JSON-LD is read apart). */
+function textNodes(root: Node): Text[] {
+ const out: Text[] = [];
+ const walk = (node: Node) => {
+ for (const child of Array.from(node.childNodes)) {
+ if (child.nodeType === Node.TEXT_NODE) out.push(child as Text);
+ else if (child.nodeType === Node.ELEMENT_NODE) {
+ const tag = (child as Element).tagName.toLowerCase();
+ if (tag !== 'script' && tag !== 'style' && tag !== 'template') walk(child);
+ }
+ }
+ };
+ walk(root);
+ return out;
+}
+
+/** A text that can be read and changed in place. */
+interface Field {
+ get: () => string;
+ set: (value: string) => void;
+}
+
+/** A JSON value's strings, each as a field: its arrays and objects are no placeholders. */
+function jsonStrings(value: unknown): Field[] {
+ if (typeof value !== 'object' || value === null) return [];
+ const box = value as Record<string, unknown>;
+ return Object.keys(box).flatMap((key) =>
+ typeof box[key] === 'string'
+ ? [{ get: () => box[key] as string, set: (next: string) => void (box[key] = next) }]
+ : jsonStrings(box[key]),
+ );
+}
+
+/**
+ * Where a placeholder can hide besides the words: alt and title text, and
+ * JSON-LD's strings. A JSON-LD changed is written again as JSON; one that
+ * doesn't parse is left as written, read for none.
+ */
+function otherText(root: HTMLElement): Field[] {
+ const attributes = Array.from(root.querySelectorAll('[alt], [title]')).flatMap((el) =>
+ ['alt', 'title']
+ .filter((name) => el.hasAttribute(name))
+ .map((name) => ({
+ get: () => el.getAttribute(name) ?? '',
+ set: (value: string) => el.setAttribute(name, value),
+ })),
+ );
+ const jsonLd = Array.from(root.querySelectorAll('script[type="application/ld+json"]')).flatMap(
+ (script) => {
+ let value: unknown;
+ try {
+ value = JSON.parse(script.textContent ?? '');
+ } catch {
+ return [];
+ }
+ return jsonStrings(value).map((field) => ({
+ get: field.get,
+ set: (next: string) => {
+ field.set(next);
+ script.textContent = jsonLdText(value);
+ },
+ }));
+ },
+ );
+ return [...attributes, ...jsonLd];
+}
+
+/** A text's placeholders; a heading's (and a title's) are the agent's own forms only. */
+const count = (text: string, heading = false) => text.match(placeholderIn(heading))?.length ?? 0;
+const inHeading = (node: Node) => !!node.parentElement?.closest(HEADINGS);
+
+/**
+ * The title's placeholders, when the page doesn't hold them already: a
+ * title that is the page's H1 is counted with the page.
+ */
+export function titlePlaceholders(title: string, html: string): number {
+ const h1 = fragment(html).querySelector('h1');
+ const same =
+ h1 && (h1.textContent ?? '').replace(/\s+/g, ' ').trim() === title.replace(/\s+/g, ' ').trim();
+ return same ? 0 : count(title, true);
+}
+
+/**
+ * The placeholders in the HTML an export gives: in its words (an HTML
+ * block's too), its images' alt and title text, and its JSON-LD's strings.
+ */
+export function exportPlaceholders(html: string): number {
+ const root = fragment(html);
+ return (
+ textNodes(root).reduce((sum, node) => sum + count(node.data, inHeading(node)), 0) +
+ otherText(root).reduce((sum, field) => sum + count(field.get()), 0)
+ );
+}
+
+/** Lines whose text is read as sentences. */
+const LINES = `p, li, td, th, dd, dt, figcaption, blockquote, caption, summary, ${HEADINGS}`;
+
+/** A text's sentences, as a reader would split them ("1.5", "e.g." and "acme.com" don't end one). */
+function sentences(text: string): { start: number; end: number; text: string }[] {
+ if (typeof Intl === 'undefined' || !('Segmenter' in Intl)) {
+ return [{ start: 0, end: text.length, text }];
+ }
+ const segmenter = new Intl.Segmenter('en', { granularity: 'sentence' });
+ return Array.from(segmenter.segment(text), (s) => ({
+ start: s.index,
+ end: s.index + s.segment.length,
+ text: s.segment,
+ }));
+}
+
+/** A text with its placeholders as letters, so a period inside one ends no sentence. */
+const masked = (text: string) => text.replace(PLACEHOLDER, (m) => 'x'.repeat(m.length));
+
+/** Cuts ranges of a line's text out of its text nodes, from the end. */
+function cut(nodes: Text[], ranges: { start: number; end: number }[]) {
+ for (const range of [...ranges].sort((a, b) => b.start - a.start)) {
+ let at = 0;
+ for (const node of nodes) {
+ const from = at;
+ at += node.data.length;
+ if (at <= range.start || from >= range.end) continue;
+ node.data =
+ node.data.slice(0, Math.max(0, range.start - from)) +
+ node.data.slice(Math.min(node.data.length, range.end - from));
+ }
+ }
+}
+
+/**
+ * The HTML with each placeholder left out (Export without them): the
+ * sentence it is in goes; one anywhere else (an HTML block's own tags, an
+ * alt text, a JSON-LD string) goes alone. A heading's stays, to be filled first. A
+ * line, an item, a quote or a row left with no words goes too; formatting
+ * in what stays is kept.
+ */
+export function withoutPlaceholders(html: string): string {
+ const root = fragment(html);
+ const one = new RegExp(PLACEHOLDER.source, 'iu');
+ for (const line of Array.from(root.querySelectorAll(LINES)).reverse()) {
+ if (line.matches(HEADINGS)) continue;
+ // The line's own text: a nested line is read on its own.
+ const nodes = textNodes(line).filter((n) => n.parentElement?.closest(LINES) === line);
+ const text = nodes.map((n) => n.data).join('');
+ if (!one.test(text)) continue;
+ cut(
+ nodes,
+ sentences(masked(text)).filter((s) => one.test(text.slice(s.start, s.end))),
+ );
+ for (const node of nodes) node.data = node.data.replace(/\s{2,}/g, ' ');
+ }
+ // What no line holds: the placeholder alone.
+ for (const node of textNodes(root)) {
+ if (inHeading(node)) continue;
+ if (one.test(node.data)) node.data = node.data.replace(PLACEHOLDER, '').replace(/\s{2,}/g, ' ');
+ }
+ for (const field of otherText(root)) {
+ const value = field.get();
+ if (one.test(value))
+ field.set(
+ value
+ .replace(PLACEHOLDER, '')
+ .replace(/\s{2,}/g, ' ')
+ .trim(),
+ );
+ }
+ // A link or a bold its words all went with goes, then a line, an item, a
+ // quote or a list left empty (a table keeps its cells; a row of empty ones goes).
+ for (const el of Array.from(root.querySelectorAll('a, strong, em, b, i, u, s, span, mark'))) {
+ if (!el.textContent?.trim() && !el.querySelector('img, br')) el.remove();
+ }
+ for (const box of Array.from(
+ root.querySelectorAll('p, li, ul, ol, blockquote, figure, dl, dd, dt, tr'),
+ ).reverse()) {
+ if (!box.textContent?.trim() && !box.querySelector('img')) box.remove();
+ }
+ return root.innerHTML;
+}
+
+/** The fields an export gives next to the content, for the CMS's own fields. */
+export interface PageFields {
+ metaTitle: string;
+ metaDescription: string;
+ /** The page's address part; empty when the title gives none (in another script). */
+ slug: string;
+ /** The page's author, the team byline, or empty while the authors aren't read. */
+ author: string;
+ featuredImage: string | null;
+}
+
+/** About the longest a search result shows of a description. */
+export const META_DESCRIPTION_CHARS = 160;
+
+/** The page's first paragraph's sentences, as many as fit, or its words cut at one. */
+function description(root: HTMLElement): string {
+ const first = Array.from(root.querySelectorAll('p')).find(
+ (p) => !p.closest('blockquote, li, td, th') && (p.textContent ?? '').trim(),
+ );
+ const text = (first?.textContent ?? '').replace(/\s+/g, ' ').trim();
+ if (text.length <= META_DESCRIPTION_CHARS) return text;
+ let out = '';
+ for (const s of sentences(text)) {
+ if ((out + s.text).trim().length > META_DESCRIPTION_CHARS) break;
+ out += s.text;
+ }
+ if (out.trim()) return out.trim();
+ const short = text.slice(0, META_DESCRIPTION_CHARS - 1);
+ const space = short.lastIndexOf(' ');
+ return `${space > 0 ? short.slice(0, space) : short}…`;
+}
+
+/** A live page's own address part ("/blog/pour-over"), else the title as one. */
+function slugOf(title: string, sourceUrl: string | null): string {
+ if (sourceUrl) {
+ try {
+ const parts = new URL(sourceUrl).pathname
+ .split('/')
+ .filter((part) => part && !/^index\.\w+$/i.test(part));
+ const last = parts.at(-1);
+ if (last) return decodeURIComponent(last);
+ } catch {
+ // Not an address, or not one to read: the title's slug.
+ }
+ }
+ return slugify(title).slice(0, 80).replace(/-+$/, '');
+}
+
+/**
+ * The page fields, from the page as it goes out: its title, its first
+ * paragraph, its live address or its title as a slug, its author and its
+ * first image.
+ */
+export function pageFields({
+ title,
+ html,
+ sourceUrl,
+ author,
+ teamByline,
+}: {
+ title: string;
+ html: string;
+ sourceUrl: string | null;
+ author: ContentAuthor | null;
+ teamByline: string;
+}): PageFields {
+ return {
+ metaTitle: title.trim(),
+ metaDescription: description(fragment(html)),
+ slug: slugOf(title, sourceUrl),
+ author: author?.name ?? teamByline,
+ featuredImage: firstImage(html),
+ };
+}
+
+/** The page's leading H1, for a destination whose title field holds it. */
+export function withoutLeadingH1(html: string): string {
+ const root = fragment(html);
+ const first = Array.from(root.children).find((el) => el.textContent?.trim() || el.matches('img'));
+ if (first?.matches('h1')) first.remove();
+ return root.innerHTML.replace(/^\s+/, '');
+}
+
+/**
+ * What Ready to paste checks besides placeholders: the links answer (as
+ * Trust's link reads found them), the images have alt text, and what an
+ * HTML block keeps of its own (classes, styles, scripts) goes out as written.
+ */
+export interface PasteChecks {
+ /**
+ * The page's links, how many were read (Trust's link reads: one that
+ * answered, or didn't; a read the site refused or timed out is none), and
+ * how many of those don't answer.
+ */
+ links: { count: number; read: number; broken: number };
+ images: { count: number; withoutAlt: number };
+ /** Elements that carry a class, a style or a script (JSON-LD aside). */
+ ownCode: number;
+}
+
+export function pasteChecks(html: string, linkChecks: ReadonlyMap<string, LinkCheck>): PasteChecks {
+ const root = fragment(html);
+ const hrefs = [
+ ...new Set(
+ Array.from(root.querySelectorAll('a[href]'))
+ .map((a) => cleanHref(a.getAttribute('href') ?? ''))
+ .filter((href): href is string => !!href && /^https?:\/\//i.test(href)),
+ ),
+ ];
+ const images = Array.from(root.querySelectorAll('img'));
+ return {
+ links: {
+ count: hrefs.length,
+ read: hrefs.filter((h) => {
+ const status = linkChecks.get(h)?.status;
+ return status === 'ok' || status === 'broken';
+ }).length,
+ broken: hrefs.filter((h) => linkChecks.get(h)?.status === 'broken').length,
+ },
+ images: {
+ count: images.length,
+ withoutAlt: images.filter((img) => !(img.getAttribute('alt') ?? '').trim()).length,
+ },
+ ownCode: root.querySelectorAll(
+ '[class], [style], script:not([type="application/ld+json"]), style, iframe',
+ ).length,
+ };
+}