Skip to content

The OSINT investigator's workbench. One case, one folder, every tool. Local-first: your media and your investigations never leave your machine.

JavaScript
29.8 KB
Raw Blame
/**
 * Post Composer thread logic — pure functions, no Svelte/DOM here.
 *
 * A thread template is a **body string with tokens** (#place, #coordinates, …).
 * The analyst arranges the tokens in the order and line layout they want; a
 * template is that layout plus the default mention, the media-tweet flag and any
 * boilerplate extra tweets. It carries no case content — the tokens are filled
 * from the live draft at build time. Some accounts drop the place line or post
 * bare coordinates; a token body expresses either without special-casing.
 */

import { entityReference, mediaReference } from './markdown.js';

// The tokens an analyst can drop into a thread body. `sample` feeds the editor
// preview so the layout reads before any real draft exists.
export const TWEET_TOKENS = [
  { tag: '#place', label: 'Place', sample: 'Bakhmut, Donetsk, Ukraine' },
  { tag: '#pluscode', label: 'Plus code', sample: '8FPGXXXX+XX' },
  { tag: '#coordinates', label: 'Coordinates', sample: '48.850000, 2.350000' },
  { tag: '#description', label: 'Description', sample: 'Rooftop match against the reference imagery' },
  { tag: '#mention', label: 'Mention', sample: '@GeoConfirmed' },
  { tag: '#source', label: 'Source', sample: 'https://x.com/example/status/1' },
  { tag: '#date', label: 'Date', sample: '2025-10-24 14:30 UTC+3' },
];

export const MAX_POST_MEDIA = 4;

/**
 * Publishing targets share the same prepared thread, but apply their own
 * character rules and composer address.
 *
 * Mastodon was here and was dropped: there is no one address to open. Its share
 * link is a redirector that asks which server first, so the app could not say
 * where the thread was going, and the extension could not fill a page nobody
 * knew the name of — one target of three behaving differently, invisibly, until
 * Publish was pressed. A draft saved with it opens on X (normalizePostTarget).
 */
export const POST_TARGETS = Object.freeze({
  x: { id: 'x', label: 'X', limit: 280, limitLabel: '280' },
  bluesky: { id: 'bluesky', label: 'Bluesky', limit: 300, limitLabel: '300' },
});

const URL_RE = /https?:\/\/\S+/g;

/** Accept stored legacy drafts and unknown values without breaking the form. */
export function normalizePostTarget(target) {
  return Object.hasOwn(POST_TARGETS, target) ? target : 'x';
}

/** Return a target profile, falling back to X for old or hand-edited drafts. */
export function postTarget(target) {
  return POST_TARGETS[normalizePostTarget(target)];
}

/** X reserves a fixed length for every HTTP(S) URL, whatever the URL is. */
export function weightedUrlLength(text) {
  const value = String(text ?? '');
  const stripped = value.replace(URL_RE, '');
  const urls = value.match(URL_RE)?.length ?? 0;
  return [...stripped].length + urls * 23;
}

/** Bluesky limits posts by visible Unicode grapheme clusters, not code units. */
export function graphemeLength(text) {
  const value = String(text ?? '');
  if (typeof Intl?.Segmenter === 'function') {
    return [...new Intl.Segmenter(undefined, { granularity: 'grapheme' }).segment(value)].length;
  }
  return [...value].length;
}

/** Count text according to the selected target's documented rule. */
export function postCharacterCount(target, text) {
  return normalizePostTarget(target) === 'bluesky'
    ? graphemeLength(text)
    : weightedUrlLength(text);
}

/** Build a compose handoff for the first post. Replies stay on the clipboard. */
export function postComposeUrl(target, text) {
  const encoded = encodeURIComponent(String(text ?? ''));
  return normalizePostTarget(target) === 'bluesky'
    ? `https://bsky.app/intent/compose?text=${encoded}`
    : `https://x.com/intent/post?text=${encoded}`;
}

/**
 * Where the capture extension opens the thread.
 *
 * The **full** composer rather than the intent page, because that is the one that
 * builds a thread: the intent posts a single message, and the whole point here is
 * the run of posts under the first.
 *
 * The text rides in the URL as well as being typed, so the first post is filled
 * even when the page changes under the script and nothing else lands.
 */
export function postHandoffUrl(target, text) {
  const encoded = encodeURIComponent(String(text ?? ''));
  return normalizePostTarget(target) === 'bluesky'
    ? `https://bsky.app/intent/compose?text=${encoded}`
    : `https://x.com/compose/post?text=${encoded}`;
}

/** What one post may carry: four images, or one video, is every site's rule. */
export const MAX_HANDOFF_FILES = 4;

/**
 * The thread as the extension hands it over: one entry per post, in order, each
 * with the text to type and the case files to attach.
 *
 * A post with no text and no file is not a post; one with a picture and no text
 * is. Anything past the per-post limit is **counted, not dropped in silence** —
 * the caller says how many stayed behind, since the alternative is a thread that
 * looks complete and is missing an image.
 */
export function handoffThread(posts) {
  const kept = [];
  let dropped = 0;
  for (const post of posts ?? []) {
    const text = String(post?.text ?? '');
    const files = (post?.files ?? []).filter(Boolean);
    if (!text.trim() && !files.length) continue;
    if (files.length > MAX_HANDOFF_FILES) dropped += files.length - MAX_HANDOFF_FILES;
    kept.push({ text, files: files.slice(0, MAX_HANDOFF_FILES) });
  }
  return { posts: kept, dropped };
}

/**
 * Turn a prepared social thread into a readable case-note report.
 *
 * ``proofEntity`` and ``mediaEntities`` use the Notebook's local reference
 * syntax, so saved reports keep clickable case links and render their evidence
 * from local media instead of copying files or embedding base64 data.
 */
export function postReportMarkdown({
  title,
  place,
  plusCode,
  coordinates,
  points = [],
  dms,
  mapLinks = {},
  description,
  source,
  attachments = [],
  proofEntity = null,
  mediaEntities = [],
} = {}) {
  const clean = (value) => String(value ?? '').trim();
  const cell = (value) => clean(value).replace(/\|/g, '\\|').replace(/[\r\n]+/g, ' ');
  const inlineCode = (value) => `\`${clean(value).replace(/`/g, '\\`')}\``;
  const reportTitle = clean(title) || 'Untitled report';
  const location = [
    clean(place) && ['Place', cell(place)],
    clean(coordinates) && ['Coordinates', inlineCode(coordinates)],
    clean(dms) && ['DMS', inlineCode(dms)],
    clean(plusCode) && ['Plus code', inlineCode(plusCode)],
  ].filter(Boolean);
  const mapRows = Object.entries(mapLinks ?? {})
    .map(([label, href]) => [cell(label), clean(href)])
    .filter(([, href]) => /^https?:\/\//i.test(href));
  if (mapRows.length) {
    location.push([
      'Maps',
      mapRows.map(([label, href]) => `[${label}](${href})`).join(' · '),
    ]);
  }
  const cleanAttachments = [...new Set(attachments.map(clean).filter(Boolean))];
  const sections = [`# ${reportTitle}`];

  if (location.length) {
    sections.push([
      '## Location',
      '',
      '| Detail | Value |',
      '| --- | --- |',
      ...location.map(([label, value]) => `| ${label} | ${value} |`),
    ].join('\n'));
  }
  // Every point the proof argues, once there is more than one. Location above
  // is the conclusion — the address the post is written about — and this is the
  // rest of what the picture shows, which a tweet has no room to name.
  const listed = (points ?? []).filter((one) => clean(one?.coords));
  if (listed.length > 1) {
    sections.push([
      '## Points',
      '',
      '| Point | Coordinates |',
      '| --- | --- |',
      ...listed.map((one, i) => `| ${cell(one.label) || `Point ${i + 1}`} | ${inlineCode(one.coords)} |`),
    ].join('\n'));
  }
  if (clean(description)) sections.push(`## Assessment\n\n${clean(description)}`);
  if (clean(source)) sections.push(`## Source\n\n${sourceSection(source)}`);
  const mediaByPath = new Map(
    mediaEntities
      .filter((entity) => entity?.id && entity?.attrs?.path)
      .map((entity) => [entity.attrs.path, entity]),
  );
  const evidence = [];
  if (proofEntity?.id && proofEntity.attrs?.path) {
    evidence.push([
      '### Proof',
      entityReference(proofEntity),
      '',
      '::: center',
      `${mediaReference(proofEntity)}{width=100% align=center}`,
      ':::',
    ].join('\n'));
  } else if (cleanAttachments[0]?.startsWith('proofs/')) {
    evidence.push(`### Proof\n\n- ${inlineCode(cleanAttachments[0])}`);
  }

  const mediaPaths = cleanAttachments.filter((path) => !path.startsWith('proofs/'));
  const renderedMedia = mediaPaths.map((path) => {
    const entity = mediaByPath.get(path);
    if (!entity) return `- ${inlineCode(path)}`;
    return [
      `### ${cell(entity.label || path)}`,
      entityReference(entity),
      '',
      '::: center',
      `${mediaReference(entity)}{width=100% align=center}`,
      ':::',
    ].join('\n');
  });
  if (renderedMedia.length) evidence.push(...renderedMedia);
  if (evidence.length) sections.push(`## Evidence\n\n${evidence.join('\n\n')}`);
  return `${sections.join('\n\n')}\n`;
}

/** DOM events are not valid tweet ids when a click handler opens the picker. */
export function normalizePostMediaPickerTarget(target) {
  return Number.isInteger(target) ? target : null;
}

/** Build the conventional prefix used for a tweet that carries media. */
export function mediaTweetLabel(type, number) {
  return `${number}/ ${type === 'video' ? 'Video' : 'Image'}:`;
}

/** Build a media tweet from its conventional prefix and optional body. */
export function mediaTweetText(type, text, number) {
  const label = mediaTweetLabel(type, number);
  return String(text ?? '').trim() ? `${label}\n${String(text).trim()}` : label;
}

/**
 * Change an untouched media prefix when its attachment type changes. Once the
 * analyst edits that prefix, their text is left alone.
 */
export function retargetMediaTweetText(text, currentType, nextType, number) {
  const value = String(text ?? '');
  const nextLabel = mediaTweetLabel(nextType, number);
  if (!value.trim()) return nextLabel;
  if (currentType === 'none') return value;

  const currentLabel = mediaTweetLabel(currentType, number);
  if (value.trim() === currentLabel) return nextLabel;
  if (value.startsWith(`${currentLabel}\n`)) {
    return `${nextLabel}${value.slice(currentLabel.length)}`;
  }
  return value;
}

/** Renumber an untouched media prefix while preserving custom text. */
export function renumberMediaTweetText(text, type, currentNumber, nextNumber) {
  const value = String(text ?? '');
  const currentLabel = mediaTweetLabel(type, currentNumber);
  const nextLabel = mediaTweetLabel(type, nextNumber);
  if (value.trim() === currentLabel) return nextLabel;
  if (value.startsWith(`${currentLabel}\n`)) {
    return `${nextLabel}${value.slice(currentLabel.length)}`;
  }
  return value;
}

/** Create an editable media tweet at its position in the thread. */
export function newPostMediaTweet(id, number, mediaType = 'none') {
  return {
    id,
    text: mediaType === 'none' ? '' : mediaTweetText(mediaType, '', number),
    mediaPaths: [],
    mediaType,
    isMediaTweet: true,
    mediaTextIncludesPrefix: true,
  };
}

/** Return the exact text copied for an extra context or media tweet. */
export function extraPostTweetText(tweet, number) {
  const text = String(tweet?.text ?? '').trim();
  if (tweet?.mediaType === 'none' || !tweet?.mediaPaths?.length) return text;
  return tweet.mediaTextIncludesPrefix ? text : mediaTweetText(tweet.mediaType, text, number);
}

/**
 * Toggle one library item in a tweet's media selection. This composer keeps
 * images and videos in separate tweets, so the other kind replaces the group.
 */
export function togglePostMedia({ mediaPaths = [], mediaType = 'none' }, item) {
  if (!item || !['image', 'video'].includes(item.kind)) {
    return { mediaPaths, mediaType, outcome: 'unsupported' };
  }
  if (mediaPaths.includes(item.path)) {
    return {
      mediaPaths: mediaPaths.filter((path) => path !== item.path),
      mediaType,
      outcome: 'removed',
    };
  }

  const itemType = item.kind === 'video' ? 'video' : 'images';
  if (mediaType !== 'none' && mediaType !== itemType) {
    return { mediaPaths: [item.path], mediaType: itemType, outcome: 'replaced' };
  }
  if (mediaPaths.length >= MAX_POST_MEDIA) {
    return { mediaPaths, mediaType, outcome: 'limit' };
  }
  return {
    mediaPaths: [...mediaPaths, item.path],
    mediaType: itemType,
    outcome: 'added',
  };
}

/** Show only the library items accepted by the tweet's selected media mode. */
export function postMediaForType(items, mediaType) {
  const kind = mediaType === 'video' ? 'video' : mediaType === 'images' ? 'image' : null;
  return kind ? (items ?? []).filter((item) => item.kind === kind) : [];
}

/**
 * Pack proof-linked media into same-type tweets while preserving the order in
 * which each type first appears. Every group respects X's media-item limit.
 */
export function groupPostMedia(mediaPaths, kindForPath) {
  const buckets = new Map();
  for (const path of mediaPaths ?? []) {
    const mediaType = kindForPath(path) === 'video' ? 'video' : 'images';
    if (!buckets.has(mediaType)) buckets.set(mediaType, []);
    buckets.get(mediaType).push(path);
  }

  const groups = [];
  for (const [mediaType, paths] of buckets) {
    for (let i = 0; i < paths.length; i += MAX_POST_MEDIA) {
      groups.push({ mediaType, mediaPaths: paths.slice(i, i + MAX_POST_MEDIA) });
    }
  }
  return groups;
}

// token → the field key it pulls its value from (see tweetFields)
const TOKEN_FIELD = {
  '#place': 'place',
  '#pluscode': 'plusCode',
  '#coordinates': 'coordinates',
  '#description': 'description',
  '#mention': 'mention',
  '#source': 'source',
  '#date': 'date',
};

// A template can omit a draft field by leaving its token out of the body.
// Coordinates also supply the plus code and can resolve a place, so keep that
// field available whenever either of those tokens is present.
const FIELD_TOKENS = {
  description: ['#description'],
  coordinates: ['#coordinates', '#pluscode', '#place'],
  place: ['#place'],
  mention: ['#mention'],
  source: ['#source'],
  date: ['#date'],
};

// A token ends where the word does: `#placement` is text, not `#place` and "ment",
// and a `#dates` hashtag a template already carried stays a hashtag.
const TOKEN_TAIL = '(?![\\p{L}\\p{N}_])';
const TOKEN_RE = new RegExp(
  `#(?:${Object.keys(TOKEN_FIELD).map((tag) => tag.slice(1)).join('|')})${TOKEN_TAIL}`,
  'giu',
);

/** The tokens a body holds, lower-cased, in the order it holds them. */
export function bodyTokens(body) {
  return (String(body ?? '').match(TOKEN_RE) ?? []).map((token) => token.toLowerCase());
}

/**
 * `#words` in a body that look like a token and are not one: `#coordinate`,
 * `#Sources`, `#plus_code`. Each would go out as text, silently, so the editor
 * names them. Close means one edit away, or the same letters with the
 * separators and a final `s` taken out; a real hashtag is left alone.
 */
export function misspelledTokens(body) {
  const tags = Object.keys(TOKEN_FIELD).map((tag) => tag.slice(1));
  const found = [];
  for (const [whole, word] of String(body ?? '').matchAll(/#([\p{L}\p{N}_-]+)/gu)) {
    const lower = word.toLowerCase();
    if (tags.includes(lower)) continue;
    const bare = lower.replace(/[-_]/g, '').replace(/s$/, '');
    const near = tags.find((tag) => bare === tag || editDistance(lower, tag) === 1);
    if (near && !found.some((f) => f.written === whole)) found.push({ written: whole, meant: `#${near}` });
  }
  return found;
}

/** A body with every whole-word `written` swapped for `meant`, and nothing else touched. */
export function fixMisspelledToken(body, written, meant) {
  const escaped = String(written).replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
  return String(body ?? '').replace(
    new RegExp(`${escaped}(?![\\p{L}\\p{N}_-])`, 'gu'),
    () => meant,
  );
}

function editDistance(a, b) {
  if (Math.abs(a.length - b.length) > 1) return 2;
  let row = Array.from({ length: b.length + 1 }, (_, i) => i);
  for (let i = 1; i <= a.length; i++) {
    const next = [i];
    for (let j = 1; j <= b.length; j++) {
      next[j] = Math.min(row[j] + 1, next[j - 1] + 1, row[j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1));
    }
    row = next;
  }
  return row[b.length];
}

// The classic GeoConfirmed thread, as a token body. Place + plus code on the
// first line, then description, decimal coordinates, mention, and a labelled
// source — each separated by a blank line.
export const DEFAULT_TWEET_BODY = [
  '#place - #pluscode',
  '',
  '#description',
  '',
  '#coordinates',
  '',
  '#mention',
  '',
  'Source: #source',
].join('\n');

/**
 * Layouts a new post template can start from, so a blank body is never the first
 * thing an analyst faces. Each one only replaces the body: the mention, the media
 * post and the boilerplate stay as they were.
 */
export const POST_TEMPLATE_STARTERS = Object.freeze([
  { id: 'classic', label: 'Classic', body: DEFAULT_TWEET_BODY },
  { id: 'coordinates', label: 'Coordinates only', body: '#coordinates\n\n#mention' },
  { id: 'short', label: 'Short', body: '#place\n#coordinates\n\nSource: #source' },
  {
    id: 'dated',
    label: 'With date',
    body: [
      '#place - #pluscode',
      '',
      '#description',
      '',
      'Filmed: #date',
      '',
      '#coordinates',
      '',
      '#mention',
      '',
      'Source: #source',
    ].join('\n'),
  },
].map((starter) => Object.freeze(starter)));

/** What separates a point's name from its coordinates, on the one line it gets. */
export const POINT_SEPARATOR = ' — ';

/** One coordinate line split into its optional name and its coordinates. */
export function splitPointLine(line) {
  const text = String(line ?? '').trim();
  const at = text.indexOf(POINT_SEPARATOR);
  if (at < 0) return { label: '', coords: text };
  return {
    label: text.slice(0, at).trim(),
    coords: text.slice(at + POINT_SEPARATOR.length).trim(),
  };
}

/** The lines a coordinate block holds, dropping the empty ones. */
export function pointLines(text) {
  return String(text ?? '')
    .split('\n')
    .map(splitPointLine)
    .filter((one) => one.coords);
}

/**
 * The coordinates a post carries, one point per line.
 *
 * A proof shows more than one place often enough — three impacts, a building,
 * the camera — and the analyst reads the numbers, so the tweet carries all of
 * them rather than the conclusion alone. The first line is the one the post
 * resolved, so it reads in decimal as it always has; the rest read as they were
 * written, and a name keeps its line either way.
 */
export function postCoordinates(text, { lat, lon } = {}) {
  const lines = pointLines(text);
  if (lat != null && lon != null) {
    const decimal = `${Number(lat).toFixed(6)}, ${Number(lon).toFixed(6)}`;
    if (lines.length) lines[0] = { ...lines[0], coords: decimal };
    else lines.push({ label: '', coords: decimal });
  }
  return lines
    .map((one) => (one.label ? `${one.label}${POINT_SEPARATOR}${one.coords}` : one.coords))
    .join('\n');
}

/** Resolve the live draft into the string each token stands for ('' when absent). */
export function tweetFields({
  place, plusCode, description, lat, lon, coordsText, mention, source, date,
} = {}) {
  return {
    place: place?.trim() ?? '',
    // Place and plus code are one point's address, and that point is the
    // conclusion: the header of a tweet is about the proof, not about impact 2.
    plusCode: plusCode ?? '',
    coordinates: postCoordinates(coordsText, { lat, lon }),
    description: description?.trim() ?? '',
    mention: mention?.trim() ?? '',
    source: source?.trim() ?? '',
    date: date?.trim() ?? '',
  };
}

const STAMP = /^(\d{4}-\d{2}-\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.\d+)?(Z|[+-]\d{2}:\d{2})?$/;

/** An offset as a reader writes it: `UTC+2`, `UTC-3:30`, plain `UTC` at zero. */
function offsetLabel(minutes) {
  if (!minutes) return 'UTC';
  const sign = minutes < 0 ? '-' : '+';
  const whole = Math.abs(minutes);
  const tail = whole % 60 ? `:${String(whole % 60).padStart(2, '0')}` : '';
  return `UTC${sign}${Math.floor(whole / 60)}${tail}`;
}

function offsetAtInstant(zone, millis) {
  const part = new Intl.DateTimeFormat('en-US', { timeZone: zone, timeZoneName: 'longOffset' })
    .formatToParts(new Date(millis))
    .find((one) => one.type === 'timeZoneName')?.value ?? '';
  const match = /GMT(?:([+-])(\d{1,2})(?::(\d{2}))?)?/.exec(part);
  if (!match) return null;
  if (!match[1]) return 0;
  const value = Number(match[2]) * 60 + Number(match[3] ?? 0);
  return match[1] === '-' ? -value : value;
}

/**
 * Minutes a named zone stands from UTC at a local wall-clock reading. Read twice:
 * the first guess takes the clock as UTC, which lands up to a day off and can sit
 * across a clock change from the moment meant, so the offset is read again at the
 * instant that guess points to.
 */
function zoneOffsetAt(zone, day, hour, minute) {
  try {
    const wall = Date.parse(`${day}T${hour}:${minute}:00Z`);
    const first = offsetAtInstant(zone, wall);
    if (first == null) return null;
    return offsetAtInstant(zone, wall - first * 60_000);
  } catch {
    return null;
  }
}

function stampParts(raw) {
  const match = STAMP.exec(raw);
  if (!match) return null;
  const [, day, hour, minute, second, offset] = match;
  const clock = second === '00' ? `${hour}:${minute}` : `${hour}:${minute}:${second}`;
  return { day, hour, minute, clock, offset };
}

function stampZone(parts, zone) {
  if (parts.offset === 'Z') return 'UTC';
  if (parts.offset) {
    const [h, m] = parts.offset.slice(1).split(':').map(Number);
    return offsetLabel((parts.offset[0] === '-' ? -1 : 1) * (h * 60 + m));
  }
  if (!zone) return '';
  const minutes = zoneOffsetAt(zone, parts.day, parts.hour, parts.minute);
  return minutes == null ? '' : offsetLabel(minutes);
}

/**
 * A stated date as a post carries it: the case's own value, written the same way
 * in every language. A date or a range of dates goes out exactly as stored,
 * qualifiers included. A time drops the `T` nobody reads and says which clock it
 * is on, as an offset from UTC: the zone it was stated in, read on that day, so a
 * summer reading in Kyiv says `UTC+3` and a winter one `UTC+2`. A time with no zone
 * goes out bare, since naming one would be inventing it.
 */
export function postDate(when, zone = null) {
  const raw = String(when ?? '').trim();
  if (!raw) return '';
  const [from, to] = raw.split('/');
  const start = stampParts(from);
  if (!start) return raw;
  if (to === undefined) {
    const label = stampZone(start, zone);
    return `${start.day} ${start.clock}${label ? ` ${label}` : ''}`;
  }
  const end = stampParts(to);
  if (!end) return raw;
  const startLabel = stampZone(start, zone);
  const endLabel = stampZone(end, zone);
  const tail = end.day === start.day ? end.clock : `${end.day} ${end.clock}`;
  if (startLabel === endLabel) {
    return `${start.day} ${start.clock}/${tail}${endLabel ? ` ${endLabel}` : ''}`;
  }
  return `${start.day} ${start.clock}${startLabel ? ` ${startLabel}` : ''}/${tail}${endLabel ? ` ${endLabel}` : ''}`;
}

/**
 * Render a thread body into the first tweet, substituting tokens with the
 * draft's values. Layout rules, so a partly-filled draft still reads clean:
 *  - a line whose tokens are *all* empty is dropped whole (label and all) —
 *    "Source: #source" vanishes when there's no source, "#place - #pluscode"
 *    when neither is known;
 *  - a dangling separator left by one empty side of a combined line is trimmed
 *    ("Bakhmut - " → "Bakhmut");
 *  - runs of blank lines collapse to one, and the ends are trimmed.
 */
export function buildTweet1(body, rawFields = {}) {
  const fields = tweetFields(rawFields);
  const out = [];
  for (const line of String(body ?? '').split('\n')) {
    const tokens = line.match(TOKEN_RE);
    if (tokens && tokens.every((t) => !fields[TOKEN_FIELD[t.toLowerCase()]])) {
      continue; // token-driven line with nothing to show
    }
    let text = line.replace(TOKEN_RE, (m) => fields[TOKEN_FIELD[m.toLowerCase()]] ?? '');
    text = text
      .replace(/^\s*[-·,]\s*/, '') // leading separator from an empty first token
      .replace(/\s*[-·,]\s*$/, '') // trailing separator from an empty last token
      .replace(/[ \t]+$/, '');
    out.push(text);
  }
  const collapsed = [];
  for (const l of out) {
    if (l.trim() === '' && collapsed[collapsed.length - 1]?.trim() === '') continue;
    collapsed.push(l);
  }
  while (collapsed.length && collapsed[0].trim() === '') collapsed.shift();
  while (collapsed.length && collapsed[collapsed.length - 1].trim() === '') collapsed.pop();
  return collapsed.join('\n');
}

/** Whether a template body uses a draft field in its first tweet. */
export function templateUsesPostField(body, field) {
  const tokens = FIELD_TOKENS[field];
  if (!tokens) return true;
  const held = bodyTokens(body);
  return tokens.some((token) => held.includes(token));
}

//: What an address looks like inside a source line. The composer joins several with
//: its own separator, so the middle dot ends one as surely as a space does.
const SOURCE_URL = /https?:\/\/[^\s·]+/gi;

/** Every address a proof's source line holds, in the order it holds them. */
export function sourceUrls(sourceLine) {
  return String(sourceLine ?? '').match(SOURCE_URL) ?? [];
}

/**
 * The Source section's body: a link per address, or the line as it stands.
 *
 * A line that is nothing but addresses becomes one link each, because wrapping the
 * whole line in a single href made a dead link the moment a proof rested on two
 * posts. A line carrying anything else is prose the analyst typed, and prose is
 * printed rather than linked.
 */
export function sourceSection(sourceLine) {
  const text = String(sourceLine ?? '').trim();
  const urls = sourceUrls(text);
  if (!urls.length) return text;
  if (text.replace(SOURCE_URL, ' ').replace(/[\s·,;|]+/g, '')) return text;
  if (urls.length === 1) return `[Open original source](${urls[0]})`;
  // A list, because Markdown needs one to break a line at all and a reader needs one to
  // tell four sources apart.
  return urls.map((url) => `- [${url}](${url})`).join('\n');
}

/**
 * Find original downloaded media behind a proof whose source matches the proof
 * source line. Derived PNGs, collages and satellite captures are excluded.
 */
export function proofSourceMediaPaths(caseData, proofPng, proofSource, mediaItems = []) {
  const wantedUrls = new Set(sourceUrls(proofSource));
  if (!wantedUrls.size) return [];
  const entities = Array.isArray(caseData?.entities) ? caseData.entities : [];
  const links = Array.isArray(caseData?.links) ? caseData.links : [];
  const proof = entities.find((e) => e.type === 'proof' && e.attrs?.path === proofPng);
  if (!proof) return [];

  const byId = new Map(entities.map((entity) => [entity.id, entity]));
  const sources = new Map();
  for (const link of links) {
    if (link.type !== 'derived-from') continue;
    const list = sources.get(link.from) ?? [];
    list.push(link.to);
    sources.set(link.from, list);
  }

  function collect(id, seen = new Set()) {
    if (seen.has(id)) return [];
    const nextSeen = new Set(seen);
    nextSeen.add(id);
    const entity = byId.get(id);
    if (!entity) return [];
    const children = (sources.get(id) ?? []).map((child) => byId.get(child)).filter(Boolean);
    const mediaChildren = children.filter((child) => child.type === 'media');
    if (entity.type === 'media') {
      if (mediaChildren.length) return mediaChildren.flatMap((child) => collect(child.id, nextSeen));
      return entity.attrs?.path ? [entity.attrs.path] : [];
    }
    return children.flatMap((child) => collect(child.id, nextSeen));
  }

  const itemByPath = new Map(mediaItems.map((item) => [item.path, item]));
  return [...new Set(collect(proof.id))].filter((path) => {
    const source = itemByPath.get(path)?.source;
    if (source?.type !== 'download') return false;
    return [source.url, source.webpage_url].some((url) => wantedUrls.has(url));
  });
}

/** A legacy toggle template (pre-token) rebuilt as a body, for one-time migration. */
function bodyFromLegacy(data) {
  const t = data?.toggles;
  if (!t) return DEFAULT_TWEET_BODY;
  const parts = [];
  if (t.showHeader) parts.push('#place - #pluscode');
  if (t.showDescription) parts.push('#description');
  if (t.showCoords) parts.push('#coordinates');
  if (t.showMention) parts.push('#mention');
  if (t.showSource) parts.push('Source: #source');
  return parts.join('\n\n') || DEFAULT_TWEET_BODY;
}

/**
 * A thread template's data blob from the composer's live settings. Content-free:
 * the mention, the token body, whether a media tweet is scaffolded, and any
 * boilerplate extra tweets (their text, no ids).
 */
export function templateFromPost({ mention = '@GeoConfirmed', body = DEFAULT_TWEET_BODY, mediaEnabled = true, extraTweets = [] } = {}) {
  return {
    mention: mention ?? '',
    body: typeof body === 'string' && body.trim() ? body : DEFAULT_TWEET_BODY,
    mediaEnabled: mediaEnabled !== false,
    extraTweets: (extraTweets ?? []).map((e) => ({ text: e.text ?? '' })),
  };
}

/**
 * Normalise a stored template blob back into the composer's settings, filling
 * shipped defaults (and migrating a legacy toggle template to a body).
 */
export function normPostTemplate(data = {}) {
  const source = data && typeof data === 'object' && !Array.isArray(data) ? data : {};
  return {
    mention: typeof source.mention === 'string' ? source.mention.slice(0, 280) : '@GeoConfirmed',
    body: typeof source.body === 'string' && source.body.trim()
      ? source.body.slice(0, 16_000)
      : bodyFromLegacy(source),
    mediaEnabled: typeof source.mediaEnabled === 'boolean' ? source.mediaEnabled : true,
    extraTweets: Array.isArray(source.extraTweets)
      ? source.extraTweets.slice(0, 20).map((e) => ({
        text: typeof e?.text === 'string' ? e.text.slice(0, 4_000) : '',
      }))
      : [],
  };
}

/** Apply a post-template skeleton while dropping all current attachments. */
export function applyPostTemplateStructure(current = {}, data = {}) {
  const normalized = normPostTemplate(data);
  return {
    ...current,
    ...normalized,
    extraTweets: normalized.extraTweets.map((tweet) => ({
      text: tweet.text,
      mediaPaths: [],
      mediaType: 'none',
      isMediaTweet: false,
      mediaTextIncludesPrefix: false,
    })),
  };
}