polytypo 1.2.0 → 1.3.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.
- package/README.md +48 -0
- package/dist/{chunk-KAO74OJ6.js → chunk-4GYU4TXA.js} +2 -2
- package/dist/{chunk-6NTDU63P.js → chunk-5NCJ3Y65.js} +35 -11
- package/dist/chunk-5NCJ3Y65.js.map +1 -0
- package/dist/chunk-5U3DRTV7.js +205 -0
- package/dist/chunk-5U3DRTV7.js.map +1 -0
- package/dist/chunk-5VNPHCL2.cjs +264 -0
- package/dist/chunk-5VNPHCL2.cjs.map +1 -0
- package/dist/{chunk-POGDWVG5.cjs → chunk-AR6FDQAX.cjs} +40 -16
- package/dist/chunk-AR6FDQAX.cjs.map +1 -0
- package/dist/chunk-B7WXSXRG.js +31 -0
- package/dist/chunk-B7WXSXRG.js.map +1 -0
- package/dist/{chunk-VARU43JT.js → chunk-DXWMSR26.js} +4 -154
- package/dist/chunk-DXWMSR26.js.map +1 -0
- package/dist/chunk-ECYUONLP.cjs +205 -0
- package/dist/chunk-ECYUONLP.cjs.map +1 -0
- package/dist/chunk-FK3F3YZT.cjs +33 -0
- package/dist/chunk-FK3F3YZT.cjs.map +1 -0
- package/dist/chunk-QNSBCZU5.cjs +31 -0
- package/dist/chunk-QNSBCZU5.cjs.map +1 -0
- package/dist/chunk-THTCAO3O.cjs +147 -0
- package/dist/chunk-THTCAO3O.cjs.map +1 -0
- package/dist/chunk-UBEV2AC3.js +264 -0
- package/dist/chunk-UBEV2AC3.js.map +1 -0
- package/dist/chunk-V4EOJ44S.cjs +5778 -0
- package/dist/chunk-V4EOJ44S.cjs.map +1 -0
- package/dist/chunk-VC5XLP6O.js +33 -0
- package/dist/chunk-VC5XLP6O.js.map +1 -0
- package/dist/chunk-VKJVMMCI.js +5778 -0
- package/dist/chunk-VKJVMMCI.js.map +1 -0
- package/dist/{chunk-YYHKX5HE.cjs → chunk-W2T7B2SH.cjs} +3 -3
- package/dist/{chunk-YYHKX5HE.cjs.map → chunk-W2T7B2SH.cjs.map} +1 -1
- package/dist/{errors-joZChNws.d.ts → errors-CGFOVO3h.d.cts} +34 -4
- package/dist/{errors-joZChNws.d.cts → errors-CGFOVO3h.d.ts} +34 -4
- package/dist/html.cjs +15 -7
- package/dist/html.cjs.map +1 -1
- package/dist/html.d.cts +9 -3
- package/dist/html.d.ts +9 -3
- package/dist/html.js +12 -4
- package/dist/html.js.map +1 -1
- package/dist/index.cjs +33 -12
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +14 -3
- package/dist/index.d.ts +14 -3
- package/dist/index.js +28 -7
- package/dist/index.js.map +1 -1
- package/dist/markdown.cjs +15 -7
- package/dist/markdown.cjs.map +1 -1
- package/dist/markdown.d.cts +9 -3
- package/dist/markdown.d.ts +9 -3
- package/dist/markdown.js +12 -4
- package/dist/markdown.js.map +1 -1
- package/dist/text.cjs +13 -6
- package/dist/text.cjs.map +1 -1
- package/dist/text.d.cts +8 -3
- package/dist/text.d.ts +8 -3
- package/dist/text.js +10 -3
- package/dist/text.js.map +1 -1
- package/dist/yaml.cjs +29 -0
- package/dist/yaml.cjs.map +1 -0
- package/dist/yaml.d.cts +27 -0
- package/dist/yaml.d.ts +27 -0
- package/dist/yaml.js +29 -0
- package/dist/yaml.js.map +1 -0
- package/package.json +12 -2
- package/dist/chunk-44A6YP7S.js +0 -21
- package/dist/chunk-44A6YP7S.js.map +0 -1
- package/dist/chunk-5VBDCDS6.cjs +0 -19
- package/dist/chunk-5VBDCDS6.cjs.map +0 -1
- package/dist/chunk-6NTDU63P.js.map +0 -1
- package/dist/chunk-B4O4R6HI.cjs +0 -5633
- package/dist/chunk-B4O4R6HI.cjs.map +0 -1
- package/dist/chunk-HGUA2NVX.cjs +0 -21
- package/dist/chunk-HGUA2NVX.cjs.map +0 -1
- package/dist/chunk-LS57O4KJ.cjs +0 -297
- package/dist/chunk-LS57O4KJ.cjs.map +0 -1
- package/dist/chunk-OHF7OFWY.js +0 -5633
- package/dist/chunk-OHF7OFWY.js.map +0 -1
- package/dist/chunk-POGDWVG5.cjs.map +0 -1
- package/dist/chunk-RC3UKUYZ.js +0 -19
- package/dist/chunk-RC3UKUYZ.js.map +0 -1
- package/dist/chunk-VARU43JT.js.map +0 -1
- /package/dist/{chunk-KAO74OJ6.js.map → chunk-4GYU4TXA.js.map} +0 -0
package/README.md
CHANGED
|
@@ -57,6 +57,7 @@ the parsers that mode needs:
|
|
|
57
57
|
| `polytypo/text` | none |
|
|
58
58
|
| `polytypo/html` | parse5 |
|
|
59
59
|
| `polytypo/markdown` | parse5, micromark and its GFM, frontmatter and MDX extensions |
|
|
60
|
+
| `polytypo/yaml` | none |
|
|
60
61
|
| `polytypo` | all of the above |
|
|
61
62
|
|
|
62
63
|
HTML and Markdown are first-class modes, not an afterthought — tags, attributes and fenced code
|
|
@@ -69,6 +70,53 @@ transform(`<a title="test... wait">Wait... she said "go on."</a>`, { locale: "en
|
|
|
69
70
|
// <a title="test... wait">Wait… she said “go on.”</a>
|
|
70
71
|
```
|
|
71
72
|
|
|
73
|
+
`yaml` mode is the one that asks something of you, and it asks for a reason. YAML is a data format
|
|
74
|
+
with prose in some of it, so you name the keys whose values are prose; there is no default and no
|
|
75
|
+
guess:
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
import { transform } from "polytypo/yaml";
|
|
79
|
+
|
|
80
|
+
transform("summary: Rates -- all of them...\nrun: git diff -- a--b\n", {
|
|
81
|
+
locale: "en-US",
|
|
82
|
+
keys: ["summary"],
|
|
83
|
+
});
|
|
84
|
+
// summary: Rates—all of them…
|
|
85
|
+
// run: git diff -- a--b
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Nothing in YAML's syntax separates a sentence from a shell script: `description` holds one and
|
|
89
|
+
`run` holds the other, spelled identically. Without `keys` the same document comes back with
|
|
90
|
+
`if !` rewritten as `if!`. Quoting, indentation, anchors and a block scalar's chomping indicator
|
|
91
|
+
are never decoded and rewritten — the file is located, not re-emitted — so the trailing newlines
|
|
92
|
+
of a `|+` block come back exactly as you wrote them.
|
|
93
|
+
|
|
94
|
+
In `markdown` mode the skipped regions are the dialect's own structural ones, not whatever looks
|
|
95
|
+
like code. CommonMark counts an indented block as code at **four** spaces; at two it is an ordinary
|
|
96
|
+
paragraph, so a JSON sample indented by two comes back with curly quotes — valid JSON in, invalid
|
|
97
|
+
JSON out, and nothing in the return value says so. MDX has no indented code blocks at all, so there
|
|
98
|
+
even four spaces is prose and a brace in it is a JSX expression: the same sample throws
|
|
99
|
+
`POLYTYPO_MALFORMED_INPUT` instead. A fence or a code span is the one marker that means _code_ in
|
|
100
|
+
both dialects.
|
|
101
|
+
|
|
102
|
+
`analyze()` runs the same pipeline and reports what it would do instead of doing it — one record
|
|
103
|
+
per edit, each with the rule that made it and code-point offsets into the input you passed (into
|
|
104
|
+
the **document**, in `html`, `markdown` and `yaml` mode, not into a span):
|
|
105
|
+
|
|
106
|
+
```ts
|
|
107
|
+
import { analyze } from "polytypo";
|
|
108
|
+
|
|
109
|
+
analyze(`Wait... "really"?`, { locale: "en-US" });
|
|
110
|
+
// [ { ruleId: "ellipsis", start: 4, end: 7, before: "...", after: "…" },
|
|
111
|
+
// { ruleId: "quotes", start: 8, end: 9, before: `"`, after: "“" }, … ]
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
It is a report, not a patch. The list is empty exactly when `transform` would return the input
|
|
115
|
+
unchanged, and every `ruleId` is a rule that was enabled for that call — but two rules may touch
|
|
116
|
+
the same original range (French `spaces` deletes the space before `:` and `nbsp` puts a no-break
|
|
117
|
+
one back), so replaying the list is not guaranteed to reproduce the output. Call `transform` for
|
|
118
|
+
the text. Full contract: `spec/rules/analyze.md`.
|
|
119
|
+
|
|
72
120
|
The aggregate `polytypo` entry defaults `mode` to `"text"` and is the one to use when the mode is
|
|
73
121
|
chosen at runtime. `locale` has no default anywhere and must always be passed explicitly — there is
|
|
74
122
|
no silent fallback to English.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
PolytypoError
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-VKJVMMCI.js";
|
|
4
4
|
|
|
5
5
|
// src/engine/assert-fixed-mode.ts
|
|
6
6
|
function assertFixedMode(mode, fixed, entryPoint) {
|
|
@@ -14,4 +14,4 @@ function assertFixedMode(mode, fixed, entryPoint) {
|
|
|
14
14
|
export {
|
|
15
15
|
assertFixedMode
|
|
16
16
|
};
|
|
17
|
-
//# sourceMappingURL=chunk-
|
|
17
|
+
//# sourceMappingURL=chunk-4GYU4TXA.js.map
|
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
import {
|
|
2
2
|
htmlFragmentSpans,
|
|
3
3
|
isSkippedElement,
|
|
4
|
-
runOverSpans,
|
|
5
4
|
wrapParserErrors
|
|
6
|
-
} from "./chunk-
|
|
5
|
+
} from "./chunk-DXWMSR26.js";
|
|
6
|
+
import {
|
|
7
|
+
analyzeOverSpans,
|
|
8
|
+
runOverSpans
|
|
9
|
+
} from "./chunk-5U3DRTV7.js";
|
|
7
10
|
import {
|
|
8
11
|
PolytypoError,
|
|
9
12
|
getLocaleData,
|
|
10
|
-
planRules
|
|
11
|
-
|
|
13
|
+
planRules,
|
|
14
|
+
resolveNarrowTarget
|
|
15
|
+
} from "./chunk-VKJVMMCI.js";
|
|
12
16
|
|
|
13
17
|
// src/modes/markdown.ts
|
|
14
18
|
import { parse, postprocess, preprocess } from "micromark";
|
|
@@ -35,9 +39,11 @@ var SKIPPED_TOKEN_TYPES = /* @__PURE__ */ new Set([
|
|
|
35
39
|
"mdxFlowExpression",
|
|
36
40
|
"mdxTextExpression",
|
|
37
41
|
"mdxjsEsm",
|
|
38
|
-
//
|
|
39
|
-
//
|
|
40
|
-
//
|
|
42
|
+
// modes.md 3.7.3, and it matters: without it the second `---` of a YAML block reads as a setext
|
|
43
|
+
// underline, `title: Une note` becomes a paragraph, and `fr` puts a narrow no-break space in
|
|
44
|
+
// front of the colon of a machine-read metadata field. Covered by conformance fixtures
|
|
45
|
+
// en-us-markdown-{commonmark,mdx}-frontmatter, fr-markdown-{commonmark,mdx}-frontmatter-nbsp,
|
|
46
|
+
// en-us-markdown-commonmark-frontmatter-toml and -frontmatter-unterminated.
|
|
41
47
|
"frontmatter"
|
|
42
48
|
]);
|
|
43
49
|
var RAW_TAG_TOKEN_TYPES = /* @__PURE__ */ new Set([
|
|
@@ -104,8 +110,10 @@ function resolveDialect(dialect) {
|
|
|
104
110
|
`Unknown dialect "${String(dialect)}". Expected "commonmark" or "mdx".`
|
|
105
111
|
);
|
|
106
112
|
}
|
|
113
|
+
var FRONTMATTER_MATTERS = ["yaml", "toml"];
|
|
107
114
|
function extensionsFor(dialect) {
|
|
108
|
-
|
|
115
|
+
const fm = frontmatter([...FRONTMATTER_MATTERS]);
|
|
116
|
+
return dialect === "mdx" ? [fm, gfm(), mdxjs()] : [fm, gfm()];
|
|
109
117
|
}
|
|
110
118
|
function markdownSpans(source, dialect) {
|
|
111
119
|
const events = wrapParserErrors(dialect, () => tokenize(source, extensionsFor(dialect)));
|
|
@@ -150,14 +158,30 @@ function markdownSpans(source, dialect) {
|
|
|
150
158
|
|
|
151
159
|
// src/engine/markdown-pipeline.ts
|
|
152
160
|
function runMarkdownPipeline(input, options) {
|
|
161
|
+
const narrowTarget = resolveNarrowTarget(options.narrowNbsp);
|
|
153
162
|
const planned = planRules(options.rules);
|
|
154
163
|
const locale = getLocaleData(options.locale);
|
|
155
164
|
const dialect = resolveDialect(options.dialect);
|
|
156
165
|
const spans = markdownSpans(input, dialect);
|
|
157
|
-
return runOverSpans(input, spans, planned, locale, "markdown");
|
|
166
|
+
return runOverSpans(input, spans, planned, locale, "markdown", narrowTarget);
|
|
167
|
+
}
|
|
168
|
+
function analyzeMarkdownPipeline(input, options) {
|
|
169
|
+
const narrowTarget = resolveNarrowTarget(options.narrowNbsp);
|
|
170
|
+
const planned = planRules(options.rules);
|
|
171
|
+
const locale = getLocaleData(options.locale);
|
|
172
|
+
const dialect = resolveDialect(options.dialect);
|
|
173
|
+
return analyzeOverSpans(
|
|
174
|
+
input,
|
|
175
|
+
markdownSpans(input, dialect),
|
|
176
|
+
planned,
|
|
177
|
+
locale,
|
|
178
|
+
"markdown",
|
|
179
|
+
narrowTarget
|
|
180
|
+
);
|
|
158
181
|
}
|
|
159
182
|
|
|
160
183
|
export {
|
|
161
|
-
runMarkdownPipeline
|
|
184
|
+
runMarkdownPipeline,
|
|
185
|
+
analyzeMarkdownPipeline
|
|
162
186
|
};
|
|
163
|
-
//# sourceMappingURL=chunk-
|
|
187
|
+
//# sourceMappingURL=chunk-5NCJ3Y65.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/modes/markdown.ts","../src/engine/markdown-pipeline.ts"],"sourcesContent":["import { parse, postprocess, preprocess } from \"micromark\";\nimport { frontmatter } from \"micromark-extension-frontmatter\";\nimport { gfm } from \"micromark-extension-gfm\";\nimport { mdxjs } from \"micromark-extension-mdxjs\";\nimport type { Event, Extension } from \"micromark-util-types\";\nimport { PolytypoError } from \"../errors.js\";\nimport type { Dialect } from \"../types.js\";\nimport { htmlFragmentSpans, isSkippedElement } from \"./html.js\";\nimport { wrapParserErrors } from \"./parse-error.js\";\nimport type { Span } from \"./spans.js\";\n\n/**\n * spec/rules/modes.md 3.7. Each entry is skipped **whole**, including anything that looks\n * processable inside it. The complement is not enumerated: a span is emitted only for a\n * micromark `data` token, which is the tokenizer's own name for \"literal content\", so list\n * markers, emphasis delimiters, heading sequences, table padding, line endings, hard-break\n * spaces, character escapes and character references are outside every span by construction\n * rather than by a second list that could drift from the first.\n */\nconst SKIPPED_TOKEN_TYPES: ReadonlySet<string> = new Set([\n // fenced code blocks (including the info string and the fences) and indented code blocks\n \"codeFenced\",\n \"codeIndented\",\n // inline code spans, including the backticks\n \"codeText\",\n // autolinks `<https://…>`, and GFM's bare-URL form\n \"autolink\",\n \"literalAutolink\",\n // link and image destinations and titles; the definition line of a reference link. The link\n // *text* is a `labelText`, which is not skipped.\n \"resource\",\n \"reference\",\n \"definition\",\n // MDX: expression containers in full, and every JSX attribute — the whole tag is skipped, so\n // attributes never surface. JSX element *children* sit outside the tag tokens and are\n // processable.\n \"mdxFlowExpression\",\n \"mdxTextExpression\",\n \"mdxjsEsm\",\n // modes.md 3.7.3, and it matters: without it the second `---` of a YAML block reads as a setext\n // underline, `title: Une note` becomes a paragraph, and `fr` puts a narrow no-break space in\n // front of the colon of a machine-read metadata field. Covered by conformance fixtures\n // en-us-markdown-{commonmark,mdx}-frontmatter, fr-markdown-{commonmark,mdx}-frontmatter-nbsp,\n // en-us-markdown-commonmark-frontmatter-toml and -frontmatter-unterminated.\n \"frontmatter\",\n]);\n\n/** Token types whose enter/exit also maintains the skipped-element stack. */\nconst RAW_TAG_TOKEN_TYPES: ReadonlySet<string> = new Set([\n \"htmlText\",\n \"mdxJsxTextTag\",\n \"mdxJsxFlowTag\",\n]);\n\nconst LESS_THAN = 0x3c;\nconst GREATER_THAN = 0x3e;\nconst SLASH = 0x2f;\nconst EXCLAMATION = 0x21;\nconst QUESTION = 0x3f;\n\nfunction isAsciiLetter(unit: number): boolean {\n return (unit >= 0x61 && unit <= 0x7a) || (unit >= 0x41 && unit <= 0x5a);\n}\n\nfunction isTagNameUnit(unit: number): boolean {\n // Deliberately permissive: anything that is not a delimiter belongs to the name. Custom\n // elements (`my-callout`) and member expressions (`Foo.Bar`) must come out whole so that they\n // fail the skip-list membership test rather than being truncated into something that passes.\n return (\n unit !== GREATER_THAN &&\n unit !== SLASH &&\n unit !== 0x20 &&\n unit !== 0x09 &&\n unit !== 0x0a &&\n unit !== 0x0d\n );\n}\n\n/** ASCII-only, because HTML tag names are ASCII case-insensitive and `toLowerCase()` is not. */\nfunction asciiLower(value: string): string {\n let out = \"\";\n for (let i = 0; i < value.length; i += 1) {\n const unit = value.charCodeAt(i);\n out += unit >= 0x41 && unit <= 0x5a ? String.fromCharCode(unit + 0x20) : value[i];\n }\n return out;\n}\n\ninterface RawTag {\n readonly name: string;\n readonly closing: boolean;\n readonly selfClosing: boolean;\n}\n\nfunction readTag(tag: string): RawTag | null {\n if (tag.charCodeAt(0) !== LESS_THAN) return null;\n let i = 1;\n const closing = tag.charCodeAt(i) === SLASH;\n if (closing) i += 1;\n // Comments, declarations and processing instructions carry no element name.\n if (tag.charCodeAt(i) === EXCLAMATION || tag.charCodeAt(i) === QUESTION) return null;\n if (!isAsciiLetter(tag.charCodeAt(i))) return null;\n const nameAt = i;\n while (i < tag.length && isTagNameUnit(tag.charCodeAt(i))) i += 1;\n const selfClosing = tag.charCodeAt(tag.length - 2) === SLASH;\n return { name: tag.slice(nameAt, i), closing, selfClosing };\n}\n\n/**\n * modes.md 3.7: raw HTML is \"handed to the html skip list of 3.6\". Inline raw HTML reaches the\n * tokenizer as isolated tags with ordinary markdown content between them, so honouring the\n * subtree rule means tracking which skipped element is currently open. JSX names are compared\n * case-sensitively (`<Code>` is a component, `<code>` is an element); HTML names are not.\n */\nfunction updateElementStack(stack: string[], tag: string, caseSensitive: boolean): void {\n const parsed = readTag(tag);\n if (parsed === null) return;\n const name = caseSensitive ? parsed.name : asciiLower(parsed.name);\n if (parsed.closing) {\n if (stack[stack.length - 1] === name) stack.pop();\n return;\n }\n if (!parsed.selfClosing && isSkippedElement(name)) stack.push(name);\n}\n\nfunction tokenize(source: string, extensions: readonly Extension[]): Event[] {\n return postprocess(\n parse({ extensions: [...extensions] })\n .document()\n .write(preprocess()(source, null, true)),\n );\n}\n\n/**\n * modes.md 3.7.1. The dialect is the caller's, never detected: `\"commonmark\"` is CommonMark 0.31\n * plus GFM, `\"mdx\"` is the same minus indented code blocks and `<…>` autolinks, plus JSX and\n * `{…}` expression containers. Both enable frontmatter and GFM, which 3.7.2 makes normative —\n * §4's promise that table alignment rows survive is empty unless tables are recognised at all.\n */\nexport function resolveDialect(dialect: Dialect | undefined): Dialect {\n if (dialect === \"commonmark\" || dialect === \"mdx\") return dialect;\n if (dialect === undefined) {\n throw new PolytypoError(\n \"POLYTYPO_INVALID_DIALECT\",\n 'Mode \"markdown\" requires a dialect. Expected \"commonmark\" or \"mdx\"; there is no default.',\n );\n }\n throw new PolytypoError(\n \"POLYTYPO_INVALID_DIALECT\",\n `Unknown dialect \"${String(dialect)}\". Expected \"commonmark\" or \"mdx\".`,\n );\n}\n\n// modes.md 3.7.3 names both frontmatter delimiters, `---` (YAML) and `+++` (TOML), and skips\n// either whole. micromark's default matter is YAML alone, so both must be asked for by name: with\n// the default, a `+++` block parses as prose and the machine-read fields inside it get typeset.\nconst FRONTMATTER_MATTERS = [\"yaml\", \"toml\"] as const;\n\nfunction extensionsFor(dialect: Dialect): Extension[] {\n const fm = frontmatter([...FRONTMATTER_MATTERS]);\n return dialect === \"mdx\" ? [fm, gfm(), mdxjs()] : [fm, gfm()];\n}\n\nexport function markdownSpans(source: string, dialect: Dialect): Span[] {\n const events = wrapParserErrors(dialect, () => tokenize(source, extensionsFor(dialect)));\n const spans: Span[] = [];\n const elementStack: string[] = [];\n let skipDepth = 0;\n\n for (const [kind, token] of events) {\n const type = token.type;\n\n if (SKIPPED_TOKEN_TYPES.has(type)) {\n skipDepth += kind === \"enter\" ? 1 : -1;\n continue;\n }\n\n if (RAW_TAG_TOKEN_TYPES.has(type)) {\n if (kind === \"enter\") {\n updateElementStack(\n elementStack,\n source.slice(token.start.offset, token.end.offset),\n type !== \"htmlText\",\n );\n }\n skipDepth += kind === \"enter\" ? 1 : -1;\n continue;\n }\n\n // An HTML block is handed to the `html` extractor rather than skipped whole, so that the\n // prose inside `<div>…</div>` is typeset while the markup is not.\n if (type === \"htmlFlow\") {\n if (kind === \"enter\" && skipDepth === 0 && elementStack.length === 0) {\n for (const span of htmlFragmentSpans(\n source.slice(token.start.offset, token.end.offset),\n token.start.offset,\n )) {\n spans.push(span);\n }\n }\n skipDepth += kind === \"enter\" ? 1 : -1;\n continue;\n }\n\n if (kind === \"enter\" && type === \"data\" && skipDepth === 0 && elementStack.length === 0) {\n spans.push({ start: token.start.offset, end: token.end.offset });\n }\n }\n\n return spans;\n}\n","import { markdownSpans, resolveDialect } from \"../modes/markdown.js\";\nimport type { Options } from \"../types.js\";\nimport { getLocaleData } from \"./locale.js\";\nimport { resolveNarrowTarget } from \"./narrow-target.js\";\nimport { planRules } from \"./rule-runner.js\";\nimport { analyzeOverSpans, runOverSpans } from \"./span-runner.js\";\nimport type { Change } from \"./origin.js\";\n\n/**\n * `markdown` mode only. Imports the Micromark/MDX stack and `parse5` (via `../modes/markdown.js`\n * importing `../modes/html.js` for normative embedded-HTML handling, modes.md 3.7) — both are\n * legitimately reachable from `polytypo/markdown` (AUDIT_REMEDIATION_AND_RELEASE_PLAN.md 5.1).\n *\n * Validation order — `planRules`, then `getLocaleData`, then `resolveDialect`/parsing — mirrors\n * the pre-Stage-5 aggregate `runPipeline` exactly: an unknown-rule error wins over an\n * unknown-locale error, which wins over a missing/invalid dialect, which wins over a parse\n * failure. All four are public, tested behaviour this refactor was not authorised to change.\n */\nexport function runMarkdownPipeline(input: string, options: Partial<Options>): string {\n const narrowTarget = resolveNarrowTarget(options.narrowNbsp);\n const planned = planRules(options.rules);\n const locale = getLocaleData(options.locale);\n const dialect = resolveDialect(options.dialect);\n const spans = markdownSpans(input, dialect);\n return runOverSpans(input, spans, planned, locale, \"markdown\", narrowTarget);\n}\n\n/** analyze.md §1, `markdown` mode. Dialect validation happens here exactly as it does for\n * `runMarkdownPipeline`, so an absent dialect throws POLYTYPO_INVALID_DIALECT from `analyze`\n * too (analyze.md §4 A1). */\nexport function analyzeMarkdownPipeline(input: string, options: Partial<Options>): Change[] {\n const narrowTarget = resolveNarrowTarget(options.narrowNbsp);\n const planned = planRules(options.rules);\n const locale = getLocaleData(options.locale);\n const dialect = resolveDialect(options.dialect);\n return analyzeOverSpans(\n input,\n markdownSpans(input, dialect),\n planned,\n locale,\n \"markdown\",\n narrowTarget,\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAAA,SAAS,OAAO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OAAO;AAC1C,SAAO,aAAa,OAAO,OAAO,SAAS,QAAQ,YAAY,YAAY;AAC7E;AAKO,SAAS,wBAAwB,OAAe,SAAqC;AAC1F,QAAM,eAAe,oBAAoB,QAAQ,UAAU;AAC3D,QAAM,UAAU,UAAU,QAAQ,KAAK;AACvC,QAAM,SAAS,cAAc,QAAQ,MAAM;AAC3C,QAAM,UAAU,eAAe,QAAQ,OAAO;AAC9C,SAAO;AAAA,IACL;AAAA,IACA,cAAc,OAAO,OAAO;AAAA,IAC5B;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;","names":[]}
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import {
|
|
2
|
+
LINE_MARKER,
|
|
3
|
+
MARKER,
|
|
4
|
+
NO_ORIGIN,
|
|
5
|
+
PolytypoError,
|
|
6
|
+
RULES,
|
|
7
|
+
applyEdits,
|
|
8
|
+
fromCodePoints,
|
|
9
|
+
isMarker,
|
|
10
|
+
runRulesRecording,
|
|
11
|
+
toCodePoints
|
|
12
|
+
} from "./chunk-VKJVMMCI.js";
|
|
13
|
+
|
|
14
|
+
// src/modes/spans.ts
|
|
15
|
+
var SPACE = 32;
|
|
16
|
+
var LINE_TERMINATORS = /* @__PURE__ */ new Set([
|
|
17
|
+
10,
|
|
18
|
+
13,
|
|
19
|
+
11,
|
|
20
|
+
12,
|
|
21
|
+
133,
|
|
22
|
+
8232,
|
|
23
|
+
8233
|
|
24
|
+
]);
|
|
25
|
+
function gapIsLineBoundary(source, from, to) {
|
|
26
|
+
for (let i = from; i < to; i += 1) {
|
|
27
|
+
if (LINE_TERMINATORS.has(source.charCodeAt(i))) return true;
|
|
28
|
+
}
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
function normalizeSpans(spans) {
|
|
32
|
+
const sorted = spans.filter((s) => s.end > s.start).sort((a, b) => a.start - b.start);
|
|
33
|
+
const out = [];
|
|
34
|
+
for (const span of sorted) {
|
|
35
|
+
const last = out[out.length - 1];
|
|
36
|
+
if (last === void 0) {
|
|
37
|
+
out.push(span);
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
if (span.start < last.end) {
|
|
41
|
+
throw new PolytypoError(
|
|
42
|
+
"POLYTYPO_RULE_CONTRACT",
|
|
43
|
+
`mode extractor produced overlapping spans (${last.start}, ${last.end}) and (${span.start}, ${span.end})`
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
if (span.start === last.end) {
|
|
47
|
+
out[out.length - 1] = { start: last.start, end: span.end };
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
out.push(span);
|
|
51
|
+
}
|
|
52
|
+
return out;
|
|
53
|
+
}
|
|
54
|
+
function concatenateSpans(source, spans) {
|
|
55
|
+
const cp = [];
|
|
56
|
+
let previous;
|
|
57
|
+
for (const span of spans) {
|
|
58
|
+
if (previous !== void 0) {
|
|
59
|
+
cp.push(gapIsLineBoundary(source, previous.end, span.start) ? LINE_MARKER : MARKER);
|
|
60
|
+
}
|
|
61
|
+
for (const value of toCodePoints(source.slice(span.start, span.end))) cp.push(value);
|
|
62
|
+
previous = span;
|
|
63
|
+
}
|
|
64
|
+
return cp;
|
|
65
|
+
}
|
|
66
|
+
function spanRangesOf(cp) {
|
|
67
|
+
const ranges = [];
|
|
68
|
+
let first = 0;
|
|
69
|
+
for (let i = 0; i < cp.length; i += 1) {
|
|
70
|
+
if (isMarker(cp[i])) {
|
|
71
|
+
ranges.push({ first, last: i - 1 });
|
|
72
|
+
first = i + 1;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
ranges.push({ first, last: cp.length - 1 });
|
|
76
|
+
return ranges;
|
|
77
|
+
}
|
|
78
|
+
function spanContaining(ranges, p) {
|
|
79
|
+
for (const range of ranges) {
|
|
80
|
+
if (p >= range.first && p <= range.last + 1) return range;
|
|
81
|
+
}
|
|
82
|
+
return void 0;
|
|
83
|
+
}
|
|
84
|
+
function filterBoundaryEdits(cp, edits, ranges) {
|
|
85
|
+
const out = [];
|
|
86
|
+
for (const edit of edits) {
|
|
87
|
+
let containsMarker = false;
|
|
88
|
+
for (let i = edit.start; i < edit.end; i += 1) {
|
|
89
|
+
if (isMarker(cp[i])) {
|
|
90
|
+
containsMarker = true;
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (containsMarker) continue;
|
|
95
|
+
const p = edit.start;
|
|
96
|
+
const q = edit.end - 1;
|
|
97
|
+
const d = edit.end - edit.start;
|
|
98
|
+
const r = edit.replacement.length;
|
|
99
|
+
const span = spanContaining(ranges, p);
|
|
100
|
+
if (span !== void 0 && (p === span.first || q === span.last)) {
|
|
101
|
+
if (r > d) continue;
|
|
102
|
+
const first = edit.replacement[0];
|
|
103
|
+
const last = edit.replacement[r - 1];
|
|
104
|
+
if (r > 0 && p === span.first && first === SPACE && cp[p] !== SPACE) continue;
|
|
105
|
+
if (r > 0 && q === span.last && last === SPACE && cp[q] !== SPACE) continue;
|
|
106
|
+
}
|
|
107
|
+
out.push(edit);
|
|
108
|
+
}
|
|
109
|
+
return out;
|
|
110
|
+
}
|
|
111
|
+
function splitOnMarker(cp, expected) {
|
|
112
|
+
const pieces = [[]];
|
|
113
|
+
for (const value of cp) {
|
|
114
|
+
if (isMarker(value)) {
|
|
115
|
+
pieces.push([]);
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
pieces[pieces.length - 1].push(value);
|
|
119
|
+
}
|
|
120
|
+
if (pieces.length !== expected) {
|
|
121
|
+
throw new PolytypoError(
|
|
122
|
+
"POLYTYPO_RULE_CONTRACT",
|
|
123
|
+
`boundary markers did not survive the pipeline: expected ${expected} spans, found ${pieces.length}`
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
return pieces;
|
|
127
|
+
}
|
|
128
|
+
function originOfSpans(source, spans) {
|
|
129
|
+
const codePointIndexOf = new Array(source.length + 1);
|
|
130
|
+
let cpIndex = 0;
|
|
131
|
+
for (let i = 0; i < source.length; ) {
|
|
132
|
+
codePointIndexOf[i] = cpIndex;
|
|
133
|
+
const code = source.codePointAt(i);
|
|
134
|
+
const width = code > 65535 ? 2 : 1;
|
|
135
|
+
if (width === 2) codePointIndexOf[i + 1] = cpIndex;
|
|
136
|
+
i += width;
|
|
137
|
+
cpIndex += 1;
|
|
138
|
+
}
|
|
139
|
+
codePointIndexOf[source.length] = cpIndex;
|
|
140
|
+
const origin = [];
|
|
141
|
+
let previous;
|
|
142
|
+
for (const span of spans) {
|
|
143
|
+
if (previous !== void 0) origin.push(NO_ORIGIN);
|
|
144
|
+
const base = codePointIndexOf[span.start];
|
|
145
|
+
const length = toCodePoints(source.slice(span.start, span.end)).length;
|
|
146
|
+
for (let k = 0; k < length; k += 1) origin.push(base + k);
|
|
147
|
+
previous = span;
|
|
148
|
+
}
|
|
149
|
+
return origin;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// src/engine/span-runner.ts
|
|
153
|
+
function runRulesOverSpans(cp, planned, locale, mode, narrowTarget) {
|
|
154
|
+
let current = cp;
|
|
155
|
+
for (const id of planned) {
|
|
156
|
+
const rule = RULES[id];
|
|
157
|
+
if (rule === void 0) continue;
|
|
158
|
+
const edits = rule.apply({ cp: current, locale, mode, narrowTarget });
|
|
159
|
+
current = applyEdits(current, filterBoundaryEdits(current, edits, spanRangesOf(current)), id);
|
|
160
|
+
}
|
|
161
|
+
return current;
|
|
162
|
+
}
|
|
163
|
+
function runOverSpans(source, spans, planned, locale, mode, narrowTarget) {
|
|
164
|
+
const normalized = normalizeSpans(spans);
|
|
165
|
+
if (normalized.length === 0) return source;
|
|
166
|
+
const transformed = runRulesOverSpans(
|
|
167
|
+
concatenateSpans(source, normalized),
|
|
168
|
+
planned,
|
|
169
|
+
locale,
|
|
170
|
+
mode,
|
|
171
|
+
narrowTarget
|
|
172
|
+
);
|
|
173
|
+
const pieces = splitOnMarker(transformed, normalized.length);
|
|
174
|
+
let out = "";
|
|
175
|
+
let cursor = 0;
|
|
176
|
+
for (let i = 0; i < normalized.length; i += 1) {
|
|
177
|
+
const span = normalized[i];
|
|
178
|
+
const replacement = fromCodePoints(pieces[i]);
|
|
179
|
+
const original = source.slice(span.start, span.end);
|
|
180
|
+
out += source.slice(cursor, span.start);
|
|
181
|
+
out += replacement === original ? original : replacement;
|
|
182
|
+
cursor = span.end;
|
|
183
|
+
}
|
|
184
|
+
return out + source.slice(cursor);
|
|
185
|
+
}
|
|
186
|
+
function analyzeOverSpans(source, spans, planned, locale, mode, narrowTarget) {
|
|
187
|
+
const normalized = normalizeSpans(spans);
|
|
188
|
+
if (normalized.length === 0) return [];
|
|
189
|
+
return runRulesRecording(
|
|
190
|
+
concatenateSpans(source, normalized),
|
|
191
|
+
planned,
|
|
192
|
+
locale,
|
|
193
|
+
mode,
|
|
194
|
+
narrowTarget,
|
|
195
|
+
originOfSpans(source, normalized),
|
|
196
|
+
toCodePoints(source).length,
|
|
197
|
+
(current, edits) => filterBoundaryEdits(current, edits, spanRangesOf(current))
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export {
|
|
202
|
+
runOverSpans,
|
|
203
|
+
analyzeOverSpans
|
|
204
|
+
};
|
|
205
|
+
//# sourceMappingURL=chunk-5U3DRTV7.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/modes/spans.ts","../src/engine/span-runner.ts"],"sourcesContent":["import { toCodePoints } from \"../engine/codepoints.js\";\nimport { isMarker, LINE_MARKER, MARKER } from \"../engine/sentinels.js\";\nimport { PolytypoError } from \"../errors.js\";\nimport type { Edit } from \"../types.js\";\nimport { NO_ORIGIN } from \"../engine/origin.js\";\n\n/**\n * The boundary markers are defined in the engine (src/engine/sentinels.ts), which is where all\n * three non-code-point sentinels live and where their disjointness is maintained. They are\n * re-exported here because the mode layer is what writes them into the array.\n *\n * Which marker separates two spans is decided by the **raw source bytes of the gap between\n * them**, so it is decidable without asking the parser anything and is identical in five\n * runtimes: a gap containing a line terminator gives `LINE_MARKER`, anything else `MARKER`.\n */\nexport { LINE_MARKER, MARKER, isMarker } from \"../engine/sentinels.js\";\n\n/** `BREAK` as the rules define it (`spaces.md` 3.1), tested against the gap's raw source. */\n/** U+0020, the one emitted code point whose meaning is positional (modes.md 3.4, 5 item 2). */\nconst SPACE = 0x20;\n\nconst LINE_TERMINATORS: ReadonlySet<number> = new Set([\n 0x0a, 0x0d, 0x0b, 0x0c, 0x85, 0x2028, 0x2029,\n]);\n\nfunction gapIsLineBoundary(source: string, from: number, to: number): boolean {\n for (let i = from; i < to; i += 1) {\n if (LINE_TERMINATORS.has(source.charCodeAt(i))) return true;\n }\n return false;\n}\n\n/**\n * A processable span, identified by its offsets in the **original source**. Offsets are UTF-16\n * indices into the JS source string, which is what every JS parser reports; they are an\n * implementation detail of this runtime and never reach the rules, which index code points.\n */\nexport interface Span {\n readonly start: number;\n readonly end: number;\n}\n\n/** A span's extent in the concatenated code-point array: `s₀` and `s₁` of modes.md 3.4. */\nexport interface SpanRange {\n readonly first: number;\n readonly last: number;\n}\n\n/**\n * Sort, drop empties, and coalesce spans separated by nothing in the source (modes.md 7.5:\n * a parser that reports one text run as two adjacent nodes must not manufacture a boundary).\n * Overlapping spans are an extractor bug and are rejected rather than silently merged.\n */\nexport function normalizeSpans(spans: readonly Span[]): Span[] {\n const sorted = spans.filter((s) => s.end > s.start).sort((a, b) => a.start - b.start);\n const out: Span[] = [];\n for (const span of sorted) {\n const last = out[out.length - 1];\n if (last === undefined) {\n out.push(span);\n continue;\n }\n if (span.start < last.end) {\n throw new PolytypoError(\n \"POLYTYPO_RULE_CONTRACT\",\n `mode extractor produced overlapping spans (${last.start}, ${last.end}) and (${span.start}, ${span.end})`,\n );\n }\n if (span.start === last.end) {\n out[out.length - 1] = { start: last.start, end: span.end };\n continue;\n }\n out.push(span);\n }\n return out;\n}\n\n/** `S₁ ⌢ [marker] ⌢ S₂ ⌢ … ⌢ Sₘ` (modes.md 3.5 step 2). */\nexport function concatenateSpans(source: string, spans: readonly Span[]): number[] {\n const cp: number[] = [];\n let previous: Span | undefined;\n for (const span of spans) {\n if (previous !== undefined) {\n cp.push(gapIsLineBoundary(source, previous.end, span.start) ? LINE_MARKER : MARKER);\n }\n for (const value of toCodePoints(source.slice(span.start, span.end))) cp.push(value);\n previous = span;\n }\n return cp;\n}\n\n/**\n * The span extents of the array as it stands. Recomputed after every rule, because applying\n * edits shifts every index after the first one — the markers themselves are guaranteed to\n * survive, since no edit may contain one.\n */\nexport function spanRangesOf(cp: readonly number[]): SpanRange[] {\n const ranges: SpanRange[] = [];\n let first = 0;\n for (let i = 0; i < cp.length; i += 1) {\n if (isMarker(cp[i] as number)) {\n ranges.push({ first, last: i - 1 });\n first = i + 1;\n }\n }\n ranges.push({ first, last: cp.length - 1 });\n return ranges;\n}\n\nfunction spanContaining(ranges: readonly SpanRange[], p: number): SpanRange | undefined {\n for (const range of ranges) {\n if (p >= range.first && p <= range.last + 1) return range;\n }\n return undefined;\n}\n\n/**\n * modes.md 3.4, two safety nets, both pure functions of `(p, q, r, s₀, s₁)` — so the verdict is\n * identical on every run, which is what makes redistribution deterministic (modes.md 5, point 3).\n *\n * 1. **No edit may contain a marker.** No rule can produce one; one that does is a bug, and the\n * edit is discarded rather than treated as a redistribution question.\n * 2. **The edge-growth rule.** An edit is discarded if it would place code points at an\n * extremity of its span that were not there before: with `d = q - p + 1` the replaced length\n * and `r` the replacement length, discard when `p = s₀ and r > d`, or `q = s₁ and r > d`.\n *\n * The length test is the whole rule and needs no knowledge of Markdown or HTML syntax. It\n * separates exactly the cases that matter: `\"` → `“` at an edge is 1 → 1 and applies; `--` →\n * `␣–␣` at an edge is 2 → 3 and is discarded, while the same edit interior to a span applies;\n * `(c)` → `©` is 3 → 1 and applies, because shrinking is always safe. An insertion has `d = 0`,\n * so it is discarded exactly when its position coincides with a span edge — the rule this one\n * generalises.\n *\n * **Deletion at an edge is not restricted here**, and must not be: `r > d` is false for a\n * deletion, so this filter never sees one. Deletion at an edge is governed by modes.md 3.3's\n * *Edge tests* clause instead — where a rule asks \"am I at the edge of the text I am allowed to\n * modify\" rather than \"what character is here\", the marker behaves as `NONE`. That clause is the\n * one place a rule may treat a span edge as the end of the text, it exists because `spaces` is\n * the only rule that deletes, and 3.3 states the division: **a rule that deletes must treat a\n * span edge as the end of the text; a rule that replaces or inserts must not, and is governed by\n * this filter.** The single test that claims the clause is `spaces.md` 3.2 step 4.\n */\nexport function filterBoundaryEdits(\n cp: readonly number[],\n edits: readonly Edit[],\n ranges: readonly SpanRange[],\n): Edit[] {\n const out: Edit[] = [];\n for (const edit of edits) {\n let containsMarker = false;\n for (let i = edit.start; i < edit.end; i += 1) {\n if (isMarker(cp[i] as number)) {\n containsMarker = true;\n break;\n }\n }\n if (containsMarker) continue;\n\n // `[start, end)` in the engine's half-open form is `cp[p … q]` with p = start, q = end - 1;\n // an insertion is `q = p - 1`, which falls out of the same expression.\n const p = edit.start;\n const q = edit.end - 1;\n const d = edit.end - edit.start;\n const r = edit.replacement.length;\n const span = spanContaining(ranges, p);\n if (span !== undefined && (p === span.first || q === span.last)) {\n if (r > d) continue;\n // The character clause. `r > d` is not the rule, only a formalisation of it that misses\n // `r === d`: `dashes` P3 admits a run of THREE dashes, so `---` -> `␣–␣` is 3 -> 3 and the\n // length test sees nothing while U+0020 lands on both extremities anyway. Testing the\n // character catches it, and only U+0020 needs testing — it is the one code point any rule\n // emits whose meaning comes from its position rather than from itself (modes.md 5 item 2).\n const first = edit.replacement[0];\n const last = edit.replacement[r - 1];\n if (r > 0 && p === span.first && first === SPACE && cp[p] !== SPACE) continue;\n if (r > 0 && q === span.last && last === SPACE && cp[q] !== SPACE) continue;\n }\n\n out.push(edit);\n }\n return out;\n}\n\n/** Redistribute the transformed array back to one piece per span (modes.md 3.5 step 4). */\nexport function splitOnMarker(cp: readonly number[], expected: number): number[][] {\n const pieces: number[][] = [[]];\n for (const value of cp) {\n if (isMarker(value)) {\n pieces.push([]);\n continue;\n }\n (pieces[pieces.length - 1] as number[]).push(value);\n }\n if (pieces.length !== expected) {\n throw new PolytypoError(\n \"POLYTYPO_RULE_CONTRACT\",\n `boundary markers did not survive the pipeline: expected ${expected} spans, found ${pieces.length}`,\n );\n }\n return pieces;\n}\n\n/**\n * The origin map for `concatenateSpans` (analyze.md §2): for every code point of the joined\n * array, the code-point offset of the character it came from IN THE DOCUMENT, and NO_ORIGIN for\n * the markers, which came from nowhere. `Span` bounds are native string indices, so the\n * conversion to code-point offsets happens here and not in the caller — this is the one place\n * that knows both coordinate systems.\n */\nexport function originOfSpans(source: string, spans: readonly Span[]): number[] {\n const codePointIndexOf = new Array<number>(source.length + 1);\n let cpIndex = 0;\n for (let i = 0; i < source.length;) {\n codePointIndexOf[i] = cpIndex;\n const code = source.codePointAt(i) as number;\n const width = code > 0xffff ? 2 : 1;\n if (width === 2) codePointIndexOf[i + 1] = cpIndex;\n i += width;\n cpIndex += 1;\n }\n codePointIndexOf[source.length] = cpIndex;\n\n const origin: number[] = [];\n let previous: Span | undefined;\n for (const span of spans) {\n if (previous !== undefined) origin.push(NO_ORIGIN);\n const base = codePointIndexOf[span.start] as number;\n const length = toCodePoints(source.slice(span.start, span.end)).length;\n for (let k = 0; k < length; k += 1) origin.push(base + k);\n previous = span;\n }\n return origin;\n}\n","import {\n concatenateSpans,\n filterBoundaryEdits,\n normalizeSpans,\n originOfSpans,\n spanRangesOf,\n splitOnMarker,\n type Span,\n} from \"../modes/spans.js\";\nimport { RULES } from \"../rules/registry.js\";\nimport type { LocaleData, Mode, RuleId } from \"../types.js\";\nimport { applyEdits } from \"./edits.js\";\nimport { fromCodePoints, toCodePoints } from \"./codepoints.js\";\nimport type { Change } from \"./origin.js\";\nimport { runRulesRecording } from \"./rule-runner.js\";\n\n/**\n * The same sequence as `runRules` (`./rule-runner.js`), with the two boundary filters of\n * modes.md 3.4 interposed. The span extents are recomputed after every rule, because applying\n * an edit shifts every index after it; the markers themselves always survive, since no edit may\n * contain one.\n */\nfunction runRulesOverSpans(\n cp: readonly number[],\n planned: readonly RuleId[],\n locale: LocaleData,\n mode: Mode,\n narrowTarget: number,\n): readonly number[] {\n let current = cp;\n for (const id of planned) {\n const rule = RULES[id];\n if (rule === undefined) continue;\n const edits = rule.apply({ cp: current, locale, mode, narrowTarget });\n current = applyEdits(current, filterBoundaryEdits(current, edits, spanRangesOf(current)), id);\n }\n return current;\n}\n\n/**\n * modes.md 3.5. The pipeline runs **once**, over the marker-separated concatenation of every\n * processable span — not per span, which would pair quotation marks in isolation, and not over a\n * naive concatenation, which would manufacture adjacencies the document does not have.\n *\n * The output is the input with a set of disjoint substring replacements applied and nothing else\n * (modes.md 4). A span whose content the rules did not change contributes no replacement, so a\n * document needing no changes comes back byte-identical; the parser located the spans and was\n * then discarded, and the document is never serialised.\n */\nexport function runOverSpans(\n source: string,\n spans: readonly Span[],\n planned: readonly RuleId[],\n locale: LocaleData,\n mode: Mode,\n narrowTarget: number,\n): string {\n const normalized = normalizeSpans(spans);\n if (normalized.length === 0) return source;\n\n const transformed = runRulesOverSpans(\n concatenateSpans(source, normalized),\n planned,\n locale,\n mode,\n narrowTarget,\n );\n const pieces = splitOnMarker(transformed, normalized.length);\n\n let out = \"\";\n let cursor = 0;\n for (let i = 0; i < normalized.length; i += 1) {\n const span = normalized[i] as Span;\n const replacement = fromCodePoints(pieces[i] as number[]);\n const original = source.slice(span.start, span.end);\n out += source.slice(cursor, span.start);\n out += replacement === original ? original : replacement;\n cursor = span.end;\n }\n return out + source.slice(cursor);\n}\n\n/**\n * `runOverSpans`, reporting instead of applying (analyze.md §1). The span table supplies the\n * origin map, so every change comes back in DOCUMENT coordinates — analyze.md §6 names a\n * runtime that reports span-local offsets here as the mistake that passes every text-mode test.\n */\nexport function analyzeOverSpans(\n source: string,\n spans: readonly Span[],\n planned: readonly RuleId[],\n locale: LocaleData,\n mode: Mode,\n narrowTarget: number,\n): Change[] {\n const normalized = normalizeSpans(spans);\n if (normalized.length === 0) return [];\n return runRulesRecording(\n concatenateSpans(source, normalized),\n planned,\n locale,\n mode,\n narrowTarget,\n originOfSpans(source, normalized),\n toCodePoints(source).length,\n (current, edits) => filterBoundaryEdits(current, edits, spanRangesOf(current)),\n );\n}\n"],"mappings":";;;;;;;;;;;;;;AAmBA,IAAM,QAAQ;AAEd,IAAM,mBAAwC,oBAAI,IAAI;AAAA,EACpD;AAAA,EAAM;AAAA,EAAM;AAAA,EAAM;AAAA,EAAM;AAAA,EAAM;AAAA,EAAQ;AACxC,CAAC;AAED,SAAS,kBAAkB,QAAgB,MAAc,IAAqB;AAC5E,WAAS,IAAI,MAAM,IAAI,IAAI,KAAK,GAAG;AACjC,QAAI,iBAAiB,IAAI,OAAO,WAAW,CAAC,CAAC,EAAG,QAAO;AAAA,EACzD;AACA,SAAO;AACT;AAuBO,SAAS,eAAe,OAAgC;AAC7D,QAAM,SAAS,MAAM,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK,CAAC,GAAG,MAAM,EAAE,QAAQ,EAAE,KAAK;AACpF,QAAM,MAAc,CAAC;AACrB,aAAW,QAAQ,QAAQ;AACzB,UAAM,OAAO,IAAI,IAAI,SAAS,CAAC;AAC/B,QAAI,SAAS,QAAW;AACtB,UAAI,KAAK,IAAI;AACb;AAAA,IACF;AACA,QAAI,KAAK,QAAQ,KAAK,KAAK;AACzB,YAAM,IAAI;AAAA,QACR;AAAA,QACA,8CAA8C,KAAK,KAAK,KAAK,KAAK,GAAG,UAAU,KAAK,KAAK,KAAK,KAAK,GAAG;AAAA,MACxG;AAAA,IACF;AACA,QAAI,KAAK,UAAU,KAAK,KAAK;AAC3B,UAAI,IAAI,SAAS,CAAC,IAAI,EAAE,OAAO,KAAK,OAAO,KAAK,KAAK,IAAI;AACzD;AAAA,IACF;AACA,QAAI,KAAK,IAAI;AAAA,EACf;AACA,SAAO;AACT;AAGO,SAAS,iBAAiB,QAAgB,OAAkC;AACjF,QAAM,KAAe,CAAC;AACtB,MAAI;AACJ,aAAW,QAAQ,OAAO;AACxB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