Commit 9e6f19fe by PLN (Algolia)

feat: les hits portent leur span — index/length dans le texte passé à lint()

Chaque hit du catalogue, de budget et mur de texte gagne `index`/`length` :
l'offset exact du match dans la CHAÎNE passée à lint() (le frontmatter
reste l'affaire de l'appelant via bodyOf — aucun re-calcul côté appelant,
le desk de www mappe donc 1:1 sur le document de l'éditeur). Champs
additifs, l'API existante ne bouge pas ; heavyParagraphs retourne en plus
le span du bloc trimmé.
Co-Authored-By: 's avatarClaude Code <noreply@anthropic.com>
parent 5c76bcef
...@@ -5,6 +5,10 @@ ...@@ -5,6 +5,10 @@
* servile 10); punctuation charges per occurrence past the budget: one tiret * servile 10); punctuation charges per occurrence past the budget: one tiret
* cadratin per 500 words, one exclamation per 1000. Every hit is a review * cadratin per 500 words, one exclamation per 1000. Every hit is a review
* candidate, not a verdict — the author's voice always wins. * candidate, not a verdict — the author's voice always wins.
*
* Every hit also carries `index`/`length`: the span of the match in the
* EXACT string passed to lint() — frontmatter stripping is the caller's
* job (bodyOf), so offsets never need re-mapping on the caller's side.
*/ */
import { CATALOG, BUDGETS } from './catalog.mjs'; import { CATALOG, BUDGETS } from './catalog.mjs';
...@@ -67,13 +71,28 @@ function excerpt(text, index) { ...@@ -67,13 +71,28 @@ function excerpt(text, index) {
*/ */
const NON_PROSE_LINE = /^\s*(?:[|>#*]|::|!\[|[-+] |\d+\.\s|$)/; const NON_PROSE_LINE = /^\s*(?:[|>#*]|::|!\[|[-+] |\d+\.\s|$)/;
/**
* Same result as before, plus the span of each heavy paragraph in the text
* passed in: `[words, block, index, length]` — index/length delimit the
* trimmed block, so a caller can map them straight onto the original string.
*/
export function heavyParagraphs(text) { export function heavyParagraphs(text) {
const out = []; const out = [];
for (const block of text.split(/\n[ \t]*\n+/)) { const re = /\n[ \t]*\n+/g;
const b = block.trim(); let start = 0;
const spans = [];
for (const m of text.matchAll(re)) {
spans.push([start, m.index]);
start = m.index + m[0].length;
}
spans.push([start, text.length]);
for (const [from, to] of spans) {
const lead = text.slice(from, to);
const padLeft = lead.length - lead.trimStart().length;
const b = lead.trim();
if (!b || !b.split('\n').every((l) => !NON_PROSE_LINE.test(l))) continue; if (!b || !b.split('\n').every((l) => !NON_PROSE_LINE.test(l))) continue;
const n = b.split(/\s+/).filter(Boolean).length; const n = b.split(/\s+/).filter(Boolean).length;
if (n > 80) out.push([n, b]); if (n > 80) out.push([n, b, from + padLeft, b.length]);
} }
return out.sort((a, b) => b[0] - a[0]); return out.sort((a, b) => b[0] - a[0]);
} }
...@@ -87,7 +106,7 @@ export function lint(text) { ...@@ -87,7 +106,7 @@ export function lint(text) {
for (const [id, w, , , re] of CATALOG) { for (const [id, w, , , re] of CATALOG) {
for (const m of text.matchAll(re)) { for (const m of text.matchAll(re)) {
if (!outside(m.index)) { masked++; continue; } if (!outside(m.index)) { masked++; continue; }
hits.push({ id, w, at: excerpt(text, m.index) }); hits.push({ id, w, at: excerpt(text, m.index), index: m.index, length: m[0].length });
score -= w; score -= w;
} }
} }
...@@ -97,15 +116,15 @@ export function lint(text) { ...@@ -97,15 +116,15 @@ export function lint(text) {
for (const m of text.matchAll(re)) { for (const m of text.matchAll(re)) {
if (!outside(m.index)) { masked++; continue; } if (!outside(m.index)) { masked++; continue; }
if (excess >= budget) { if (excess >= budget) {
hits.push({ id: `budget ${id} (>${budget}/${words} mots)`, w: weight, at: `occurrence ${excess + 1}` }); hits.push({ id: `budget ${id} (>${budget}/${words} mots)`, w: weight, at: `occurrence ${excess + 1}`, index: m.index, length: m[0].length });
score -= weight; score -= weight;
} }
excess++; excess++;
} }
} }
for (const [n, b] of heavyParagraphs(text)) { for (const [n, b, index, length] of heavyParagraphs(text)) {
const w = n > 140 ? 5 : 3; const w = n > 140 ? 5 : 3;
hits.push({ id: `mur de texte (${n} mots)`, w, at: '…' + b.slice(0, 64).replace(/\s+/g, ' ') + '…' }); hits.push({ id: `mur de texte (${n} mots)`, w, at: '…' + b.slice(0, 64).replace(/\s+/g, ' ') + '…', index, length });
score -= w; score -= w;
} }
return { words, hits, masked, score: Math.max(0, score) }; return { words, hits, masked, score: Math.max(0, score) };
......
...@@ -80,3 +80,69 @@ test('dashes inside quotes stay off the budget', () => { ...@@ -80,3 +80,69 @@ test('dashes inside quotes stay off the budget', () => {
assert.equal(r.hits.filter((h) => h.id.startsWith('budget tiret')).length, 0); assert.equal(r.hits.filter((h) => h.id.startsWith('budget tiret')).length, 0);
assert.equal(r.masked, 2); assert.equal(r.masked, 2);
}); });
// --- offsets (index/length) — pour le desk : chaque hit se mappe 1:1 sur le
// texte passé à lint(), sans re-calcul côté appelant -------------------------
test('catalog hits carry index/length slicing back to the exact match', () => {
const t = "Préambule quelconque. Soyons honnêtes : le montage cassait. Fin.";
const r = lint(t);
const h = r.hits.find((x) => x.id === 'ouverture-vide');
assert.ok(h, 'hit attendu');
assert.equal(typeof h.index, 'number');
assert.equal(typeof h.length, 'number');
assert.equal(t.slice(h.index, h.index + h.length), 'Soyons honnêtes');
});
test('offsets are into the string the caller passed (frontmatter included)', () => {
const fm = "---\ntitle: 'Un titre'\ndate: '2026-11-15'\n---\n\n";
const body = "Voici le corps. Ce n'est pas un outil, c'est un compagnon de voyage.";
const doc = fm + body;
const r = lint(doc); // lint() ne strips PAS le frontmatter : offsets = document entier
const h = r.hits.find((x) => x.id === 'contraste binaire');
assert.ok(h, 'hit attendu dans le corps');
assert.ok(h.index >= fm.length, 'le hit vit après le frontmatter');
assert.equal(doc.slice(h.index, h.index + h.length).toLowerCase(), "ce n'est pas un outil, c'est");
});
test('budget hits carry the offset of the exact excess occurrence', () => {
const words = Array(600).fill('mot').join(' ');
const t = words + ' — fin — vraiment — trois.';
const r = lint(t);
const budgetHits = r.hits.filter((h) => h.id.startsWith('budget tiret'));
assert.equal(budgetHits.length, 2);
for (const h of budgetHits) assert.equal(t.slice(h.index, h.index + h.length), '—');
// les dépassements tombent sur les 2e et 3e tirets du texte
const dashPositions = [...t.matchAll(/—/g)].map((m) => m.index);
assert.deepEqual(budgetHits.map((h) => h.index), [dashPositions[1], dashPositions[2]]);
});
test('heavy paragraphs carry the span of the trimmed block', () => {
const wall = Array(90).fill('mot').join(' ');
const t = 'Intro.\n\n ' + wall + ' \n\nFin.';
const r = lint(t);
const h = r.hits.find((x) => x.id.startsWith('mur de texte'));
assert.ok(h, 'mur attendu');
assert.equal(t.slice(h.index, h.index + h.length), wall);
});
test('every hit of a mixed document slices to non-empty text', () => {
const t = [
'---',
"title: 'Soyons honnêtes : le test'", // frontmatter, non exempté (data, pas prose)
'---',
'',
'Soyons honnêtes : le montage cassait.',
'',
Array(85).fill('mot').join(' ') + ' — vraiment.',
].join('\n');
const r = lint(t);
assert.ok(r.hits.length >= 3);
for (const h of r.hits) {
assert.equal(typeof h.index, 'number', h.id);
assert.equal(typeof h.length, 'number', h.id);
assert.ok(h.length > 0, h.id);
const slice = t.slice(h.index, h.index + h.length);
assert.ok(slice.length > 0, h.id);
}
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment