textoneitor 0.0.0-stage → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/LICENSE +101 -0
  2. package/LICENSE-AGPL-3.0 +661 -0
  3. package/README.md +69 -2
  4. package/dist/api/index.d.ts +34 -0
  5. package/dist/api/index.js +98 -0
  6. package/dist/baseline/index.d.ts +20 -0
  7. package/dist/baseline/index.js +81 -0
  8. package/dist/bundle/dist/cli.mjs +29861 -0
  9. package/dist/bundle/package.json +6 -0
  10. package/dist/bundle/rulepack/rulepack.json +1496 -0
  11. package/dist/bundle/rules/situaciones.yml +27 -0
  12. package/dist/cli/benchmark.d.ts +89 -0
  13. package/dist/cli/benchmark.js +185 -0
  14. package/dist/cli/main.d.ts +2 -0
  15. package/dist/cli/main.js +357 -0
  16. package/dist/config/core.d.ts +55 -0
  17. package/dist/config/core.js +244 -0
  18. package/dist/config/glob.d.ts +2 -0
  19. package/dist/config/glob.js +72 -0
  20. package/dist/config/index.d.ts +8 -0
  21. package/dist/config/index.js +38 -0
  22. package/dist/config/situaciones.d.ts +17 -0
  23. package/dist/config/situaciones.js +43 -0
  24. package/dist/contracts/index.d.ts +238 -0
  25. package/dist/contracts/index.js +4 -0
  26. package/dist/document/index.d.ts +10 -0
  27. package/dist/document/index.js +68 -0
  28. package/dist/document/text.d.ts +22 -0
  29. package/dist/document/text.js +91 -0
  30. package/dist/document/tokenize.d.ts +4 -0
  31. package/dist/document/tokenize.js +59 -0
  32. package/dist/markdown/index.d.ts +7 -0
  33. package/dist/markdown/index.js +178 -0
  34. package/dist/paths.d.ts +4 -0
  35. package/dist/paths.js +14 -0
  36. package/dist/reporters/index.d.ts +29 -0
  37. package/dist/reporters/index.js +182 -0
  38. package/dist/rules/compiler/build.d.ts +1 -0
  39. package/dist/rules/compiler/build.js +46 -0
  40. package/dist/rules/compiler/index.d.ts +40 -0
  41. package/dist/rules/compiler/index.js +343 -0
  42. package/dist/rules/compiler/safety.d.ts +16 -0
  43. package/dist/rules/compiler/safety.js +124 -0
  44. package/dist/rules/detectors/index.d.ts +4 -0
  45. package/dist/rules/detectors/index.js +636 -0
  46. package/dist/rules/detectors/shared.d.ts +30 -0
  47. package/dist/rules/detectors/shared.js +87 -0
  48. package/dist/rules/runtime/index.d.ts +17 -0
  49. package/dist/rules/runtime/index.js +74 -0
  50. package/dist/runner/index.d.ts +16 -0
  51. package/dist/runner/index.js +171 -0
  52. package/dist/runner/lint-text.d.ts +19 -0
  53. package/dist/runner/lint-text.js +20 -0
  54. package/dist/scoring/index.d.ts +16 -0
  55. package/dist/scoring/index.js +41 -0
  56. package/dist/suppressions/index.d.ts +23 -0
  57. package/dist/suppressions/index.js +86 -0
  58. package/dist/web/engine.meta.json +1 -0
  59. package/dist/web/engine.mjs +11018 -0
  60. package/dist/web/index.d.ts +39 -0
  61. package/dist/web/index.js +37 -0
  62. package/dist/web/node-crypto-shim.d.ts +9 -0
  63. package/dist/web/node-crypto-shim.js +24 -0
  64. package/dist/web/sha256.d.ts +11 -0
  65. package/dist/web/sha256.js +89 -0
  66. package/package.json +88 -4
  67. package/rulepack/rulepack.json +1496 -0
  68. package/rules/profiles/academico.yml +4 -0
  69. package/rules/profiles/chat.yml +4 -0
  70. package/rules/profiles/correo.yml +3 -0
  71. package/rules/profiles/general.yml +2 -0
  72. package/rules/profiles/marketing.yml +4 -0
  73. package/rules/profiles/readme.yml +5 -0
  74. package/rules/profiles/redes.yml +6 -0
  75. package/rules/profiles/tecnico.yml +4 -0
  76. package/rules/situaciones.yml +27 -0
  77. package/schemas/rule.schema.json +90 -0
@@ -0,0 +1,238 @@
1
+ /**
2
+ * Contratos públicos del linter. Todo formato JSON externo lleva `schema_version`.
3
+ */
4
+ export declare const SCHEMA_VERSION: 1;
5
+ export type Level = "off" | "info" | "warning" | "error";
6
+ export type RuleStatus = "draft" | "candidate" | "stable" | "deprecated";
7
+ export type RuleCategory = "lexico" | "estructura" | "repeticion" | "densidad" | "retorica" | "formato";
8
+ export type DetectorKind = "regex" | "lexicon" | "sequence" | "density" | "repetition" | "structure" | "cooccurrence";
9
+ export type RuleScope = "sentence" | "paragraph" | "document" | "block";
10
+ export type ProfileName = "general" | "tecnico" | "academico" | "marketing" | "chat" | "correo" | "readme" | "redes";
11
+ export type Register = "general" | "tecnico" | "academico" | "marketing" | "literario" | "periodistico" | "institucional";
12
+ /** Posición: offset 0-based en el texto original (unidades UTF-16) y línea/columna 1-based (columna en code points). */
13
+ export interface Position {
14
+ offset: number;
15
+ line: number;
16
+ column: number;
17
+ }
18
+ export interface SourceRange {
19
+ start: Position;
20
+ end: Position;
21
+ }
22
+ export interface RuleExample {
23
+ text: string;
24
+ /** Número de hallazgos esperados. */
25
+ expect: number;
26
+ note?: string;
27
+ }
28
+ /** Definición de regla tal y como se escribe en YAML (autoría). */
29
+ export interface RuleDefinition {
30
+ schema_version: number;
31
+ id: string;
32
+ revision: number;
33
+ status: RuleStatus;
34
+ title: string;
35
+ summary: string;
36
+ category: RuleCategory;
37
+ scope: RuleScope;
38
+ detector: DetectorKind;
39
+ conditions: Record<string, unknown>;
40
+ exceptions?: Record<string, unknown>;
41
+ default_level: Level;
42
+ score: {
43
+ weight: number;
44
+ cap: number;
45
+ };
46
+ message: string;
47
+ explanation: string;
48
+ rewrite_guidance: string;
49
+ examples: {
50
+ positive: RuleExample[];
51
+ negative: RuleExample[];
52
+ };
53
+ provenance: {
54
+ evidence: string;
55
+ known_false_positives: string[];
56
+ reviewed_by: string[];
57
+ };
58
+ profiles?: Partial<Record<ProfileName, Level>>;
59
+ }
60
+ /** Regla compilada: lo único que interpreta el runtime. */
61
+ export interface CompiledRule {
62
+ id: string;
63
+ revision: number;
64
+ status: RuleStatus;
65
+ title: string;
66
+ summary: string;
67
+ category: RuleCategory;
68
+ scope: RuleScope;
69
+ detector: DetectorKind;
70
+ /** Parámetros del detector ya resueltos (léxicos expandidos, regex validadas). */
71
+ params: Record<string, unknown>;
72
+ exceptions: {
73
+ patterns?: string[];
74
+ lexicon?: string[];
75
+ };
76
+ default_level: Level;
77
+ score: {
78
+ weight: number;
79
+ cap: number;
80
+ };
81
+ message: string;
82
+ explanation: string;
83
+ rewrite_guidance: string;
84
+ profiles: Partial<Record<ProfileName, Level>>;
85
+ }
86
+ export interface RulePack {
87
+ schema_version: number;
88
+ name: string;
89
+ version: string;
90
+ generated_at: string;
91
+ rules: CompiledRule[];
92
+ }
93
+ export type BlockKind = "paragraph" | "heading" | "list_item" | "blockquote" | "table_cell" | "plain";
94
+ /** Bloque de texto elegible para análisis, con su posición en el original. */
95
+ export interface TextBlock {
96
+ kind: BlockKind;
97
+ /** Texto del bloque sin marcado. */
98
+ text: string;
99
+ /** Mapa: índice en `text` -> offset en el texto original. Longitud = text.length + 1. */
100
+ map: Int32Array;
101
+ depth: number;
102
+ /** El bloque empieza con texto en negrita (patrón «- **Título:** texto»). */
103
+ startsWithStrong?: boolean;
104
+ }
105
+ export interface Sentence {
106
+ blockIndex: number;
107
+ /** Rango dentro de `block.text`. */
108
+ start: number;
109
+ end: number;
110
+ text: string;
111
+ }
112
+ export interface Token {
113
+ blockIndex: number;
114
+ start: number;
115
+ end: number;
116
+ text: string;
117
+ lower: string;
118
+ /** Palabra alfabética (no número ni símbolo). */
119
+ isWord: boolean;
120
+ }
121
+ export interface Document {
122
+ path: string | null;
123
+ format: "text" | "markdown";
124
+ /** Texto original tal y como se leyó (con BOM y CRLF si los había). */
125
+ original: string;
126
+ lineStarts: number[];
127
+ blocks: TextBlock[];
128
+ sentences: Sentence[];
129
+ tokens: Token[];
130
+ /** Palabras elegibles (tokens alfabéticos de bloques analizados). */
131
+ eligibleWords: number;
132
+ /** Rangos [inicio, fin) del original ocupados por código en Markdown (bloques e inline). Las directivas de supresión escritas ahí son ejemplos, no directivas. */
133
+ codeRanges?: [number, number][];
134
+ }
135
+ export interface Finding {
136
+ rule: string;
137
+ revision: number;
138
+ level: Exclude<Level, "off">;
139
+ category: RuleCategory;
140
+ message: string;
141
+ range: SourceRange;
142
+ snippet: string;
143
+ /** Huella estable para baseline y deduplicación. */
144
+ fingerprint: string;
145
+ suppressed?: {
146
+ by: "inline" | "baseline";
147
+ reason?: string;
148
+ };
149
+ }
150
+ export interface ScoreContributor {
151
+ rule: string;
152
+ findings: number;
153
+ contribution: number;
154
+ }
155
+ export interface ScoreResult {
156
+ /** Índice de patrones editoriales 0-100. `null` si el texto no tiene suficientes palabras. */
157
+ index: number | null;
158
+ eligibleWords: number;
159
+ minWords: number;
160
+ contributors: ScoreContributor[];
161
+ }
162
+ export interface FileResult {
163
+ path: string;
164
+ format: "text" | "markdown";
165
+ findings: Finding[];
166
+ score: ScoreResult;
167
+ durationMs: number;
168
+ fromCache?: boolean;
169
+ }
170
+ export interface PolicyResult {
171
+ ok: boolean;
172
+ failOn: Level | "never";
173
+ maxIndex: number | null;
174
+ reasons: string[];
175
+ counts: {
176
+ error: number;
177
+ warning: number;
178
+ info: number;
179
+ suppressed: number;
180
+ };
181
+ }
182
+ export interface ScanResult {
183
+ schema_version: number;
184
+ tool: {
185
+ name: string;
186
+ version: string;
187
+ rulepack: string;
188
+ };
189
+ files: FileResult[];
190
+ policy: PolicyResult;
191
+ baseline?: {
192
+ path: string;
193
+ matched: number;
194
+ stale: number;
195
+ };
196
+ durationMs: number;
197
+ }
198
+ export interface BaselineEntry {
199
+ rule: string;
200
+ path: string;
201
+ fingerprint: string;
202
+ reason?: string;
203
+ }
204
+ export interface BaselineFile {
205
+ schema_version: number;
206
+ created: string;
207
+ entries: BaselineEntry[];
208
+ }
209
+ export interface PathOverride {
210
+ files: string[];
211
+ rules?: Record<string, Level>;
212
+ profile?: ProfileName;
213
+ }
214
+ export interface Config {
215
+ schema_version: number;
216
+ profile: ProfileName;
217
+ register: Register;
218
+ include: string[];
219
+ exclude: string[];
220
+ respect_gitignore: boolean;
221
+ rules: Record<string, Level>;
222
+ overrides: PathOverride[];
223
+ baseline: {
224
+ path: string | null;
225
+ require_reason: boolean;
226
+ };
227
+ fail_on: Level | "never";
228
+ max_index: number | null;
229
+ privacy: {
230
+ snippets: boolean;
231
+ };
232
+ reporter: "terminal" | "json" | "sarif" | "revision";
233
+ cache: {
234
+ enabled: boolean;
235
+ dir: string;
236
+ };
237
+ min_words_for_index: number;
238
+ }
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Contratos públicos del linter. Todo formato JSON externo lleva `schema_version`.
3
+ */
4
+ export const SCHEMA_VERSION = 1;
@@ -0,0 +1,10 @@
1
+ import type { Document, TextBlock } from "../contracts/index.js";
2
+ export { normalizeText, computeLineStarts, offsetToLineColumn, snippetOf } from "./text.js";
3
+ export { splitSentences, tokenizeBlock } from "./tokenize.js";
4
+ /** Bloques de texto plano: párrafos separados por líneas en blanco. */
5
+ export declare function plainTextBlocks(text: string): TextBlock[];
6
+ export declare function detectFormat(path: string | null | undefined): "text" | "markdown";
7
+ export declare function buildDocument(original: string, opts: {
8
+ path?: string | null;
9
+ format?: "text" | "markdown";
10
+ }): Document;
@@ -0,0 +1,68 @@
1
+ import { computeLineStarts, normalizeText } from "./text.js";
2
+ import { splitSentences, tokenizeBlock } from "./tokenize.js";
3
+ import { markdownBlocks } from "../markdown/index.js";
4
+ export { normalizeText, computeLineStarts, offsetToLineColumn, snippetOf } from "./text.js";
5
+ export { splitSentences, tokenizeBlock } from "./tokenize.js";
6
+ /** Bloques de texto plano: párrafos separados por líneas en blanco. */
7
+ export function plainTextBlocks(text) {
8
+ const blocks = [];
9
+ const re = /(?:[^\n]|\n(?![ \t]*\n))+/g;
10
+ let m;
11
+ while ((m = re.exec(text)) !== null) {
12
+ const raw = m[0];
13
+ const lead = raw.length - raw.trimStart().length;
14
+ const body = raw.trim();
15
+ if (!body)
16
+ continue;
17
+ const start = m.index + lead;
18
+ const map = new Int32Array(body.length + 1);
19
+ for (let i = 0; i <= body.length; i++)
20
+ map[i] = start + i;
21
+ blocks.push({ kind: "plain", text: body, map, depth: 0 });
22
+ }
23
+ return blocks;
24
+ }
25
+ export function detectFormat(path) {
26
+ if (!path)
27
+ return "text";
28
+ return /\.(md|markdown|mdown|mkd)$/i.test(path) ? "markdown" : "text";
29
+ }
30
+ export function buildDocument(original, opts) {
31
+ const format = opts.format ?? detectFormat(opts.path);
32
+ const norm = normalizeText(original);
33
+ const codeRanges = [];
34
+ const blocks = format === "markdown" ? markdownBlocks(norm.text, codeRanges) : plainTextBlocks(norm.text);
35
+ if (norm.hadBom || norm.hadCr) {
36
+ for (const b of blocks) {
37
+ for (let i = 0; i < b.map.length; i++)
38
+ b.map[i] = norm.toOriginal(b.map[i]);
39
+ }
40
+ for (const r of codeRanges) {
41
+ r[0] = norm.toOriginal(r[0]);
42
+ r[1] = norm.toOriginal(r[1]);
43
+ }
44
+ }
45
+ const sentences = [];
46
+ const tokens = [];
47
+ let eligibleWords = 0;
48
+ for (let i = 0; i < blocks.length; i++) {
49
+ const b = blocks[i];
50
+ sentences.push(...splitSentences(b, i));
51
+ const t = tokenizeBlock(b, i);
52
+ for (const tk of t)
53
+ if (tk.isWord)
54
+ eligibleWords++;
55
+ tokens.push(...t);
56
+ }
57
+ return {
58
+ path: opts.path ?? null,
59
+ format,
60
+ original,
61
+ lineStarts: computeLineStarts(original),
62
+ blocks,
63
+ sentences,
64
+ tokens,
65
+ eligibleWords,
66
+ ...(codeRanges.length ? { codeRanges } : {}),
67
+ };
68
+ }
@@ -0,0 +1,22 @@
1
+ /** Normalización de texto y utilidades de posiciones. */
2
+ export interface NormalizedText {
3
+ original: string;
4
+ /** Texto sin BOM y con CRLF/CR convertidos a LF. */
5
+ text: string;
6
+ /** Offset en `text` -> offset en `original`. */
7
+ toOriginal(offset: number): number;
8
+ hadBom: boolean;
9
+ hadCr: boolean;
10
+ }
11
+ /**
12
+ * Elimina BOM y convierte CRLF/CR a LF conservando la correspondencia de offsets.
13
+ */
14
+ export declare function normalizeText(original: string): NormalizedText;
15
+ export declare function computeLineStarts(text: string): number[];
16
+ /** Línea y columna 1-based para un offset. La columna se cuenta en code points (no en unidades UTF-16). */
17
+ export declare function offsetToLineColumn(text: string, lineStarts: number[], offset: number): {
18
+ line: number;
19
+ column: number;
20
+ };
21
+ /** Recorta un fragmento para snippets sin partir pares subrogados. */
22
+ export declare function snippetOf(text: string, start: number, end: number, max?: number): string;
@@ -0,0 +1,91 @@
1
+ /** Normalización de texto y utilidades de posiciones. */
2
+ /**
3
+ * Elimina BOM y convierte CRLF/CR a LF conservando la correspondencia de offsets.
4
+ */
5
+ export function normalizeText(original) {
6
+ const hadBom = original.charCodeAt(0) === 0xfeff;
7
+ const src = hadBom ? original.slice(1) : original;
8
+ const bomShift = hadBom ? 1 : 0;
9
+ if (!src.includes("\r")) {
10
+ return { original, text: src, hadBom, hadCr: false, toOriginal: (o) => o + bomShift };
11
+ }
12
+ let out = "";
13
+ let last = 0;
14
+ // Posiciones (en `out`) a partir de las cuales el desplazamiento aumenta en 1.
15
+ const shifts = [];
16
+ for (let i = 0; i < src.length; i++) {
17
+ if (src.charCodeAt(i) === 13) {
18
+ out += src.slice(last, i);
19
+ if (src.charCodeAt(i + 1) === 10) {
20
+ // CRLF -> LF: se elimina el CR; desplazamiento +1 a partir de aquí.
21
+ shifts.push(out.length);
22
+ }
23
+ else {
24
+ // CR solitario -> LF: mismo tamaño.
25
+ out += "\n";
26
+ }
27
+ last = i + 1;
28
+ }
29
+ }
30
+ out += src.slice(last);
31
+ const toOriginal = (o) => {
32
+ let lo = 0;
33
+ let hi = shifts.length;
34
+ while (lo < hi) {
35
+ const mid = (lo + hi) >> 1;
36
+ if (shifts[mid] <= o)
37
+ lo = mid + 1;
38
+ else
39
+ hi = mid;
40
+ }
41
+ return o + lo + bomShift;
42
+ };
43
+ return { original, text: out, hadBom, hadCr: true, toOriginal };
44
+ }
45
+ export function computeLineStarts(text) {
46
+ const starts = [0];
47
+ for (let i = 0; i < text.length; i++) {
48
+ const c = text.charCodeAt(i);
49
+ // LF, y también CR solitario (finales de línea de Mac clásico), igual que hace normalizeText.
50
+ if (c === 10 || (c === 13 && text.charCodeAt(i + 1) !== 10))
51
+ starts.push(i + 1);
52
+ }
53
+ return starts;
54
+ }
55
+ /** Línea y columna 1-based para un offset. La columna se cuenta en code points (no en unidades UTF-16). */
56
+ export function offsetToLineColumn(text, lineStarts, offset) {
57
+ let lo = 0;
58
+ let hi = lineStarts.length - 1;
59
+ while (lo < hi) {
60
+ const mid = (lo + hi + 1) >> 1;
61
+ if (lineStarts[mid] <= offset)
62
+ lo = mid;
63
+ else
64
+ hi = mid - 1;
65
+ }
66
+ const lineStart = lineStarts[lo];
67
+ let column = 1;
68
+ for (let i = lineStart; i < offset; i++) {
69
+ const c = text.charCodeAt(i);
70
+ if (c >= 0xdc00 && c <= 0xdfff)
71
+ continue; // unidad baja de par subrogado
72
+ if (c === 13)
73
+ continue; // CR de un CRLF no cuenta como columna
74
+ if (c === 0xfeff && i === 0)
75
+ continue; // el BOM no es una columna visible
76
+ column++;
77
+ }
78
+ return { line: lo + 1, column };
79
+ }
80
+ /** Recorta un fragmento para snippets sin partir pares subrogados. */
81
+ export function snippetOf(text, start, end, max = 120) {
82
+ let s = text.slice(start, end).replace(/\s+/g, " ").trim();
83
+ if (s.length > max) {
84
+ let cut = max;
85
+ const c = s.charCodeAt(cut - 1);
86
+ if (c >= 0xd800 && c <= 0xdbff)
87
+ cut--;
88
+ s = s.slice(0, cut) + "…";
89
+ }
90
+ return s;
91
+ }
@@ -0,0 +1,4 @@
1
+ import type { Sentence, TextBlock, Token } from "../contracts/index.js";
2
+ export declare function tokenizeBlock(block: TextBlock, blockIndex: number): Token[];
3
+ /** Divide un bloque en frases. Un encabezado o elemento de lista sin puntuación es una única frase. */
4
+ export declare function splitSentences(block: TextBlock, blockIndex: number): Sentence[];
@@ -0,0 +1,59 @@
1
+ const WORD_RE = /[\p{L}\p{M}\p{N}]+(?:['’\-·][\p{L}\p{M}\p{N}]+)*/gu;
2
+ const SENT_END = /[.!?…]+["»”')\]]*|\n/g;
3
+ const ABBREV = new Set([
4
+ "sr", "sra", "srta", "dr", "dra", "etc", "p", "pp", "pág", "págs", "ej", "núm", "ud", "uds", "vs",
5
+ "art", "cap", "fig", "vol", "aprox", "tel", "av", "avda", "sto", "sta", "d", "dña", "prof", "ing", "lic",
6
+ ]);
7
+ export function tokenizeBlock(block, blockIndex) {
8
+ const tokens = [];
9
+ const re = new RegExp(WORD_RE.source, WORD_RE.flags);
10
+ let m;
11
+ while ((m = re.exec(block.text)) !== null) {
12
+ const text = m[0];
13
+ const isWord = /\p{L}/u.test(text);
14
+ tokens.push({ blockIndex, start: m.index, end: m.index + text.length, text, lower: text.toLowerCase(), isWord });
15
+ }
16
+ return tokens;
17
+ }
18
+ /** Divide un bloque en frases. Un encabezado o elemento de lista sin puntuación es una única frase. */
19
+ export function splitSentences(block, blockIndex) {
20
+ const text = block.text;
21
+ const out = [];
22
+ let start = 0;
23
+ const re = new RegExp(SENT_END.source, SENT_END.flags);
24
+ let m;
25
+ while ((m = re.exec(text)) !== null) {
26
+ const end = m.index + m[0].length;
27
+ if (m[0] === "\n") {
28
+ // Un salto de línea dentro de un párrafo envuelto a mano (correo, README a 72-80 columnas) no cierra la frase:
29
+ // si la línea siguiente sigue en minúscula, es la misma frase. Sin esto el ritmo mide líneas y no frases.
30
+ if (/^[ \t]*\p{Ll}/u.test(text.slice(end, end + 8)))
31
+ continue;
32
+ }
33
+ else {
34
+ const before = text.slice(Math.max(start, m.index - 8), m.index);
35
+ const wordBefore = /(\p{L}+)$/u.exec(before)?.[1]?.toLowerCase();
36
+ const next = text.charAt(end);
37
+ if (m[0] === "." && wordBefore && ABBREV.has(wordBefore))
38
+ continue;
39
+ if (m[0] === "." && /\p{N}/u.test(next))
40
+ continue;
41
+ if (m[0] === "." && wordBefore && wordBefore.length === 1 && /\p{Lu}/u.test(before.slice(-1)))
42
+ continue; // iniciales
43
+ if (next !== "" && !/\s/.test(next))
44
+ continue; // "v1.0", "www.x"
45
+ }
46
+ push(start, end);
47
+ start = end;
48
+ }
49
+ push(start, text.length);
50
+ return out;
51
+ function push(s, e) {
52
+ const t = text.slice(s, e);
53
+ const ns = s + (t.length - t.trimStart().length);
54
+ const ne = e - (t.length - t.trimEnd().length);
55
+ if (ne <= ns)
56
+ return;
57
+ out.push({ blockIndex, start: ns, end: ne, text: text.slice(ns, ne) });
58
+ }
59
+ }
@@ -0,0 +1,7 @@
1
+ import type { TextBlock } from "../contracts/index.js";
2
+ /**
3
+ * Extrae bloques elegibles de un Markdown mediante AST.
4
+ * Excluidos: código (bloque e inline), front matter, URLs y destinos de enlaces, comentarios y HTML, definiciones.
5
+ * Analizados: párrafos, encabezados, elementos de lista, citas, celdas de tabla y texto visible de enlaces.
6
+ */
7
+ export declare function markdownBlocks(text: string, codeRanges?: [number, number][]): TextBlock[];
@@ -0,0 +1,178 @@
1
+ import { fromMarkdown } from "mdast-util-from-markdown";
2
+ import { frontmatter } from "micromark-extension-frontmatter";
3
+ import { frontmatterFromMarkdown } from "mdast-util-frontmatter";
4
+ import { gfm } from "micromark-extension-gfm";
5
+ import { gfmFromMarkdown } from "mdast-util-gfm";
6
+ /**
7
+ * Extrae bloques elegibles de un Markdown mediante AST.
8
+ * Excluidos: código (bloque e inline), front matter, URLs y destinos de enlaces, comentarios y HTML, definiciones.
9
+ * Analizados: párrafos, encabezados, elementos de lista, citas, celdas de tabla y texto visible de enlaces.
10
+ */
11
+ export function markdownBlocks(text, codeRanges) {
12
+ const tree = fromMarkdown(text, {
13
+ extensions: [frontmatter(["yaml", "toml"]), gfm()],
14
+ mdastExtensions: [frontmatterFromMarkdown(["yaml", "toml"]), gfmFromMarkdown()],
15
+ });
16
+ const blocks = [];
17
+ walk(tree, 0);
18
+ return blocks;
19
+ function codeRange(node) {
20
+ const s = node.position?.start.offset;
21
+ const e = node.position?.end.offset;
22
+ if (codeRanges && s !== undefined && e !== undefined)
23
+ codeRanges.push([s, e]);
24
+ }
25
+ function walk(node, depth) {
26
+ if (node.type === "code")
27
+ codeRange(node);
28
+ switch (node.type) {
29
+ case "root":
30
+ for (const c of node.children)
31
+ walk(c, depth);
32
+ return;
33
+ case "paragraph":
34
+ emit("paragraph", node, depth);
35
+ return;
36
+ case "heading":
37
+ emit("heading", node, node.depth);
38
+ return;
39
+ case "blockquote":
40
+ for (const c of node.children) {
41
+ if (c.type === "paragraph")
42
+ emit("blockquote", c, depth + 1);
43
+ else
44
+ walk(c, depth + 1);
45
+ }
46
+ return;
47
+ case "list":
48
+ for (const c of node.children)
49
+ walk(c, depth + 1);
50
+ return;
51
+ case "listItem":
52
+ for (const c of node.children) {
53
+ if (c.type === "paragraph")
54
+ emit("list_item", c, depth);
55
+ else
56
+ walk(c, depth);
57
+ }
58
+ return;
59
+ case "table":
60
+ for (const row of node.children)
61
+ for (const cell of row.children)
62
+ emit("table_cell", cell, depth);
63
+ return;
64
+ case "footnoteDefinition":
65
+ for (const c of node.children)
66
+ walk(c, depth);
67
+ return;
68
+ default:
69
+ // code, html, yaml, toml, definition, thematicBreak: excluidos.
70
+ return;
71
+ }
72
+ }
73
+ function emit(kind, node, depth) {
74
+ const parts = [];
75
+ collect(node, parts);
76
+ let out = "";
77
+ const map = [];
78
+ for (const p of parts) {
79
+ out += p.text;
80
+ for (const o of p.offsets)
81
+ map.push(o);
82
+ }
83
+ let s = 0;
84
+ let e = out.length;
85
+ while (s < e && /\s/.test(out[s]))
86
+ s++;
87
+ while (e > s && /\s/.test(out[e - 1]))
88
+ e--;
89
+ if (e <= s)
90
+ return;
91
+ const body = out.slice(s, e);
92
+ const m = new Int32Array(body.length + 1);
93
+ for (let i = 0; i < body.length; i++)
94
+ m[i] = map[s + i];
95
+ m[body.length] = map[e - 1] + 1;
96
+ const first = node.children[0];
97
+ const startsWithStrong = first?.type === "strong";
98
+ blocks.push({ kind, text: body, map: m, depth, startsWithStrong });
99
+ }
100
+ function collect(node, parts) {
101
+ switch (node.type) {
102
+ case "text": {
103
+ const start = node.position?.start.offset ?? 0;
104
+ const end = node.position?.end.offset ?? start;
105
+ const raw = text.slice(start, end);
106
+ if (raw === node.value) {
107
+ parts.push({ text: node.value, offsets: range(start, end) });
108
+ }
109
+ else {
110
+ // Entidades o escapes: alineación aproximada y acotada dentro del nodo.
111
+ parts.push({ text: node.value, offsets: approx(node.value, raw, start) });
112
+ }
113
+ return;
114
+ }
115
+ case "inlineCode":
116
+ codeRange(node);
117
+ parts.push({ text: " ", offsets: [node.position?.start.offset ?? 0] });
118
+ return;
119
+ case "html":
120
+ case "image":
121
+ case "imageReference":
122
+ case "footnoteReference":
123
+ case "break": {
124
+ const o = node.position?.start.offset ?? 0;
125
+ parts.push({ text: " ", offsets: [o] });
126
+ return;
127
+ }
128
+ case "link":
129
+ case "linkReference":
130
+ case "emphasis":
131
+ case "strong":
132
+ case "delete":
133
+ for (const c of node.children)
134
+ collect(c, parts);
135
+ return;
136
+ default:
137
+ if ("children" in node)
138
+ for (const c of node.children)
139
+ collect(c, parts);
140
+ return;
141
+ }
142
+ }
143
+ }
144
+ function range(a, b) {
145
+ const r = new Array(b - a);
146
+ for (let i = a; i < b; i++)
147
+ r[i - a] = i;
148
+ return r;
149
+ }
150
+ /** Alinea `value` (decodificado) con `raw` (crudo) de forma monótona; cada carácter apunta a una posición válida dentro de raw. */
151
+ function approx(value, raw, start) {
152
+ const offs = [];
153
+ let j = 0;
154
+ for (let i = 0; i < value.length; i++) {
155
+ const v = value[i];
156
+ // Escape "\x" en raw que produce "x" en value.
157
+ if (raw[j] === "\\" && raw[j + 1] === v && v !== "\\")
158
+ j++;
159
+ // Entidad HTML en raw (&amp;) que produce un carácter distinto en value.
160
+ if (raw[j] === "&") {
161
+ const semi = raw.indexOf(";", j);
162
+ if (semi > j && semi - j <= 12 && !value.startsWith(raw.slice(j, semi + 1), i)) {
163
+ offs.push(start + j);
164
+ j = semi + 1;
165
+ continue;
166
+ }
167
+ }
168
+ if (j < raw.length && raw[j] === v) {
169
+ offs.push(start + j);
170
+ j++;
171
+ }
172
+ else {
173
+ offs.push(start + Math.min(j, raw.length - 1));
174
+ j++;
175
+ }
176
+ }
177
+ return offs;
178
+ }