|
| 1 | +/** |
| 2 | + * The headline accent word. |
| 3 | + * |
| 4 | + * The sponsor-break mockups render ONE word of the headline in the accent |
| 5 | + * colour ("Your next idea. *Live.*"). That is a real difference in what the |
| 6 | + * advertiser bought — the accent is the loudest pixel on a break card — so it |
| 7 | + * has to be authored, stored and reproduced rather than guessed at render |
| 8 | + * time. |
| 9 | + * |
| 10 | + * ## Why a markup rather than a second column |
| 11 | + * |
| 12 | + * A `title_accent_word` column has to answer "which occurrence?" the first |
| 13 | + * time an advertiser writes "Live. Really live." A span written INSIDE the |
| 14 | + * title cannot be ambiguous: it names a position, not a word. The title is |
| 15 | + * stored verbatim, markers included, so the authored intent survives an edit |
| 16 | + * round-trip through the console. |
| 17 | + * |
| 18 | + * ## Why one span, and one parser |
| 19 | + * |
| 20 | + * Three surfaces read the same string and only one of them can draw an accent: |
| 21 | + * the Desktop break cards can, and the CLI dock, the inline terminal card and |
| 22 | + * the house-ad width budget cannot. Every one of those has to receive the |
| 23 | + * PLAIN string — a terminal that prints `*Live.*` has shipped our markup to a |
| 24 | + * user. So the split and the strip live here, together, and every surface |
| 25 | + * imports one of the two rather than writing its own regex. |
| 26 | + * |
| 27 | + * More than one span is REFUSED at the console rather than rendered: two |
| 28 | + * accents is not an emphasis, it is a rainbow, and quietly honouring the first |
| 29 | + * would ship copy the advertiser did not preview. The renderers still degrade |
| 30 | + * safely — {@link splitAccentSpan} answers "no accent" for a title it will not |
| 31 | + * vouch for, and {@link stripAccentSpan} still removes the markers — because a |
| 32 | + * row written before this validation existed must not print asterisks. |
| 33 | + * |
| 34 | + * ## Character budgets count the PLAIN text |
| 35 | + * |
| 36 | + * `*` is markup, not copy. A 28-character break title with an accented last |
| 37 | + * word is 28 characters on screen and 30 in the database, and charging the |
| 38 | + * advertiser two characters for a formatting mark they never see is the kind |
| 39 | + * of limit nobody can explain. Callers of the break copy limits measure |
| 40 | + * {@link stripAccentSpan} of the title. |
| 41 | + */ |
| 42 | + |
| 43 | +/** One `*…*` run. Non-greedy by construction: the body may not contain `*`. */ |
| 44 | +const ACCENT_SPAN = /\*([^*]+)\*/g |
| 45 | + |
| 46 | +export interface AccentSpanParts { |
| 47 | + /** Text before the accent — the whole plain title when there is no accent. */ |
| 48 | + before: string |
| 49 | + /** The accented run, without its markers. Empty when there is no accent. */ |
| 50 | + accent: string |
| 51 | + after: string |
| 52 | + /** The whole title with every marker removed. What non-accent surfaces get. */ |
| 53 | + plain: string |
| 54 | +} |
| 55 | + |
| 56 | +/** How many well-formed spans a title carries. */ |
| 57 | +export function countAccentSpans(title: string): number { |
| 58 | + return title.match(ACCENT_SPAN)?.length ?? 0 |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * The title with the markers removed. |
| 63 | + * |
| 64 | + * Only PAIRED markers are removed. A lone `*` is left alone: an advertiser |
| 65 | + * writing "3 * 4 faster" wrote an asterisk on purpose, and silently deleting |
| 66 | + * it would corrupt copy to tidy up markup that is not there. |
| 67 | + */ |
| 68 | +export function stripAccentSpan(title: string): string { |
| 69 | + return title.replace(ACCENT_SPAN, '$1') |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Split a title into its accent parts. TOTAL: never throws, and never returns |
| 74 | + * an accent it is not sure about. |
| 75 | + * |
| 76 | + * A title with zero spans, or with more than one, comes back as plain text in |
| 77 | + * `before` with an empty `accent`. That is the same answer a surface that |
| 78 | + * cannot draw an accent would get, which is exactly the fallback wanted: an |
| 79 | + * unvouched-for title renders as ordinary copy, never as markup. |
| 80 | + */ |
| 81 | +export function splitAccentSpan(title: string): AccentSpanParts { |
| 82 | + const plain = stripAccentSpan(title) |
| 83 | + if (countAccentSpans(title) !== 1) { |
| 84 | + return { before: plain, accent: '', after: '', plain } |
| 85 | + } |
| 86 | + // Re-run without the global flag: a `g` regex carries `lastIndex` across |
| 87 | + // calls, and a shared module-level literal would answer differently on |
| 88 | + // every other invocation. |
| 89 | + const match = /\*([^*]+)\*/.exec(title) |
| 90 | + if (!match || match.index === undefined) { |
| 91 | + return { before: plain, accent: '', after: '', plain } |
| 92 | + } |
| 93 | + return { |
| 94 | + before: title.slice(0, match.index), |
| 95 | + accent: match[1] ?? '', |
| 96 | + after: title.slice(match.index + match[0].length), |
| 97 | + plain, |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +/** Whether this title carries exactly one accent a surface may draw. */ |
| 102 | +export function hasAccentSpan(title: string): boolean { |
| 103 | + return splitAccentSpan(title).accent.length > 0 |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * Why this title's accent markup is not acceptable, in the advertiser's terms, |
| 108 | + * or null. |
| 109 | + * |
| 110 | + * Checked on write in the console, never at render: a stored row that predates |
| 111 | + * this rule still has to draw. |
| 112 | + */ |
| 113 | +export function accentSpanIssue(title: string): string | null { |
| 114 | + const spans = countAccentSpans(title) |
| 115 | + if (spans > 1) { |
| 116 | + return 'Accent one word only — remove the extra *asterisks* from this title.' |
| 117 | + } |
| 118 | + // An odd marker left over after the paired ones are consumed is a span the |
| 119 | + // advertiser started and did not close. Saying so beats rendering a stray |
| 120 | + // asterisk on a card they cannot edit from. |
| 121 | + if (stripAccentSpan(title).includes('*')) { |
| 122 | + return 'Close the accent with a second asterisk, like *this*.' |
| 123 | + } |
| 124 | + return null |
| 125 | +} |
| 126 | + |
| 127 | +/** |
| 128 | + * Apply an accent to `word` inside `title`, or clear it. What the console's |
| 129 | + * "Accent a word" control writes, so nobody is asked to type asterisks. |
| 130 | + * |
| 131 | + * The FIRST occurrence, matched on a word boundary, so accenting "Live" in |
| 132 | + * "Live it live" marks the word the advertiser clicked rather than a fragment |
| 133 | + * of another one. A word the title does not contain leaves the title alone. |
| 134 | + */ |
| 135 | +export function setAccentWord(title: string, word: string | null): string { |
| 136 | + const plain = stripAccentSpan(title) |
| 137 | + if (!word) return plain |
| 138 | + const target = word.trim() |
| 139 | + if (!target) return plain |
| 140 | + const at = accentWordOffset(plain, target) |
| 141 | + if (at < 0) return plain |
| 142 | + return `${plain.slice(0, at)}*${target}*${plain.slice(at + target.length)}` |
| 143 | +} |
| 144 | + |
| 145 | +/** Where a whole-word occurrence of `word` starts in `plain`, or -1. */ |
| 146 | +function accentWordOffset(plain: string, word: string): number { |
| 147 | + for (const candidate of accentWordCandidates(plain)) { |
| 148 | + if (candidate.text === word) return candidate.at |
| 149 | + } |
| 150 | + return -1 |
| 151 | +} |
| 152 | + |
| 153 | +/** |
| 154 | + * The words of a title an advertiser may accent, in order, with their |
| 155 | + * offsets. Whitespace-separated runs — punctuation stays attached, because |
| 156 | + * the mockup's accent is "Live." with its full stop inside the colour. |
| 157 | + */ |
| 158 | +export function accentWordCandidates( |
| 159 | + title: string, |
| 160 | +): { text: string; at: number }[] { |
| 161 | + const plain = stripAccentSpan(title) |
| 162 | + const words: { text: string; at: number }[] = [] |
| 163 | + const pattern = /\S+/g |
| 164 | + let match: RegExpExecArray | null |
| 165 | + while ((match = pattern.exec(plain)) !== null) { |
| 166 | + words.push({ text: match[0], at: match.index }) |
| 167 | + } |
| 168 | + return words |
| 169 | +} |
0 commit comments