# Capability: Create from designer HTML (HtmlComponent)

## Intent phrases

- build from designer HTML
- HTML dump
- create from HTML prototype
- import HTML landing page
- HtmlComponent from HTML
- custom HTML page
- bring this HTML into the site
- designed HTML page
- load HtmlComponent external scripts

## Requires capabilities

`pages` or `articles`

## Prerequisites

- Read `cms-edit://customer/pages` (or `articles`) and `cms-edit://customer/components-index`
- `cms_edit ["schema", "htmlComponent"]` — use this space’s field IDs (`rawHtml` vs `htmlContent`, `customJs` vs `jsContent`). **`externalScripts` is the same field id on every space.** If `schema` does **not** list `externalScripts`, **stop** and ask for Contentful migration 28 — do not stuff vendor/long JS into `customJs` (Contentful Text is ~50k characters).
- Designer HTML / prototype available (file, paste, or Drive). **Do not** paste a multi-megabyte dump with `data:` URIs into Contentful.
- For ordinary Google Doc / Figma-to-registered-stack briefs with no custom CSS/JS, use `task-create-from-document` instead.

## When to use this playbook

Use this when the source is a **designer HTML dump**, interactive prototype, or a block that needs a **third-party JS library**. Do not flatten custom CSS/JS/scroll into registered cards because the copy looks like a card grid. Do not paste the document into one `HtmlComponent`.

## Shelf, before any CMS write

For **each** `<section>`, pick one shelf. A page is a mix. The default is not one `HtmlComponent` per section, and not one `HtmlComponent` for the whole file.

| Shelf | When | Action |
|-----|------|--------|
| Site layout | Nav, footer, menu, contact panel | Leave it in the file. The page template supplies chrome. |
| Registered component | An editor will change the words, media, or links, **or** the section repeats (on this page or another) | Stop the HTML paste for that section. Reuse a registered type, or build a customer component with the create-component skill (four layers, real links so `useAnalytics()` runs, the site form for any form). |
| `HtmlComponent` | A developer-owned island (motion, a tool demo, vendor JS, or a one-off) **and** an editor will not change the words, media, or links, **and** the section does not repeat | Continue with this playbook. |

## Keep or promote

Make this call for every section, and again when starting a **first version** of a section that has no designed file yet. Write the answer on the soft-proof. Do not leave it implied by which playbook you opened.

| Call | When |
|-----|------|
| Site layout | Nav, footer, menu, contact. Not a component decision. |
| Promote | Required when an editor will change the words, media, or links, or the section repeats (on this page or another). Also a valid first-version choice when you expect that later. Use an existing registered type, or build one. |
| Keep `HtmlComponent` | A developer will own it, editors will not change the words, media, or links, and it does not repeat. |

A layout match (FAQ accordion → FAQs) names the type you would promote to. It does not apply the promotion. A custom layout does not keep the section as HTML. Ask, then record **keep** or **promote**.

## Convert the specimen

Do this before `rawHtml` / `customCss` / `customJs` are written:

- Drop `@font-face` and embedded font files. The site already loads the brand font.
- Drop a second `:root` palette. Colours and type use named classes from `docs/cms-guidelines/html-component-style-guide.md`. Motion those classes cannot express goes in `customCss`, using the site’s existing tokens.
- Do not paste a `<form>`. Forms use the site’s registered form and server submit.
- Clicks that must be measured use a registered link (`useAnalytics()`). `HtmlComponent` does not emit analytics. Do not push to `dataLayer` from `customJs`.
- One page has one `h1`. Set `isHero` only when this block is that heading.
- `markdownContent` is the visible sentences, not the layout chrome.

Mock **nav and footer** in the HTML: strip. The page **template** supplies chrome.

## Split HtmlComponents (mixed pages)

Mixed pages are expected: registered types **plus** one or more `htmlComponent` entries.

**Do split** when the recorded call is **promote**, remaining HTML would exceed field limits, or adjacent HtmlComponent blocks **do not share** JS/CSS/sticky state.

**Do not split** when one behaviour spans markup whose sections are already **kept** as HtmlComponent (sticky rail + stacked cards), or several HtmlComponent sections share one stylesheet / SVG sprite / library. An editor-owned or repeated section stays registered even when the sticky behaviour crosses it.

Rules:

1. A layout match names a candidate registered type. Apply it only when the recorded call is **promote**. A **keep** stays HtmlComponent. Do not pull a registered neighbour onto HtmlComponent because it shares CSS or JS.
2. Merge consecutive sections already shelved as HtmlComponent that share CSS/JS into **one** HtmlComponent. An editor-owned or repeated section stays a registered component.
3. Start a new HtmlComponent only at a clean boundary (no shared script, no sticky parent).
4. Never one HtmlComponent per `<section>` by default.

## External scripts (non-negotiable)

Put third-party libraries in **`externalScripts`**, not `<script src>` in HTML and not `createElement('script')` inside `customJs`.

Allowed values:

- Same-origin paths under `/html-components/` that end in `.js` (no `..`, no query, no hash). Prefer files in the site repo at `public/html-components/<feature>/….js`.
- HTTPS URLs whose hostname is already listed on the site’s `CmsRendererConfig.htmlComponentExternalScriptHosts`. If the site sets CSP, that host must also be in `script-src`.

If the host is **not** allowlisted, **stop** — do not invent a CDN load and do not patch renderer config without a human.

Rejected (renderer drops these; preview/dev may warn): `http:`, `javascript:`, protocol-relative `//…`, path `..`, query/hash on same-origin paths.

Max **10** URLs per entry. Duplicate URLs on one page share one `next/script` id. Externals load with `scriptStrategy` (default `afterInteractive`). **`customJs` runs only after every listed src is ready**; if any src fails, inline JS is not injected.

### How to set `externalScripts`

It is an **Array of Symbol**, not a string. Scalar `set` is rejected.

```text
cms_edit ["set", "@cN", "externalScripts", "--json", "[\"/html-components/feature/lib.js\"]"]
```

On `create from-json`, put the same array under `fields.externalScripts`. Run `schema htmlComponent` first — unknown keys fail (`html` vs `htmlContent`).

Then set HTML/CSS/JS. Hosted MCP **rejects** `--content` / `--content-base64` / `--json` over **8192 bytes**. Do not stub-create then patch. Write files on disk, then:

```
cms_edit_request_staged_upload  kind=text  mimeType=text/html|text/css|text/javascript
# run curlCommand
cms_edit ["set", "@cN", "rawHtml", "--staged", "<id>"]
```

Contentful Text fields themselves cap around **50k characters**. Put vendor/long JS in `externalScripts` (`/html-components/….js` in the site repo), not `customJs`.

`diff` prints bytes + sha256 + a 20-line head for long Text — not the full body. Confirm integrity with `["read", "@cN", "--hash"]` (full body only on `read` without `--hash`).

## Media first (mandatory)

1. Extract binary media from `data:` URIs and local files. Upload via `task-media-reuse-and-upload` (search/reuse first).
2. Rewrite HTML `src` / `poster` to Contentful or CDN URLs. Never leave base64 in the field.
3. `markdownContent` = user-visible copy and structure. Omit layout chrome and JS behaviour.

## HtmlComponent authoring

1. Follow the site HTML style guide (`docs/cms-guidelines/html-component-style-guide.md` when available).
2. Scope all JS queries to injected `hcRoot`.
3. `isHero` — `true` if this block contains the page `<h1>`. `fullWidth` — as needed for full-bleed sections.
4. Set `cmsLabel` immediately (never leave “New Generic”).
5. Dual-write legacy + core field IDs when `schema htmlComponent` shows both.
6. Draft only — never publish unless the human explicitly asks.

## Steps

1. For each section, record **keep** or **promote** (site layout is strip-only). Promote when an editor will change the words, media, or links, or the section repeats. Otherwise ask. A first version with no file yet gets the same call. Merge only sections already kept as HtmlComponent that share CSS/JS.
2. List vendor libraries. Copy same-origin files into `public/html-components/<feature>/` or confirm the HTTPS host is already allowlisted.
3. Post a soft-proof (ordered stack, **keep or promote per section**, `externalScripts` URLs) and get a light human **yes**.
4. Media pipeline: extract, upload, rewrite URLs.
5. Create or open the page/article. Add registered components where decided.
6. For each HtmlComponent: `add htmlComponent` (or `create from-json --staged` / `--dry-run --strict` with this space’s field keys). Do not put HTML/CSS/JS over 8k in `--json`.
7. Set `externalScripts` with `--json`. Set HTML/CSS/JS via `--staged` when over 8k. Set `cmsLabel`.
8. `diff` → `read --hash` → `save` (draft).
9. `cms_edit ["preview", "urls", "<slug>"]` — follow `task-preview-verify`. Confirm libraries load (no `createElement('script')`, no dropped-URL warnings).

## Confirmation gates

1. Soft-proof (mixed stack, keep or promote per section, script URLs) + human yes
2. Media extracted (no `data:` in saved HTML)
3. `schema htmlComponent` field IDs used
4. Libraries only via `externalScripts`
5. `diff` before `save`

## Out of scope

- Publish
- Flattening custom interaction into cards/lists because copy looks similar
- One HtmlComponent per `<section>` by default
- Rewriting source copy
- Pasting the raw designer file into HTML
- Pasting `<form>`, `@font-face`, or a second `:root` palette into an HtmlComponent
- Pushing analytics events from `customJs` (`dataLayer`, `gtag`)
- Adding a new CDN host to `htmlComponentExternalScriptHosts` without a human
- Scalar `set` of a single URL string onto `externalScripts`

## Related resources

- `cms-edit://customer/pages`
- `cms-edit://customer/task-create-from-document`
- `cms-edit://customer/task-create-page`
- `cms-edit://customer/task-media-reuse-and-upload`
- `cms-edit://customer/task-preview-verify`
- `cms-edit://customer/task-publish-handoff`
- `cms_edit ["help", "fields-html-component"]`
