Something went wrong. Try again.
A monorepo for writing specs and other serious text on the AT Protocol.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114// Words and characters in the text: what a reader would read. Section// titles (they name the text, they are not it), outline notes (sketches, not// prose yet) and suggested deletions stay out, as does// anything that is not text (math, images); comments never come in, since// they live beside the text, not in it. A host's metadata (title, authors,// abstract) sits outside the editor and so is not counted either.import type { Node as PMNode } from '@tiptap/pm/model';import type { PMJson } from './types.ts';
export type Counts = { words: number; chars: number };/** One line of the count note: the counts, what they are of when there are several lines, and the words aimed for when a target was set. */export type CountLine = Counts & { label?: string; target?: number };
export const NO_COUNTS: Counts = { words: 0, chars: 0 };
/** A word holds a letter or a digit: a lone dash or bullet is not one. */const WORD = /[\p{L}\p{N}]/u;
/** Words (whitespace-separated runs holding a letter or a digit) and characters (all of them, spaces included) of a run of text. */export function countText(text: string): Counts { let words = 0; for (const w of text.split(/\s+/)) if (WORD.test(w)) words++; return { words, chars: text.length };}
export function addCounts(a: Counts, b: Counts): Counts { return { words: a.words + b.words, chars: a.chars + b.chars };}
/** "1,234 words · 7,890 characters" */export function formatCounts(c: Counts): string { const n = (v: number) => v.toLocaleString(); return `${n(c.words)} ${c.words === 1 ? 'word' : 'words'} · ${n(c.chars)} ${c.chars === 1 ? 'character' : 'characters'}`;}
/** A line of the note: "412 of 1,200 words · 2,600 characters" against a target, the plain counts without one. */export function formatLine(l: CountLine): string { if (!l.target) return formatCounts(l); const n = (v: number) => v.toLocaleString(); return `${n(l.words)} of ${n(l.target)} words · ${n(l.chars)} ${l.chars === 1 ? 'character' : 'characters'}`;}
/** How much of a target is written, in percent (past 100 when over it). */export function percentOf(words: number, target: number): number { return target > 0 ? Math.round((words / target) * 100) : 0;}
// The text of one block (a paragraph, a title, a list item's line) is counted// at once, so a word never straddles two blocks; an inline atom (math) also// closes the run it sits in.type Acc = { words: number; chars: number; buf: string };
function flush(acc: Acc): void { if (!acc.buf) return; const c = countText(acc.buf); acc.words += c.words; acc.chars += c.chars; acc.buf = '';}
/** An outline note (a paragraph, or a list of points): a plan, not text for the reader. */const isOutline = (name: string, attrs: Record<string, unknown> | undefined): boolean => (name === 'paragraph' || name === 'bulletList' || name === 'orderedList') && !!attrs?.outline;
function walkNode(n: PMNode, acc: Acc): void { if (n.type.name === 'sectionTitle') return; // a heading names the section; it is not its prose if (n.isText) { if (!n.marks.some((m) => m.type.name === 'deletion')) acc.buf += n.text ?? ''; return; } if (isOutline(n.type.name, n.attrs)) return; n.forEach((c) => walkNode(c, acc)); flush(acc);}
function walkJson(n: PMJson, acc: Acc): void { if (n.type === 'sectionTitle') return; if (n.text !== undefined) { if (!n.marks?.some((m) => m.type === 'deletion')) acc.buf += n.text; return; } if (isOutline(n.type, n.attrs)) return; for (const c of n.content ?? []) walkJson(c, acc); flush(acc);}
// Documents are immutable values on both sides (a transaction makes a new// node, a host replaces a chapter's JSON), so a count is kept per document.const nodeCounts = new WeakMap<PMNode, Counts>();const jsonCounts = new WeakMap<PMJson, Counts>();
/** The counts of a live document. */export function countNode(doc: PMNode): Counts { let c = nodeCounts.get(doc); if (!c) { const acc: Acc = { words: 0, chars: 0, buf: '' }; walkNode(doc, acc); c = { words: acc.words, chars: acc.chars }; nodeCounts.set(doc, c); } return c;}
/** The counts of a document as JSON (another chapter, say). */export function countJson(doc: PMJson): Counts { let c = jsonCounts.get(doc); if (!c) { const acc: Acc = { words: 0, chars: 0, buf: '' }; walkJson(doc, acc); c = { words: acc.words, chars: acc.chars }; jsonCounts.set(doc, c); } return c;}