@alauda/doom 2.8.0 → 2.8.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.
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { visit } from 'unist-util-visit';
|
|
2
2
|
import { explainUnparsedEmphasis, findUnparsedEmphasis, } from "../remark-lint/no-unparsed-emphasis.js";
|
|
3
|
-
import { collectComponents, collectHeadingDepths, } from "../remark-lint/translation-parity/shared.js";
|
|
3
|
+
import { collectComponents, collectHeadingDepths, diffMultiset, } from "../remark-lint/translation-parity/shared.js";
|
|
4
|
+
import { collectProseUrls } from "../remark-lint/translation-parity/url-residue.js";
|
|
4
5
|
import { countPlaceholders } from "./translate-mask.js";
|
|
5
6
|
import { parseAttributeElement } from "./translate-segment.js";
|
|
6
7
|
/**
|
|
@@ -140,11 +141,13 @@ const collectTrailingAnchors = (tree) => {
|
|
|
140
141
|
/**
|
|
141
142
|
* The skeleton is the source's, not the model's.
|
|
142
143
|
*
|
|
143
|
-
* Headings and
|
|
144
|
-
* never changes a heading's level
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
*
|
|
144
|
+
* Headings, components and URLs written in prose are structure rather than
|
|
145
|
+
* prose: translating a page never changes a heading's level, never adds or
|
|
146
|
+
* removes a component, and never drops a URL that markdown left as plain text.
|
|
147
|
+
* All three are already checked over the whole document by
|
|
148
|
+
* `translation-heading-sequence`, `translation-component-multiset` and
|
|
149
|
+
* `translation-url-residue` — this runs the *same* collectors, one segment at
|
|
150
|
+
* a time.
|
|
148
151
|
*
|
|
149
152
|
* That is not a duplicate of those rules, it is where they become actionable.
|
|
150
153
|
* Every pairwise rule reports against the document as a whole, with no line
|
|
@@ -193,6 +196,26 @@ const structureFindings = (segment, tree, processor) => {
|
|
|
193
196
|
: `This segment's translation invented ${got - want} \`<${name}>\` (the source has ${want}, the translation has ${got}).`,
|
|
194
197
|
});
|
|
195
198
|
}
|
|
199
|
+
// URLs markdown did not turn into links.
|
|
200
|
+
//
|
|
201
|
+
// The whole-document rule that owns this comparison,
|
|
202
|
+
// `translation-url-residue`, is the one that proved the note above the hard
|
|
203
|
+
// way: it fired on an assembled page, had no segment to be routed to, and
|
|
204
|
+
// failed the document with no repair round — on a false positive, and on
|
|
205
|
+
// every build for a day. Checked here it arrives with a segment attached, so
|
|
206
|
+
// a genuine loss is retranslated instead of being fatal, and the whole-page
|
|
207
|
+
// rule goes back to meaning what the note says it means: assembly did it.
|
|
208
|
+
const expectedUrls = collectProseUrls(source);
|
|
209
|
+
const actualUrls = collectProseUrls(tree);
|
|
210
|
+
const { missing: missingUrls } = diffMultiset(expectedUrls, actualUrls);
|
|
211
|
+
if (missingUrls.length > 0) {
|
|
212
|
+
findings.push({
|
|
213
|
+
rule: 'doom-translate:segment-url-residue',
|
|
214
|
+
reason: `This segment's translation dropped ${missingUrls.length} URL(s) that the source writes out in prose: ${missingUrls
|
|
215
|
+
.map((url) => `\`${url}\``)
|
|
216
|
+
.join(', ')}. They are plain text rather than links, so nothing carries them through for you — reproduce each one exactly as the source spells it.`,
|
|
217
|
+
});
|
|
218
|
+
}
|
|
196
219
|
// Only meaningful while the headings still correspond one to one; when they
|
|
197
220
|
// do not, the sequence finding above already says so and pairing by index
|
|
198
221
|
// would invent a second, misleading one.
|
|
@@ -19,11 +19,108 @@ import { currentPair, diffMultiset } from "./shared.js";
|
|
|
19
19
|
* between prose and inline code. Those are identifiers, not links, and a guard
|
|
20
20
|
* that cries wolf about them is worse than no guard at all.
|
|
21
21
|
*
|
|
22
|
-
* Honest caveat
|
|
23
|
-
*
|
|
24
|
-
*
|
|
22
|
+
* Honest caveat, kept because it turned out to matter: when this shipped,
|
|
23
|
+
* measured exposure on the corpus was zero — all seven bare URLs carried
|
|
24
|
+
* `https://`, which makes them link nodes, protected structurally. It was a
|
|
25
|
+
* guard for a case nothing else covered, not a demonstrated detector. Its
|
|
26
|
+
* first meeting with real prose was a false positive that took a build down
|
|
27
|
+
* for a day; what that cost, and what it changed, is written up on
|
|
28
|
+
* {@link BARE_URL} and {@link HAS_HOST}.
|
|
25
29
|
*/
|
|
26
|
-
|
|
30
|
+
/**
|
|
31
|
+
* A URL runs to the first character a URL cannot contain.
|
|
32
|
+
*
|
|
33
|
+
* This used to be `\S+`, "everything up to the next space" — and Chinese does
|
|
34
|
+
* not write spaces. On the translation side the match therefore swallowed the
|
|
35
|
+
* rest of the sentence: `(https://)和路径` yielded `https://)和路径`, which
|
|
36
|
+
* could not equal anything the English side produced no matter how faithful
|
|
37
|
+
* the translation was. `[!-~]` is printable ASCII, which is what a URL is made
|
|
38
|
+
* of, so a match now ends at the first full-width bracket or Han character.
|
|
39
|
+
*
|
|
40
|
+
* `[^\s\P{ASCII}]` reads as a double negative because that is the only way to
|
|
41
|
+
* say it without an opaque code-point range: not whitespace, and not outside
|
|
42
|
+
* ASCII.
|
|
43
|
+
*
|
|
44
|
+
* The letters are spelled out rather than left to an `i` flag, and that is not
|
|
45
|
+
* style. Under `iu` the engine case-folds before testing the class, and two
|
|
46
|
+
* ASCII letters fold to non-ASCII ones — `s` to `ſ` (U+017F), `k` to `K`
|
|
47
|
+
* (U+212A) — so `\P{ASCII}` would swallow them and the negated class would
|
|
48
|
+
* *reject* `s` and `k`. Caught by the one test with an `s` mid-URL:
|
|
49
|
+
* `ftp://h/Foo_(disambiguation)` matched as far as `ftp://h/Foo_(di`.
|
|
50
|
+
*
|
|
51
|
+
* That misfire is not hypothetical and it was not cheap. `connectors-operator`
|
|
52
|
+
* release-1.14 was red on every run for 22 hours over one line of prose —
|
|
53
|
+
* `includes the correct protocol (https://) and path` — where nothing had been
|
|
54
|
+
* lost at all: the translation said the same thing with full-width brackets.
|
|
55
|
+
* And because this rule reports against the whole page, the assembled document
|
|
56
|
+
* could not attribute the finding to any segment and failed outright without a
|
|
57
|
+
* repair round. A misfire here does not cost "one finding to read".
|
|
58
|
+
*
|
|
59
|
+
* The price of the narrowing, stated plainly: an internationalised host
|
|
60
|
+
* written out in prose (`https://例え.jp`) is no longer detected. That is the
|
|
61
|
+
* right trade — the sources are English technical documentation, and a
|
|
62
|
+
* detector that cannot agree with its own translation is not a detector.
|
|
63
|
+
*
|
|
64
|
+
* The sibling rule made the opposite choice, and deliberately:
|
|
65
|
+
* `translation-link-isomorphism` *reports* the full-width stop an autolink
|
|
66
|
+
* absorbed rather than normalising it away, because there the punctuation ends
|
|
67
|
+
* up inside a real `href` and the link genuinely breaks. Prose has no `href`
|
|
68
|
+
* to break — `(ftp://h/x)` reads exactly as intended — so here the two
|
|
69
|
+
* spellings are made comparable instead of being reported.
|
|
70
|
+
*/
|
|
71
|
+
const BARE_URL = /\b(?:[a-zA-Z][a-zA-Z0-9+.-]*:\/\/|[wW]{3}\.)[^\s\P{ASCII}]+/gu;
|
|
72
|
+
/**
|
|
73
|
+
* Punctuation that ends the sentence rather than the URL.
|
|
74
|
+
*
|
|
75
|
+
* `(see ftp://h/x)` and `fetch ftp://h/x.tar.` both put a character in the
|
|
76
|
+
* match that the writer did not mean as part of the address, and the
|
|
77
|
+
* translation — which repunctuates, because that is what translating prose
|
|
78
|
+
* does — will not have it. Trimmed the way GFM's autolink extension trims it,
|
|
79
|
+
* which is also the way a reader would.
|
|
80
|
+
*
|
|
81
|
+
* Only ASCII forms are listed: {@link BARE_URL} cannot match anything else, so
|
|
82
|
+
* a full-width bracket here would be a branch that can never be taken.
|
|
83
|
+
*/
|
|
84
|
+
const TRAILING_PUNCTUATION = /[!"'*,.:;?_~]+$/u;
|
|
85
|
+
/**
|
|
86
|
+
* Closing brackets, and the opener each needs in order to belong to the URL.
|
|
87
|
+
*
|
|
88
|
+
* So `ftp://h/Foo_(disambiguation)` keeps its parenthesis — the URL opened it
|
|
89
|
+
* — and `(see ftp://h/x)` does not.
|
|
90
|
+
*/
|
|
91
|
+
const BRACKETS = new Map([
|
|
92
|
+
[')', '('],
|
|
93
|
+
[']', '['],
|
|
94
|
+
['}', '{'],
|
|
95
|
+
]);
|
|
96
|
+
const occurrences = (value, character) => value.split(character).length - 1;
|
|
97
|
+
const trimSentence = (url) => {
|
|
98
|
+
let value = url;
|
|
99
|
+
for (;;) {
|
|
100
|
+
const before = value;
|
|
101
|
+
value = value.replace(TRAILING_PUNCTUATION, '');
|
|
102
|
+
const closing = value.at(-1);
|
|
103
|
+
const opening = closing == null ? undefined : BRACKETS.get(closing);
|
|
104
|
+
if (closing != null &&
|
|
105
|
+
opening != null &&
|
|
106
|
+
occurrences(value, closing) > occurrences(value, opening)) {
|
|
107
|
+
value = value.slice(0, -1);
|
|
108
|
+
}
|
|
109
|
+
if (value === before) {
|
|
110
|
+
return value;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
/**
|
|
115
|
+
* A scheme with nothing after it is not a URL.
|
|
116
|
+
*
|
|
117
|
+
* `https://` on its own is prose — a sentence naming the protocol, as in "the
|
|
118
|
+
* address must include the protocol (https://)". Requiring a host is what
|
|
119
|
+
* tells the two apart, and it gives up nothing this rule was protecting: an
|
|
120
|
+
* address with no host cannot be fetched, so it cannot be a link a translation
|
|
121
|
+
* has to carry through.
|
|
122
|
+
*/
|
|
123
|
+
const HAS_HOST = /(?::\/\/|^[wW]{3}\.)[^\s\P{ASCII}]/u;
|
|
27
124
|
/**
|
|
28
125
|
* Text inside a link is not prose for this purpose.
|
|
29
126
|
*
|
|
@@ -49,7 +146,10 @@ export const collectProseUrls = (tree) => {
|
|
|
49
146
|
return;
|
|
50
147
|
}
|
|
51
148
|
for (const [match] of node.value.matchAll(BARE_URL)) {
|
|
52
|
-
|
|
149
|
+
const url = trimSentence(match);
|
|
150
|
+
if (HAS_HOST.test(url)) {
|
|
151
|
+
found.push(url);
|
|
152
|
+
}
|
|
53
153
|
}
|
|
54
154
|
});
|
|
55
155
|
return found;
|