cleanpastedtext 0.1.0 → 0.2.0
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/dist/index.d.ts +27 -0
- package/dist/index.js +256 -4
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -22,6 +22,8 @@
|
|
|
22
22
|
* an emoji and are always removed when the variation-selector pass is on.
|
|
23
23
|
*/
|
|
24
24
|
export type DetectionCategory = "ai-artifact" | "invisible" | "bidi" | "tag" | "variation-selector" | "control" | "space" | "typography"
|
|
25
|
+
/** Citation and entity markers left behind when an AI chat answer is copied (e.g. ChatGPT's oaicite). */
|
|
26
|
+
| "chat-artifact"
|
|
25
27
|
/** Changes made by the NFKC and whitespace stages rather than by a character strip. */
|
|
26
28
|
| "normalization";
|
|
27
29
|
export type DetectionAction = "removed" | "replaced" | "kept";
|
|
@@ -53,10 +55,21 @@ export type CleaningStats = {
|
|
|
53
55
|
/** Hard-wrapped line breaks joined inside paragraphs, plus words rejoined across a line-end hyphen. */
|
|
54
56
|
linesJoined: number;
|
|
55
57
|
};
|
|
58
|
+
/** A message decoded from invisible characters found in the input. */
|
|
59
|
+
export type HiddenMessage = {
|
|
60
|
+
/** "tag": Unicode tag characters (U+E0020–U+E007E) spelling ASCII text. "variation-selector": bytes encoded as variation selectors after a visible character. */
|
|
61
|
+
kind: "tag" | "variation-selector";
|
|
62
|
+
/** The decoded text (truncated to 500 characters). */
|
|
63
|
+
text: string;
|
|
64
|
+
/** Number of invisible characters that carried it. */
|
|
65
|
+
carriers: number;
|
|
66
|
+
};
|
|
56
67
|
export type CleaningResult = {
|
|
57
68
|
cleanedText: string;
|
|
58
69
|
stats: CleaningStats;
|
|
59
70
|
detections: Detection[];
|
|
71
|
+
/** Readable messages decoded from invisible characters in the original text. Empty when none. */
|
|
72
|
+
hiddenMessages: HiddenMessage[];
|
|
60
73
|
};
|
|
61
74
|
export type CleaningOptions = {
|
|
62
75
|
normalizeUnicode?: boolean;
|
|
@@ -88,6 +101,13 @@ export type CleaningOptions = {
|
|
|
88
101
|
* starts with a lowercase letter.
|
|
89
102
|
*/
|
|
90
103
|
joinLines?: boolean;
|
|
104
|
+
/**
|
|
105
|
+
* Remove citation and entity markers that AI chat apps leave in copied text,
|
|
106
|
+
* such as ChatGPT's ":contentReference[oaicite:0]{index=0}", "【4†source】" and
|
|
107
|
+
* the private-use "cite turn0search0" markers. Entity markers keep their
|
|
108
|
+
* display name.
|
|
109
|
+
*/
|
|
110
|
+
removeChatArtifacts?: boolean;
|
|
91
111
|
};
|
|
92
112
|
export declare const DEFAULT_PRESET = "AI Clean";
|
|
93
113
|
export declare const PRESETS: Record<string, CleaningOptions>;
|
|
@@ -95,6 +115,13 @@ export declare const PRESETS: Record<string, CleaningOptions>;
|
|
|
95
115
|
export declare const PRESET_DESCRIPTIONS: Record<string, string>;
|
|
96
116
|
export declare function formatCodepoint(cp: number): string;
|
|
97
117
|
export declare function codepointName(cp: number): string;
|
|
118
|
+
/**
|
|
119
|
+
* Decodes the two known ways of hiding text in invisible characters:
|
|
120
|
+
* runs of Unicode tag characters that spell ASCII (outside real emoji flag
|
|
121
|
+
* sequences), and "emoji smuggling", where each byte of a UTF-8 message is a
|
|
122
|
+
* variation selector (U+FE00–U+FE0F for 0–15, U+E0100–U+E01EF for 16–255).
|
|
123
|
+
*/
|
|
124
|
+
export declare function findHiddenMessages(text: string): HiddenMessage[];
|
|
98
125
|
export declare function cleanText(text: string, options: CleaningOptions): CleaningResult;
|
|
99
126
|
/**
|
|
100
127
|
* Scans text for hidden characters and AI watermark artifacts without
|
package/dist/index.js
CHANGED
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
export const DEFAULT_PRESET = "AI Clean";
|
|
25
25
|
export const PRESETS = {
|
|
26
26
|
"AI Clean": {
|
|
27
|
+
removeChatArtifacts: true,
|
|
27
28
|
normalizeUnicode: true,
|
|
28
29
|
removeInvisibleChars: true,
|
|
29
30
|
removeJoinControls: true,
|
|
@@ -38,6 +39,7 @@ export const PRESETS = {
|
|
|
38
39
|
plainAscii: false,
|
|
39
40
|
},
|
|
40
41
|
"Safe Clean": {
|
|
42
|
+
removeChatArtifacts: true,
|
|
41
43
|
normalizeUnicode: true,
|
|
42
44
|
removeInvisibleChars: true,
|
|
43
45
|
removeJoinControls: true,
|
|
@@ -52,6 +54,7 @@ export const PRESETS = {
|
|
|
52
54
|
plainAscii: false,
|
|
53
55
|
},
|
|
54
56
|
"Keep Typography": {
|
|
57
|
+
removeChatArtifacts: true,
|
|
55
58
|
normalizeUnicode: true,
|
|
56
59
|
removeInvisibleChars: true,
|
|
57
60
|
removeJoinControls: true,
|
|
@@ -66,6 +69,7 @@ export const PRESETS = {
|
|
|
66
69
|
plainAscii: false,
|
|
67
70
|
},
|
|
68
71
|
"Plain ASCII": {
|
|
72
|
+
removeChatArtifacts: true,
|
|
69
73
|
normalizeUnicode: true,
|
|
70
74
|
removeInvisibleChars: true,
|
|
71
75
|
removeJoinControls: true,
|
|
@@ -80,10 +84,12 @@ export const PRESETS = {
|
|
|
80
84
|
plainAscii: true,
|
|
81
85
|
},
|
|
82
86
|
"Markdown to Text": {
|
|
87
|
+
removeChatArtifacts: true,
|
|
83
88
|
stripMarkdown: true,
|
|
84
89
|
normalizeWhitespace: true,
|
|
85
90
|
},
|
|
86
91
|
"Join Lines": {
|
|
92
|
+
removeChatArtifacts: true,
|
|
87
93
|
normalizeUnicode: true,
|
|
88
94
|
removeInvisibleChars: true,
|
|
89
95
|
removeJoinControls: true,
|
|
@@ -163,6 +169,65 @@ function isSupplementaryVariationSelector(cp) {
|
|
|
163
169
|
function isVariationSelector(cp) {
|
|
164
170
|
return (cp >= 0xfe00 && cp <= 0xfe0f) || isSupplementaryVariationSelector(cp);
|
|
165
171
|
}
|
|
172
|
+
const INVISIBLE_INFO = { category: "invisible", disposition: "remove" };
|
|
173
|
+
const FLAG_INFO = { category: "control", disposition: "flag" };
|
|
174
|
+
/**
|
|
175
|
+
* Format characters with a visible or script-specific job, left alone even
|
|
176
|
+
* though Unicode classes them as format (Cf) or default-ignorable: Arabic,
|
|
177
|
+
* Syriac and Kaithi number signs that print above digits, Egyptian
|
|
178
|
+
* hieroglyph and Duployan shorthand format controls.
|
|
179
|
+
*/
|
|
180
|
+
function isScriptFormatChar(cp) {
|
|
181
|
+
return ((cp >= 0x0600 && cp <= 0x0605) ||
|
|
182
|
+
cp === 0x06dd ||
|
|
183
|
+
cp === 0x070f ||
|
|
184
|
+
cp === 0x0890 ||
|
|
185
|
+
cp === 0x0891 ||
|
|
186
|
+
cp === 0x08e2 ||
|
|
187
|
+
cp === 0x110bd ||
|
|
188
|
+
cp === 0x110cd ||
|
|
189
|
+
(cp >= 0x13430 && cp <= 0x1345f) ||
|
|
190
|
+
(cp >= 0x1bca0 && cp <= 0x1bca3));
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Joiners that spell words in some scripts: ZWNJ/ZWJ (Persian, Indic) and the
|
|
194
|
+
* Mongolian free variation selectors. They share the removeJoinControls switch.
|
|
195
|
+
*/
|
|
196
|
+
function isScriptJoinControl(cp) {
|
|
197
|
+
return cp === 0x200c || cp === 0x200d || (cp >= 0x180b && cp <= 0x180d) || cp === 0x180f;
|
|
198
|
+
}
|
|
199
|
+
const IGNORABLE = /[\p{Default_Ignorable_Code_Point}\p{Cf}]/u;
|
|
200
|
+
const PRIVATE_USE = /\p{Co}/u;
|
|
201
|
+
const NONCHARACTER = /\p{Noncharacter_Code_Point}/u;
|
|
202
|
+
const PROPERTY_CACHE = new Map();
|
|
203
|
+
/**
|
|
204
|
+
* Everything the hand-written tables above do not name, classified from the
|
|
205
|
+
* Unicode properties built into the JavaScript engine, so coverage follows
|
|
206
|
+
* the Unicode version of the browser or Node.js running it: every
|
|
207
|
+
* default-ignorable or format character becomes "invisible", private-use
|
|
208
|
+
* characters and noncharacters become "control", and U+FFFD is flagged.
|
|
209
|
+
*/
|
|
210
|
+
function classifyByProperty(cp) {
|
|
211
|
+
if (cp < 0xad)
|
|
212
|
+
return null;
|
|
213
|
+
const cached = PROPERTY_CACHE.get(cp);
|
|
214
|
+
if (cached !== undefined)
|
|
215
|
+
return cached;
|
|
216
|
+
const ch = String.fromCodePoint(cp);
|
|
217
|
+
let info = null;
|
|
218
|
+
if (cp === 0x2800)
|
|
219
|
+
info = { category: "invisible", disposition: "space" };
|
|
220
|
+
else if (cp === 0xfffd)
|
|
221
|
+
info = FLAG_INFO;
|
|
222
|
+
else if (isScriptFormatChar(cp))
|
|
223
|
+
info = null;
|
|
224
|
+
else if (IGNORABLE.test(ch))
|
|
225
|
+
info = INVISIBLE_INFO;
|
|
226
|
+
else if (PRIVATE_USE.test(ch) || NONCHARACTER.test(ch))
|
|
227
|
+
info = CONTROL_INFO;
|
|
228
|
+
PROPERTY_CACHE.set(cp, info);
|
|
229
|
+
return info;
|
|
230
|
+
}
|
|
166
231
|
function classify(cp) {
|
|
167
232
|
const single = CHAR_INFO.get(cp);
|
|
168
233
|
if (single)
|
|
@@ -176,7 +241,7 @@ function classify(cp) {
|
|
|
176
241
|
(cp >= 0x80 && cp <= 0x9f)) {
|
|
177
242
|
return CONTROL_INFO;
|
|
178
243
|
}
|
|
179
|
-
return
|
|
244
|
+
return classifyByProperty(cp);
|
|
180
245
|
}
|
|
181
246
|
// Typography characters — counted only when typography normalization is on.
|
|
182
247
|
const TYPOGRAPHY_CPS = new Set([
|
|
@@ -207,7 +272,11 @@ const SINGLE_NAMES = {
|
|
|
207
272
|
0x1160: "Hangul jungseong filler",
|
|
208
273
|
0x17b4: "Khmer vowel inherent aq",
|
|
209
274
|
0x17b5: "Khmer vowel inherent aa",
|
|
275
|
+
0x180b: "Mongolian free variation selector one",
|
|
276
|
+
0x180c: "Mongolian free variation selector two",
|
|
277
|
+
0x180d: "Mongolian free variation selector three",
|
|
210
278
|
0x180e: "Mongolian vowel separator",
|
|
279
|
+
0x180f: "Mongolian free variation selector four",
|
|
211
280
|
0x2000: "En quad",
|
|
212
281
|
0x2001: "Em quad",
|
|
213
282
|
0x2002: "En space",
|
|
@@ -241,6 +310,17 @@ const SINGLE_NAMES = {
|
|
|
241
310
|
0x202f: "Narrow no-break space",
|
|
242
311
|
0x205f: "Medium mathematical space",
|
|
243
312
|
0x2060: "Word joiner",
|
|
313
|
+
0x2061: "Function application (invisible)",
|
|
314
|
+
0x2062: "Invisible times",
|
|
315
|
+
0x2063: "Invisible separator",
|
|
316
|
+
0x2064: "Invisible plus",
|
|
317
|
+
0x206a: "Inhibit symmetric swapping (deprecated)",
|
|
318
|
+
0x206b: "Activate symmetric swapping (deprecated)",
|
|
319
|
+
0x206c: "Inhibit Arabic form shaping (deprecated)",
|
|
320
|
+
0x206d: "Activate Arabic form shaping (deprecated)",
|
|
321
|
+
0x206e: "National digit shapes (deprecated)",
|
|
322
|
+
0x206f: "Nominal digit shapes (deprecated)",
|
|
323
|
+
0x2800: "Braille pattern blank",
|
|
244
324
|
0x2066: "Left-to-right isolate",
|
|
245
325
|
0x2067: "Right-to-left isolate",
|
|
246
326
|
0x2068: "First strong isolate",
|
|
@@ -255,6 +335,15 @@ const SINGLE_NAMES = {
|
|
|
255
335
|
0xfffa: "Interlinear annotation separator",
|
|
256
336
|
0xfffb: "Interlinear annotation terminator",
|
|
257
337
|
0xfffc: "Object replacement character",
|
|
338
|
+
0xfffd: "Replacement character (�)",
|
|
339
|
+
0x1d173: "Musical symbol begin beam (invisible)",
|
|
340
|
+
0x1d174: "Musical symbol end beam (invisible)",
|
|
341
|
+
0x1d175: "Musical symbol begin tie (invisible)",
|
|
342
|
+
0x1d176: "Musical symbol end tie (invisible)",
|
|
343
|
+
0x1d177: "Musical symbol begin slur (invisible)",
|
|
344
|
+
0x1d178: "Musical symbol end slur (invisible)",
|
|
345
|
+
0x1d179: "Musical symbol begin phrase (invisible)",
|
|
346
|
+
0x1d17a: "Musical symbol end phrase (invisible)",
|
|
258
347
|
0xe0001: "Language tag",
|
|
259
348
|
0xe0020: "Tag space",
|
|
260
349
|
0xe007f: "Cancel tag",
|
|
@@ -276,6 +365,13 @@ export function codepointName(cp) {
|
|
|
276
365
|
return "Control character (C0)";
|
|
277
366
|
if (cp >= 0x80 && cp <= 0x9f)
|
|
278
367
|
return "Control character (C1)";
|
|
368
|
+
const ch = String.fromCodePoint(cp);
|
|
369
|
+
if (PRIVATE_USE.test(ch))
|
|
370
|
+
return "Private-use character";
|
|
371
|
+
if (NONCHARACTER.test(ch))
|
|
372
|
+
return "Noncharacter";
|
|
373
|
+
if (IGNORABLE.test(ch))
|
|
374
|
+
return "Invisible format character";
|
|
279
375
|
return `Character ${formatCodepoint(cp)}`;
|
|
280
376
|
}
|
|
281
377
|
const NOTES = {
|
|
@@ -290,6 +386,9 @@ const NOTES = {
|
|
|
290
386
|
0x202e: "Reorders displayed text; used in spoofing attacks",
|
|
291
387
|
0x2014: "Frequently cited as an AI typography tell",
|
|
292
388
|
0x180e: "Renders invisibly in modern fonts",
|
|
389
|
+
0x2800: "Looks like a space; a popular invisible filler outside Braille text",
|
|
390
|
+
0xfffd: "The original character was lost in an encoding error and can't be recovered; check the source",
|
|
391
|
+
0x2062: "Invisible math operator; no visible glyph in prose",
|
|
293
392
|
};
|
|
294
393
|
function codepointNote(cp) {
|
|
295
394
|
const note = NOTES[cp];
|
|
@@ -302,6 +401,9 @@ function codepointNote(cp) {
|
|
|
302
401
|
}
|
|
303
402
|
if (isVariationSelector(cp))
|
|
304
403
|
return "Invisible; can encode hidden data after ordinary characters";
|
|
404
|
+
const ch = String.fromCodePoint(cp);
|
|
405
|
+
if (PRIVATE_USE.test(ch))
|
|
406
|
+
return "Only displays with the font that defined it; shows as a box elsewhere";
|
|
305
407
|
return undefined;
|
|
306
408
|
}
|
|
307
409
|
const EMOJI_KEPT_NOTE = "Part of an emoji sequence — kept so your emoji don't break";
|
|
@@ -461,7 +563,7 @@ function isCategoryEnabled(category, cp, o) {
|
|
|
461
563
|
case "invisible":
|
|
462
564
|
// ZWNJ/ZWJ carry meaning in Persian and Indic scripts, so they get their
|
|
463
565
|
// own switch; it falls back to removeInvisibleChars for older callers.
|
|
464
|
-
if (cp
|
|
566
|
+
if (isScriptJoinControl(cp)) {
|
|
465
567
|
return (o.removeJoinControls ?? !!o.removeInvisibleChars) || ascii;
|
|
466
568
|
}
|
|
467
569
|
return !!o.removeInvisibleChars || ascii;
|
|
@@ -537,6 +639,11 @@ function runCharacterPass(text, o) {
|
|
|
537
639
|
outParts.push(ch);
|
|
538
640
|
continue;
|
|
539
641
|
}
|
|
642
|
+
if (info.disposition === "flag") {
|
|
643
|
+
record(cp, info.category, "kept", false);
|
|
644
|
+
outParts.push(ch);
|
|
645
|
+
continue;
|
|
646
|
+
}
|
|
540
647
|
if (info.disposition === "remove") {
|
|
541
648
|
record(cp, info.category, "removed", false);
|
|
542
649
|
}
|
|
@@ -873,12 +980,155 @@ function plainAsciiStage(text) {
|
|
|
873
980
|
.replace(/\p{M}/gu, "")
|
|
874
981
|
.replace(/[^\t\n\x20-\x7E]/g, "");
|
|
875
982
|
}
|
|
983
|
+
/**
|
|
984
|
+
* Pull the display name out of a ChatGPT entity marker body such as
|
|
985
|
+
* ["city","Paris","capital of France"] — the second string is what the
|
|
986
|
+
* answer showed as a link.
|
|
987
|
+
*/
|
|
988
|
+
function entityDisplayName(body) {
|
|
989
|
+
const strings = body.match(/"((?:[^"\\]|\\.)*)"/g);
|
|
990
|
+
if (!strings || strings.length === 0)
|
|
991
|
+
return "";
|
|
992
|
+
const pick = strings.length > 1 ? strings[1] : strings[0];
|
|
993
|
+
return pick.slice(1, -1).replace(/\\(.)/g, "$1");
|
|
994
|
+
}
|
|
995
|
+
const CHAT_ARTIFACT_RULES = [
|
|
996
|
+
{
|
|
997
|
+
// U+E200 <kind> U+E202 <payload> U+E201 — ChatGPT's private-use citation,
|
|
998
|
+
// entity and widget markers. Entities keep their visible name.
|
|
999
|
+
label: "U+E200…U+E201",
|
|
1000
|
+
name: "ChatGPT citation or entity marker",
|
|
1001
|
+
note: "Private-use markers ChatGPT uses for sources and links; they show as boxes or text like \"cite turn0search0\" when pasted",
|
|
1002
|
+
pattern: /\uE200([a-z_]+)\uE202([\s\S]*?)\uE201/g,
|
|
1003
|
+
replace: (_m, kind, payload) => (kind === "entity" ? entityDisplayName(payload) : ""),
|
|
1004
|
+
},
|
|
1005
|
+
{
|
|
1006
|
+
label: "oaicite",
|
|
1007
|
+
name: "ChatGPT content reference (oaicite)",
|
|
1008
|
+
note: "Citation placeholder from ChatGPT answers, e.g. :contentReference[oaicite:0]{index=0}",
|
|
1009
|
+
pattern: /[ \t]?:?contentReference\[oaicite:\d+\](?:\{index=\d+\})?/g,
|
|
1010
|
+
replace: () => "",
|
|
1011
|
+
},
|
|
1012
|
+
{
|
|
1013
|
+
label: "【†】",
|
|
1014
|
+
name: "ChatGPT source marker",
|
|
1015
|
+
note: "Citation marker from ChatGPT file and browsing answers, e.g. 【4†source】",
|
|
1016
|
+
pattern: /[ \t]?【\d+(?::\d+)?†[^】\n]{0,40}】/g,
|
|
1017
|
+
replace: () => "",
|
|
1018
|
+
},
|
|
1019
|
+
{
|
|
1020
|
+
// The same citations after the private-use delimiters were stripped by
|
|
1021
|
+
// another app: "citeturn0search0turn0news2".
|
|
1022
|
+
label: "citeturn",
|
|
1023
|
+
name: "ChatGPT citation marker (flattened)",
|
|
1024
|
+
note: "What ChatGPT's citation marker becomes when the invisible delimiters are lost",
|
|
1025
|
+
pattern: /[ \t]?\b(?:file)?cite(?:turn\d+[a-z]+\d+)+/g,
|
|
1026
|
+
replace: () => "",
|
|
1027
|
+
},
|
|
1028
|
+
{
|
|
1029
|
+
label: "entity[…]",
|
|
1030
|
+
name: "ChatGPT entity marker (flattened)",
|
|
1031
|
+
note: "A linked name from a ChatGPT answer that lost its invisible delimiters; the name is kept",
|
|
1032
|
+
pattern: /\bentity(\["[^\]\n]{1,300}\])/g,
|
|
1033
|
+
replace: (_m, body) => entityDisplayName(body),
|
|
1034
|
+
},
|
|
1035
|
+
];
|
|
1036
|
+
function stripChatArtifactsStage(text) {
|
|
1037
|
+
const rows = [];
|
|
1038
|
+
let out = text;
|
|
1039
|
+
for (const rule of CHAT_ARTIFACT_RULES) {
|
|
1040
|
+
let count = 0;
|
|
1041
|
+
out = out.replace(rule.pattern, (...args) => {
|
|
1042
|
+
count++;
|
|
1043
|
+
const groups = args.slice(1, -2);
|
|
1044
|
+
return rule.replace(args[0], ...groups);
|
|
1045
|
+
});
|
|
1046
|
+
if (count > 0) {
|
|
1047
|
+
rows.push({ codepoint: rule.label, name: rule.name, count, category: "chat-artifact", action: "removed", note: rule.note });
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
return { out, rows };
|
|
1051
|
+
}
|
|
876
1052
|
/* ------------------------------------------------------------------ */
|
|
877
|
-
/*
|
|
1053
|
+
/* Hidden message decoding */
|
|
878
1054
|
/* ------------------------------------------------------------------ */
|
|
1055
|
+
const HIDDEN_MESSAGE_LIMIT = 500;
|
|
1056
|
+
function utf8Decode(bytes) {
|
|
1057
|
+
try {
|
|
1058
|
+
return decodeURIComponent(bytes.map((b) => "%" + b.toString(16).padStart(2, "0")).join(""));
|
|
1059
|
+
}
|
|
1060
|
+
catch {
|
|
1061
|
+
return null;
|
|
1062
|
+
}
|
|
1063
|
+
}
|
|
1064
|
+
function isReadable(text) {
|
|
1065
|
+
if (text.trim().length === 0)
|
|
1066
|
+
return false;
|
|
1067
|
+
const printable = Array.from(text).filter((c) => /[\p{L}\p{N}\p{P}\p{S}\p{Zs}\n\t]/u.test(c)).length;
|
|
1068
|
+
return printable / Array.from(text).length >= 0.9;
|
|
1069
|
+
}
|
|
1070
|
+
/**
|
|
1071
|
+
* Decodes the two known ways of hiding text in invisible characters:
|
|
1072
|
+
* runs of Unicode tag characters that spell ASCII (outside real emoji flag
|
|
1073
|
+
* sequences), and "emoji smuggling", where each byte of a UTF-8 message is a
|
|
1074
|
+
* variation selector (U+FE00–U+FE0F for 0–15, U+E0100–U+E01EF for 16–255).
|
|
1075
|
+
*/
|
|
1076
|
+
export function findHiddenMessages(text) {
|
|
1077
|
+
const cps = Array.from(text, (c) => c.codePointAt(0));
|
|
1078
|
+
const found = [];
|
|
1079
|
+
let i = 0;
|
|
1080
|
+
while (i < cps.length) {
|
|
1081
|
+
const cp = cps[i];
|
|
1082
|
+
if (cp >= 0xe0020 && cp <= 0xe007e) {
|
|
1083
|
+
let j = i;
|
|
1084
|
+
while (j < cps.length && cps[j] >= 0xe0020 && cps[j] <= 0xe007e)
|
|
1085
|
+
j++;
|
|
1086
|
+
// A subdivision flag is U+1F3F4, lowercase tag letters, then CANCEL TAG.
|
|
1087
|
+
const isFlag = i > 0 && cps[i - 1] === BLACK_FLAG && cps[j] === CANCEL_TAG;
|
|
1088
|
+
if (!isFlag) {
|
|
1089
|
+
const decoded = String.fromCharCode(...cps.slice(i, j).map((c) => c - 0xe0000));
|
|
1090
|
+
if (isReadable(decoded)) {
|
|
1091
|
+
found.push({ kind: "tag", text: decoded.slice(0, HIDDEN_MESSAGE_LIMIT), carriers: j - i });
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
i = j;
|
|
1095
|
+
continue;
|
|
1096
|
+
}
|
|
1097
|
+
if (isVariationSelector(cp)) {
|
|
1098
|
+
let j = i;
|
|
1099
|
+
const bytes = [];
|
|
1100
|
+
while (j < cps.length && isVariationSelector(cps[j])) {
|
|
1101
|
+
const v = cps[j];
|
|
1102
|
+
bytes.push(v <= 0xfe0f ? v - 0xfe00 : v - 0xe0100 + 16);
|
|
1103
|
+
j++;
|
|
1104
|
+
}
|
|
1105
|
+
// One selector is ordinary emoji styling; a message needs at least two bytes.
|
|
1106
|
+
if (j - i >= 2) {
|
|
1107
|
+
const decoded = utf8Decode(bytes);
|
|
1108
|
+
if (decoded !== null && isReadable(decoded)) {
|
|
1109
|
+
found.push({ kind: "variation-selector", text: decoded.slice(0, HIDDEN_MESSAGE_LIMIT), carriers: j - i });
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
i = j;
|
|
1113
|
+
continue;
|
|
1114
|
+
}
|
|
1115
|
+
i++;
|
|
1116
|
+
}
|
|
1117
|
+
return found;
|
|
1118
|
+
}
|
|
879
1119
|
export function cleanText(text, options) {
|
|
880
1120
|
const o = options ?? {};
|
|
881
1121
|
const normalizationRows = [];
|
|
1122
|
+
const hiddenMessages = findHiddenMessages(text);
|
|
1123
|
+
// Stage 0 — AI chat citation markers. Runs before the character pass,
|
|
1124
|
+
// which would otherwise delete their private-use delimiters and leave the
|
|
1125
|
+
// visible "citeturn0search0" text behind.
|
|
1126
|
+
const artifactRows = [];
|
|
1127
|
+
if (o.removeChatArtifacts) {
|
|
1128
|
+
const artifacts = stripChatArtifactsStage(text);
|
|
1129
|
+
text = artifacts.out;
|
|
1130
|
+
artifactRows.push(...artifacts.rows);
|
|
1131
|
+
}
|
|
882
1132
|
// A soft hyphen at a line end marks a word the PDF split across two lines.
|
|
883
1133
|
// Stage 1 would delete it and leave "hyph" + newline + "enation", so when
|
|
884
1134
|
// lines are being joined, rejoin those words first.
|
|
@@ -1018,6 +1268,7 @@ export function cleanText(text, options) {
|
|
|
1018
1268
|
note: codepointNote(cp),
|
|
1019
1269
|
});
|
|
1020
1270
|
}
|
|
1271
|
+
detections.push(...artifactRows);
|
|
1021
1272
|
detections.push(...normalizationRows);
|
|
1022
1273
|
// Most frequent first; on a tie, character findings outrank formatting ones.
|
|
1023
1274
|
const rank = (d) => (d.category === "normalization" ? 1 : 0);
|
|
@@ -1053,6 +1304,7 @@ export function cleanText(text, options) {
|
|
|
1053
1304
|
stats.quotesDashesNormalized += d.count;
|
|
1054
1305
|
break;
|
|
1055
1306
|
case "ai-artifact":
|
|
1307
|
+
case "chat-artifact":
|
|
1056
1308
|
case "tag":
|
|
1057
1309
|
case "variation-selector":
|
|
1058
1310
|
stats.aiArtifactsRemoved += d.count;
|
|
@@ -1066,7 +1318,7 @@ export function cleanText(text, options) {
|
|
|
1066
1318
|
break;
|
|
1067
1319
|
}
|
|
1068
1320
|
}
|
|
1069
|
-
return { cleanedText: cleaned, stats, detections };
|
|
1321
|
+
return { cleanedText: cleaned, stats, detections, hiddenMessages };
|
|
1070
1322
|
}
|
|
1071
1323
|
/**
|
|
1072
1324
|
* Scans text for hidden characters and AI watermark artifacts without
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cleanpastedtext",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Detect and remove hidden Unicode (zero-width spaces, BiDi controls, tag characters), odd spaces and AI-style typography from text. Deterministic, dependency-free, runs locally.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|