@open-mena-mcp/arabic-tools 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +152 -2
- package/dist/bin.d.ts +8 -0
- package/dist/bin.js +24 -0
- package/dist/bin.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/server.d.ts +116 -0
- package/dist/server.js +188 -0
- package/dist/server.js.map +1 -0
- package/dist/text/diacritics.d.ts +30 -0
- package/dist/text/diacritics.js +60 -0
- package/dist/text/diacritics.js.map +1 -0
- package/dist/text/hijri.d.ts +57 -0
- package/dist/text/hijri.js +270 -0
- package/dist/text/hijri.js.map +1 -0
- package/dist/text/normalize.d.ts +49 -0
- package/dist/text/normalize.js +148 -0
- package/dist/text/normalize.js.map +1 -0
- package/dist/text/numerals.d.ts +30 -0
- package/dist/text/numerals.js +106 -0
- package/dist/text/numerals.js.map +1 -0
- package/dist/text/rtl.d.ts +58 -0
- package/dist/text/rtl.js +210 -0
- package/dist/text/rtl.js.map +1 -0
- package/dist/toolkit.d.ts +14 -0
- package/dist/toolkit.js +19 -0
- package/dist/toolkit.js.map +1 -0
- package/package.json +52 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Dinaelsayed99
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,153 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @open-mena-mcp/arabic-tools
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Zero-auth MCP server for Arabic text: normalization, diacritics, numeral systems, Hijri dates and RTL cleanup.
|
|
4
|
+
|
|
5
|
+
Part of the [open-mena-mcp](https://github.com/Dinaelsayed99/open-mena-mcp) monorepo. Every tool runs locally: no API key, no network call, no rate limit.
|
|
6
|
+
|
|
7
|
+
## Why this server exists
|
|
8
|
+
|
|
9
|
+
Arabic text arriving from forms, scraped pages, PDFs and chat exports is messy. The same word shows up with different alef and yaa forms, diacritics are inconsistent, digits come in three numeral systems, and invisible bidi characters break rendering. An LLM asked to "clean up this Arabic text" gets these details wrong in small, hard-to-notice ways. These tools do it deterministically.
|
|
10
|
+
|
|
11
|
+
## Tools
|
|
12
|
+
|
|
13
|
+
| Tool | What it does |
|
|
14
|
+
| ------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
15
|
+
| `normalize_arabic` | Folds letter variants (آ أ إ ٱ → ا, ى → ي, ة → ه, ؤ/ئ → و/ي), strips tashkeel and tatweel, decomposes presentation forms, collapses whitespace. Returns the text plus a count of every change. |
|
|
16
|
+
| `strip_diacritics` | Removes harakat, tanween, shadda, sukun, superscript alef and Quranic annotation signs. Shadda and sukun can be kept. |
|
|
17
|
+
| `convert_numerals` | Converts digits between Western (0-9), Eastern Arabic-Indic (٠-٩) and Persian (۰-۹), including the Arabic decimal and thousands separators. `from: "auto"` handles mixed text. |
|
|
18
|
+
| `convert_hijri_gregorian` | Converts dates both ways with the Umm al-Qura calendar, offline. Returns both dates, Arabic and English month and weekday names, and pre-formatted Hijri strings. |
|
|
19
|
+
| `clean_rtl_text` | Removes bidi control and zero-width characters, fixes spacing before and after Arabic punctuation, collapses spaces, optionally wraps the result in RLI/PDI isolation marks, and reports the Arabic/Latin letter mix. |
|
|
20
|
+
|
|
21
|
+
Every input field carries a `.describe()` so MCP clients can show users what each parameter means.
|
|
22
|
+
|
|
23
|
+
## Examples
|
|
24
|
+
|
|
25
|
+
Normalize a name for matching:
|
|
26
|
+
|
|
27
|
+
```json
|
|
28
|
+
{ "text": "أحمد" }
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
```json
|
|
32
|
+
{
|
|
33
|
+
"text": "احمد",
|
|
34
|
+
"removedDiacritics": 0,
|
|
35
|
+
"replacedLetters": 1,
|
|
36
|
+
"replacedPresentationForms": 0,
|
|
37
|
+
"removedTatweel": 0,
|
|
38
|
+
"removedControlChars": 0,
|
|
39
|
+
"inputLength": 4,
|
|
40
|
+
"outputLength": 4
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Convert a date to Hijri:
|
|
45
|
+
|
|
46
|
+
```json
|
|
47
|
+
{ "date": "2026-12-05", "direction": "gregorian_to_hijri" }
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
```json
|
|
51
|
+
{
|
|
52
|
+
"direction": "gregorian_to_hijri",
|
|
53
|
+
"calendar": "islamic-umalqura",
|
|
54
|
+
"offline": true,
|
|
55
|
+
"gregorian": {
|
|
56
|
+
"year": 2026,
|
|
57
|
+
"month": 12,
|
|
58
|
+
"day": 5,
|
|
59
|
+
"iso": "2026-12-05",
|
|
60
|
+
"monthName": "December / ديسمبر",
|
|
61
|
+
"weekday": "Saturday / السبت"
|
|
62
|
+
},
|
|
63
|
+
"hijri": {
|
|
64
|
+
"year": 1448,
|
|
65
|
+
"month": 6,
|
|
66
|
+
"day": 25,
|
|
67
|
+
"iso": "1448-06-25",
|
|
68
|
+
"monthName": "Jumada al-Akhirah / جمادى الآخرة",
|
|
69
|
+
"weekday": "Saturday / السبت",
|
|
70
|
+
"formattedAr": "٢٥ جمادى الآخرة ١٤٤٨ هـ",
|
|
71
|
+
"formattedEn": "25 Jumada al-Akhirah 1448 AH"
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Fix punctuation spacing in a chat message:
|
|
77
|
+
|
|
78
|
+
```json
|
|
79
|
+
{ "text": "مرحبا،كيف حالك؟" }
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
```json
|
|
83
|
+
{
|
|
84
|
+
"text": "مرحبا، كيف حالك؟",
|
|
85
|
+
"removedInvisible": 0,
|
|
86
|
+
"removedControlChars": 0,
|
|
87
|
+
"fixedPunctuation": 1,
|
|
88
|
+
"arabicLetters": 12,
|
|
89
|
+
"latinLetters": 0,
|
|
90
|
+
"arabicRatio": 1,
|
|
91
|
+
"direction": "rtl",
|
|
92
|
+
"mixedDirection": false,
|
|
93
|
+
"hadBidiControls": false
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## Run it
|
|
98
|
+
|
|
99
|
+
### Claude Desktop
|
|
100
|
+
|
|
101
|
+
Add this to `claude_desktop_config.json` (macOS: `~/Library/Application Support/Claude/claude_desktop_config.json`, Windows: `%APPDATA%\Claude\claude_desktop_config.json`), then restart Claude Desktop:
|
|
102
|
+
|
|
103
|
+
```json
|
|
104
|
+
{
|
|
105
|
+
"mcpServers": {
|
|
106
|
+
"arabic-tools": {
|
|
107
|
+
"command": "npx",
|
|
108
|
+
"args": ["-y", "@open-mena-mcp/arabic-tools"]
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### Claude Code
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
claude mcp add arabic-tools -- npx -y @open-mena-mcp/arabic-tools
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### From this monorepo
|
|
121
|
+
|
|
122
|
+
```bash
|
|
123
|
+
pnpm install
|
|
124
|
+
pnpm --filter @open-mena-mcp/arabic-tools build
|
|
125
|
+
node packages/mcp-arabic-tools/dist/bin.js
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
Point a client at that path with the same `mcpServers` block, using `"command": "node"` and `"args": ["/absolute/path/to/packages/mcp-arabic-tools/dist/bin.js"]`.
|
|
129
|
+
|
|
130
|
+
## Develop
|
|
131
|
+
|
|
132
|
+
```bash
|
|
133
|
+
pnpm --filter @open-mena-mcp/arabic-tools build
|
|
134
|
+
pnpm --filter @open-mena-mcp/arabic-tools typecheck
|
|
135
|
+
pnpm --filter @open-mena-mcp/arabic-tools lint
|
|
136
|
+
pnpm --filter @open-mena-mcp/arabic-tools test
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
The test suite has 58 tests: unit tests for each text module, an MCP protocol handshake over the SDK's in-memory transport, and a real stdio handshake that spawns `dist/bin.js` when a build is present. No test makes a network call.
|
|
140
|
+
|
|
141
|
+
`LOG_LEVEL=debug` turns on debug logging. Logs go to stderr so stdout stays clean for the stdio transport.
|
|
142
|
+
|
|
143
|
+
## Notes and limits
|
|
144
|
+
|
|
145
|
+
- **Umm al-Qura range.** The Hijri calendar is tabulated for 1300-1600 AH. Dates outside that range are rejected with an `INVALID_INPUT` error instead of being approximated, so a caller never gets a plausible but wrong day.
|
|
146
|
+
- **ICU data.** Hijri conversion uses `Intl.DateTimeFormat` with `calendar: "islamic-umalqura"`. Node 20 and later ship full ICU, so no extra data is needed.
|
|
147
|
+
- **Zero-width joiners.** Persian uses ZWNJ (U+200C) as a real letter separator. `clean_rtl_text` removes it by default and keeps it with `keep_joiners: true`; `normalize_arabic` leaves it alone.
|
|
148
|
+
- **Digits are not Arabic letters.** `arabicRatio` and `direction` count letters only, so a numeric string reports a neutral direction.
|
|
149
|
+
- **Errors** come back as MCP error results with a code (`INVALID_INPUT`, `INTERNAL`) rather than a thrown exception, so a client can show the message and retry.
|
|
150
|
+
|
|
151
|
+
## License
|
|
152
|
+
|
|
153
|
+
MIT
|
package/dist/bin.d.ts
ADDED
package/dist/bin.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* stdio entrypoint for @open-mena-mcp/arabic-tools.
|
|
4
|
+
*
|
|
5
|
+
* Nothing may be written to stdout except MCP protocol frames, so all logging goes
|
|
6
|
+
* to stderr through the core structured logger.
|
|
7
|
+
*/
|
|
8
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
9
|
+
import { createLogger } from "@open-mena-mcp/core";
|
|
10
|
+
import { SERVER_NAME, SERVER_VERSION, createServer } from "./server.js";
|
|
11
|
+
async function main() {
|
|
12
|
+
const level = process.env.LOG_LEVEL === "debug" ? "debug" : "info";
|
|
13
|
+
const logger = createLogger({ name: "arabic-tools", level });
|
|
14
|
+
const server = createServer();
|
|
15
|
+
const transport = new StdioServerTransport();
|
|
16
|
+
await server.connect(transport);
|
|
17
|
+
logger.info("MCP server running on stdio", { server: SERVER_NAME, version: SERVER_VERSION });
|
|
18
|
+
}
|
|
19
|
+
main().catch((error) => {
|
|
20
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
21
|
+
process.stderr.write(`Failed to start ${SERVER_NAME}: ${message}\n`);
|
|
22
|
+
process.exit(1);
|
|
23
|
+
});
|
|
24
|
+
//# sourceMappingURL=bin.js.map
|
package/dist/bin.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bin.js","sourceRoot":"","sources":["../src/bin.ts"],"names":[],"mappings":";AACA;;;;;GAKG;AAEH,OAAO,EAAE,oBAAoB,EAAE,MAAM,2CAA2C,CAAC;AACjF,OAAO,EAAE,YAAY,EAAiB,MAAM,qBAAqB,CAAC;AAElE,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAExE,KAAK,UAAU,IAAI;IACjB,MAAM,KAAK,GAAa,OAAO,CAAC,GAAG,CAAC,SAAS,KAAK,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC;IAC7E,MAAM,MAAM,GAAG,YAAY,CAAC,EAAE,IAAI,EAAE,cAAc,EAAE,KAAK,EAAE,CAAC,CAAC;IAE7D,MAAM,MAAM,GAAG,YAAY,EAAE,CAAC;IAC9B,MAAM,SAAS,GAAG,IAAI,oBAAoB,EAAE,CAAC;IAC7C,MAAM,MAAM,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IAEhC,MAAM,CAAC,IAAI,CAAC,6BAA6B,EAAE,EAAE,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,cAAc,EAAE,CAAC,CAAC;AAC/F,CAAC;AAED,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,KAAc,EAAE,EAAE;IAC9B,MAAM,OAAO,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IACvE,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,mBAAmB,WAAW,KAAK,OAAO,IAAI,CAAC,CAAC;IACrE,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC,CAAC,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./server.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,8DAA8D;AAC9D,kFAAkF;AAClF,cAAc,aAAa,CAAC"}
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool registration for @open-mena-mcp/arabic-tools.
|
|
3
|
+
*
|
|
4
|
+
* Five zero-auth, offline tools. Nothing here makes a network call: the Arabic text
|
|
5
|
+
* rules are local and the Hijri calendar comes from the runtime's ICU data.
|
|
6
|
+
*/
|
|
7
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
8
|
+
import { z } from "zod";
|
|
9
|
+
export declare const SERVER_NAME = "@open-mena-mcp/arabic-tools";
|
|
10
|
+
export declare const SERVER_VERSION = "0.1.0";
|
|
11
|
+
export declare const SERVER_INSTRUCTIONS: string;
|
|
12
|
+
export declare const normalizeArabicSchema: z.ZodObject<{
|
|
13
|
+
text: z.ZodString;
|
|
14
|
+
strip_diacritics: z.ZodOptional<z.ZodBoolean>;
|
|
15
|
+
unify_alef: z.ZodOptional<z.ZodBoolean>;
|
|
16
|
+
unify_yaa: z.ZodOptional<z.ZodBoolean>;
|
|
17
|
+
unify_hamza: z.ZodOptional<z.ZodBoolean>;
|
|
18
|
+
unify_taa_marbuta: z.ZodOptional<z.ZodBoolean>;
|
|
19
|
+
remove_tatweel: z.ZodOptional<z.ZodBoolean>;
|
|
20
|
+
decompose_presentation_forms: z.ZodOptional<z.ZodBoolean>;
|
|
21
|
+
remove_control_chars: z.ZodOptional<z.ZodBoolean>;
|
|
22
|
+
collapse_whitespace: z.ZodOptional<z.ZodBoolean>;
|
|
23
|
+
}, "strip", z.ZodTypeAny, {
|
|
24
|
+
text: string;
|
|
25
|
+
strip_diacritics?: boolean | undefined;
|
|
26
|
+
unify_alef?: boolean | undefined;
|
|
27
|
+
unify_yaa?: boolean | undefined;
|
|
28
|
+
unify_hamza?: boolean | undefined;
|
|
29
|
+
unify_taa_marbuta?: boolean | undefined;
|
|
30
|
+
remove_tatweel?: boolean | undefined;
|
|
31
|
+
decompose_presentation_forms?: boolean | undefined;
|
|
32
|
+
remove_control_chars?: boolean | undefined;
|
|
33
|
+
collapse_whitespace?: boolean | undefined;
|
|
34
|
+
}, {
|
|
35
|
+
text: string;
|
|
36
|
+
strip_diacritics?: boolean | undefined;
|
|
37
|
+
unify_alef?: boolean | undefined;
|
|
38
|
+
unify_yaa?: boolean | undefined;
|
|
39
|
+
unify_hamza?: boolean | undefined;
|
|
40
|
+
unify_taa_marbuta?: boolean | undefined;
|
|
41
|
+
remove_tatweel?: boolean | undefined;
|
|
42
|
+
decompose_presentation_forms?: boolean | undefined;
|
|
43
|
+
remove_control_chars?: boolean | undefined;
|
|
44
|
+
collapse_whitespace?: boolean | undefined;
|
|
45
|
+
}>;
|
|
46
|
+
export declare const stripDiacriticsSchema: z.ZodObject<{
|
|
47
|
+
text: z.ZodString;
|
|
48
|
+
keep_shadda: z.ZodOptional<z.ZodBoolean>;
|
|
49
|
+
keep_sukun: z.ZodOptional<z.ZodBoolean>;
|
|
50
|
+
remove_tatweel: z.ZodOptional<z.ZodBoolean>;
|
|
51
|
+
remove_quranic_marks: z.ZodOptional<z.ZodBoolean>;
|
|
52
|
+
}, "strip", z.ZodTypeAny, {
|
|
53
|
+
text: string;
|
|
54
|
+
remove_tatweel?: boolean | undefined;
|
|
55
|
+
keep_shadda?: boolean | undefined;
|
|
56
|
+
keep_sukun?: boolean | undefined;
|
|
57
|
+
remove_quranic_marks?: boolean | undefined;
|
|
58
|
+
}, {
|
|
59
|
+
text: string;
|
|
60
|
+
remove_tatweel?: boolean | undefined;
|
|
61
|
+
keep_shadda?: boolean | undefined;
|
|
62
|
+
keep_sukun?: boolean | undefined;
|
|
63
|
+
remove_quranic_marks?: boolean | undefined;
|
|
64
|
+
}>;
|
|
65
|
+
export declare const convertNumeralsSchema: z.ZodObject<{
|
|
66
|
+
text: z.ZodString;
|
|
67
|
+
from: z.ZodOptional<z.ZodUnion<[z.ZodEnum<["western", "eastern", "persian"]>, z.ZodLiteral<"auto">]>>;
|
|
68
|
+
to: z.ZodEnum<["western", "eastern", "persian"]>;
|
|
69
|
+
convert_separators: z.ZodOptional<z.ZodBoolean>;
|
|
70
|
+
}, "strip", z.ZodTypeAny, {
|
|
71
|
+
to: "western" | "eastern" | "persian";
|
|
72
|
+
text: string;
|
|
73
|
+
from?: "western" | "eastern" | "persian" | "auto" | undefined;
|
|
74
|
+
convert_separators?: boolean | undefined;
|
|
75
|
+
}, {
|
|
76
|
+
to: "western" | "eastern" | "persian";
|
|
77
|
+
text: string;
|
|
78
|
+
from?: "western" | "eastern" | "persian" | "auto" | undefined;
|
|
79
|
+
convert_separators?: boolean | undefined;
|
|
80
|
+
}>;
|
|
81
|
+
export declare const convertHijriGregorianSchema: z.ZodObject<{
|
|
82
|
+
date: z.ZodString;
|
|
83
|
+
direction: z.ZodEnum<["gregorian_to_hijri", "hijri_to_gregorian"]>;
|
|
84
|
+
}, "strip", z.ZodTypeAny, {
|
|
85
|
+
direction: "gregorian_to_hijri" | "hijri_to_gregorian";
|
|
86
|
+
date: string;
|
|
87
|
+
}, {
|
|
88
|
+
direction: "gregorian_to_hijri" | "hijri_to_gregorian";
|
|
89
|
+
date: string;
|
|
90
|
+
}>;
|
|
91
|
+
export declare const cleanRtlTextSchema: z.ZodObject<{
|
|
92
|
+
text: z.ZodString;
|
|
93
|
+
strip_invisible: z.ZodOptional<z.ZodBoolean>;
|
|
94
|
+
keep_joiners: z.ZodOptional<z.ZodBoolean>;
|
|
95
|
+
normalize_spaces: z.ZodOptional<z.ZodBoolean>;
|
|
96
|
+
fix_punctuation_spacing: z.ZodOptional<z.ZodBoolean>;
|
|
97
|
+
isolate: z.ZodOptional<z.ZodBoolean>;
|
|
98
|
+
remove_control_chars: z.ZodOptional<z.ZodBoolean>;
|
|
99
|
+
}, "strip", z.ZodTypeAny, {
|
|
100
|
+
text: string;
|
|
101
|
+
isolate?: boolean | undefined;
|
|
102
|
+
remove_control_chars?: boolean | undefined;
|
|
103
|
+
strip_invisible?: boolean | undefined;
|
|
104
|
+
keep_joiners?: boolean | undefined;
|
|
105
|
+
normalize_spaces?: boolean | undefined;
|
|
106
|
+
fix_punctuation_spacing?: boolean | undefined;
|
|
107
|
+
}, {
|
|
108
|
+
text: string;
|
|
109
|
+
isolate?: boolean | undefined;
|
|
110
|
+
remove_control_chars?: boolean | undefined;
|
|
111
|
+
strip_invisible?: boolean | undefined;
|
|
112
|
+
keep_joiners?: boolean | undefined;
|
|
113
|
+
normalize_spaces?: boolean | undefined;
|
|
114
|
+
fix_punctuation_spacing?: boolean | undefined;
|
|
115
|
+
}>;
|
|
116
|
+
export declare function createServer(): McpServer;
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool registration for @open-mena-mcp/arabic-tools.
|
|
3
|
+
*
|
|
4
|
+
* Five zero-auth, offline tools. Nothing here makes a network call: the Arabic text
|
|
5
|
+
* rules are local and the Hijri calendar comes from the runtime's ICU data.
|
|
6
|
+
*/
|
|
7
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
8
|
+
import { z } from "zod";
|
|
9
|
+
import { DEFAULT_STRIP_DIACRITICS_OPTIONS, stripDiacritics } from "./text/diacritics.js";
|
|
10
|
+
import { convertHijriGregorian } from "./text/hijri.js";
|
|
11
|
+
import { DEFAULT_NORMALIZE_OPTIONS, normalizeArabic } from "./text/normalize.js";
|
|
12
|
+
import { convertNumerals } from "./text/numerals.js";
|
|
13
|
+
import { DEFAULT_CLEAN_RTL_OPTIONS, cleanRtlText } from "./text/rtl.js";
|
|
14
|
+
import { defineTool } from "./toolkit.js";
|
|
15
|
+
export const SERVER_NAME = "@open-mena-mcp/arabic-tools";
|
|
16
|
+
export const SERVER_VERSION = "0.1.0";
|
|
17
|
+
export const SERVER_INSTRUCTIONS = "Arabic text utilities for MENA workflows: normalize_arabic folds letter variants for " +
|
|
18
|
+
"search and matching, strip_diacritics removes tashkeel, convert_numerals moves digits " +
|
|
19
|
+
"between Western, Eastern Arabic-Indic and Persian systems, convert_hijri_gregorian " +
|
|
20
|
+
"converts dates offline with the Umm al-Qura calendar, and clean_rtl_text fixes bidi " +
|
|
21
|
+
"marks and punctuation spacing. All five tools are offline and need no API key.";
|
|
22
|
+
const numeralSystemSchema = z
|
|
23
|
+
.enum(["western", "eastern", "persian"])
|
|
24
|
+
.describe("Numeral system: 'western' is 0-9, 'eastern' is Arabic-Indic ٠-٩ (Egypt, Gulf), 'persian' is ۰-۹ (Iran).");
|
|
25
|
+
export const normalizeArabicSchema = z.object({
|
|
26
|
+
text: z.string().min(1).describe("The Arabic text to normalize."),
|
|
27
|
+
strip_diacritics: z
|
|
28
|
+
.boolean()
|
|
29
|
+
.optional()
|
|
30
|
+
.describe("Remove tashkeel (harakat, tanween, shadda, sukun) before folding letters. Default true."),
|
|
31
|
+
unify_alef: z
|
|
32
|
+
.boolean()
|
|
33
|
+
.optional()
|
|
34
|
+
.describe("Fold the alef variants آ أ إ ٱ to a bare alef ا. Default true."),
|
|
35
|
+
unify_yaa: z.boolean().optional().describe("Fold alef maqsura ى to yaa ي. Default true."),
|
|
36
|
+
unify_hamza: z
|
|
37
|
+
.boolean()
|
|
38
|
+
.optional()
|
|
39
|
+
.describe("Fold the hamza carriers ؤ to و and ئ to ي. Default true."),
|
|
40
|
+
unify_taa_marbuta: z.boolean().optional().describe("Fold taa marbuta ة to haa ه. Default true."),
|
|
41
|
+
remove_tatweel: z
|
|
42
|
+
.boolean()
|
|
43
|
+
.optional()
|
|
44
|
+
.describe("Remove tatweel / kashida (U+0640) used to stretch a word. Default true."),
|
|
45
|
+
decompose_presentation_forms: z
|
|
46
|
+
.boolean()
|
|
47
|
+
.optional()
|
|
48
|
+
.describe("Decompose Arabic presentation forms (U+FB50..U+FEFF, common in PDF and legacy encodings) to base letters. Default true."),
|
|
49
|
+
remove_control_chars: z
|
|
50
|
+
.boolean()
|
|
51
|
+
.optional()
|
|
52
|
+
.describe("Remove C0/C1 control characters, keeping newline and tab. Default true."),
|
|
53
|
+
collapse_whitespace: z
|
|
54
|
+
.boolean()
|
|
55
|
+
.optional()
|
|
56
|
+
.describe("Collapse runs of spaces (including non-breaking spaces) to one space and trim. Default true."),
|
|
57
|
+
});
|
|
58
|
+
export const stripDiacriticsSchema = z.object({
|
|
59
|
+
text: z.string().min(1).describe("The Arabic text whose diacritics should be removed."),
|
|
60
|
+
keep_shadda: z
|
|
61
|
+
.boolean()
|
|
62
|
+
.optional()
|
|
63
|
+
.describe("Keep the shadda (U+0651) that marks a doubled consonant. Default false."),
|
|
64
|
+
keep_sukun: z
|
|
65
|
+
.boolean()
|
|
66
|
+
.optional()
|
|
67
|
+
.describe("Keep the sukun (U+0652) that marks a vowelless consonant. Default false."),
|
|
68
|
+
remove_tatweel: z
|
|
69
|
+
.boolean()
|
|
70
|
+
.optional()
|
|
71
|
+
.describe("Also remove tatweel / kashida (U+0640). Default true."),
|
|
72
|
+
remove_quranic_marks: z
|
|
73
|
+
.boolean()
|
|
74
|
+
.optional()
|
|
75
|
+
.describe("Remove Quranic annotation signs (U+06D6..U+06ED). Default true."),
|
|
76
|
+
});
|
|
77
|
+
export const convertNumeralsSchema = z.object({
|
|
78
|
+
text: z
|
|
79
|
+
.string()
|
|
80
|
+
.min(1)
|
|
81
|
+
.describe("Text whose digits should be converted. Non-digit characters are left untouched."),
|
|
82
|
+
from: z
|
|
83
|
+
.union([numeralSystemSchema, z.literal("auto")])
|
|
84
|
+
.optional()
|
|
85
|
+
.describe("Source digit system, or 'auto' to convert digits from all three systems. Default 'auto'."),
|
|
86
|
+
to: numeralSystemSchema.describe("Target digit system."),
|
|
87
|
+
convert_separators: z
|
|
88
|
+
.boolean()
|
|
89
|
+
.optional()
|
|
90
|
+
.describe("Also convert the Arabic decimal separator U+066B and thousands separator U+066C to and from '.' and ','. Default true."),
|
|
91
|
+
});
|
|
92
|
+
export const convertHijriGregorianSchema = z.object({
|
|
93
|
+
date: z
|
|
94
|
+
.string()
|
|
95
|
+
.regex(/^\d{4}-\d{2}-\d{2}$/u, "Expected YYYY-MM-DD")
|
|
96
|
+
.describe("Date in YYYY-MM-DD format: Gregorian when direction is 'gregorian_to_hijri', Hijri (AH) when direction is 'hijri_to_gregorian'."),
|
|
97
|
+
direction: z.enum(["gregorian_to_hijri", "hijri_to_gregorian"]).describe("Which way to convert."),
|
|
98
|
+
});
|
|
99
|
+
export const cleanRtlTextSchema = z.object({
|
|
100
|
+
text: z.string().min(1).describe("The right-to-left text to clean up."),
|
|
101
|
+
strip_invisible: z
|
|
102
|
+
.boolean()
|
|
103
|
+
.optional()
|
|
104
|
+
.describe("Remove bidi control characters (RLM, LRM, ALM, RLE/PDF, RLI/PDI) and zero-width characters. Default true."),
|
|
105
|
+
keep_joiners: z
|
|
106
|
+
.boolean()
|
|
107
|
+
.optional()
|
|
108
|
+
.describe("Keep ZWNJ (U+200C) and ZWJ (U+200D), which are meaningful in Persian and emoji. Default false."),
|
|
109
|
+
normalize_spaces: z
|
|
110
|
+
.boolean()
|
|
111
|
+
.optional()
|
|
112
|
+
.describe("Collapse runs of spaces and trim the result. Default true."),
|
|
113
|
+
fix_punctuation_spacing: z
|
|
114
|
+
.boolean()
|
|
115
|
+
.optional()
|
|
116
|
+
.describe("Remove spaces before punctuation and add a space after Arabic punctuation (، ؛ ؟). Default true."),
|
|
117
|
+
isolate: z
|
|
118
|
+
.boolean()
|
|
119
|
+
.optional()
|
|
120
|
+
.describe("Wrap the result in RLI (U+2067) and PDI (U+2069) so it renders right-to-left inside a left-to-right document. Default false."),
|
|
121
|
+
remove_control_chars: z
|
|
122
|
+
.boolean()
|
|
123
|
+
.optional()
|
|
124
|
+
.describe("Remove C0/C1 control characters, keeping newline and tab. Default true."),
|
|
125
|
+
});
|
|
126
|
+
const READ_ONLY = { readOnlyHint: true, idempotentHint: true, openWorldHint: false };
|
|
127
|
+
export function createServer() {
|
|
128
|
+
const server = new McpServer({ name: SERVER_NAME, version: SERVER_VERSION }, { instructions: SERVER_INSTRUCTIONS });
|
|
129
|
+
server.registerTool("normalize_arabic", {
|
|
130
|
+
title: "Normalize Arabic text",
|
|
131
|
+
description: "Fold Arabic letter variants (آ أ إ ٱ -> ا, ى -> ي, ة -> ه, ؤ/ئ -> و/ي) and clean marks, tatweel, presentation forms and spacing. Use this to make Arabic text comparable for search, deduplication and keyword matching. Returns the normalized text with a count of each change.",
|
|
132
|
+
inputSchema: normalizeArabicSchema.shape,
|
|
133
|
+
annotations: READ_ONLY,
|
|
134
|
+
}, defineTool(normalizeArabicSchema, (input) => normalizeArabic(input.text, {
|
|
135
|
+
stripDiacritics: input.strip_diacritics ?? DEFAULT_NORMALIZE_OPTIONS.stripDiacritics,
|
|
136
|
+
unifyAlef: input.unify_alef ?? DEFAULT_NORMALIZE_OPTIONS.unifyAlef,
|
|
137
|
+
unifyYaa: input.unify_yaa ?? DEFAULT_NORMALIZE_OPTIONS.unifyYaa,
|
|
138
|
+
unifyHamza: input.unify_hamza ?? DEFAULT_NORMALIZE_OPTIONS.unifyHamza,
|
|
139
|
+
unifyTaaMarbuta: input.unify_taa_marbuta ?? DEFAULT_NORMALIZE_OPTIONS.unifyTaaMarbuta,
|
|
140
|
+
removeTatweel: input.remove_tatweel ?? DEFAULT_NORMALIZE_OPTIONS.removeTatweel,
|
|
141
|
+
decomposePresentationForms: input.decompose_presentation_forms ??
|
|
142
|
+
DEFAULT_NORMALIZE_OPTIONS.decomposePresentationForms,
|
|
143
|
+
removeControlChars: input.remove_control_chars ?? DEFAULT_NORMALIZE_OPTIONS.removeControlChars,
|
|
144
|
+
collapseWhitespace: input.collapse_whitespace ?? DEFAULT_NORMALIZE_OPTIONS.collapseWhitespace,
|
|
145
|
+
})));
|
|
146
|
+
server.registerTool("strip_diacritics", {
|
|
147
|
+
title: "Strip Arabic diacritics",
|
|
148
|
+
description: "Remove Arabic diacritics (harakat, tanween, shadda, sukun, superscript alef, Quranic annotation signs) and optionally tatweel. Use this when you want the bare consonants, for example before indexing text or comparing two spellings. Shadda and sukun can be kept.",
|
|
149
|
+
inputSchema: stripDiacriticsSchema.shape,
|
|
150
|
+
annotations: READ_ONLY,
|
|
151
|
+
}, defineTool(stripDiacriticsSchema, (input) => stripDiacritics(input.text, {
|
|
152
|
+
keepShadda: input.keep_shadda ?? DEFAULT_STRIP_DIACRITICS_OPTIONS.keepShadda,
|
|
153
|
+
keepSukun: input.keep_sukun ?? DEFAULT_STRIP_DIACRITICS_OPTIONS.keepSukun,
|
|
154
|
+
removeTatweel: input.remove_tatweel ?? DEFAULT_STRIP_DIACRITICS_OPTIONS.removeTatweel,
|
|
155
|
+
removeQuranicMarks: input.remove_quranic_marks ?? DEFAULT_STRIP_DIACRITICS_OPTIONS.removeQuranicMarks,
|
|
156
|
+
})));
|
|
157
|
+
server.registerTool("convert_numerals", {
|
|
158
|
+
title: "Convert numeral systems",
|
|
159
|
+
description: "Convert digits between Western (0-9), Eastern Arabic-Indic (٠-٩) and Persian (۰-۹), and convert the Arabic decimal and thousands separators. Use 'from: auto' when a document mixes systems. Returns the converted text and how many characters changed.",
|
|
160
|
+
inputSchema: convertNumeralsSchema.shape,
|
|
161
|
+
annotations: READ_ONLY,
|
|
162
|
+
}, defineTool(convertNumeralsSchema, (input) => convertNumerals(input.text, {
|
|
163
|
+
from: input.from ?? "auto",
|
|
164
|
+
to: input.to,
|
|
165
|
+
convertSeparators: input.convert_separators ?? true,
|
|
166
|
+
})));
|
|
167
|
+
server.registerTool("convert_hijri_gregorian", {
|
|
168
|
+
title: "Convert Hijri and Gregorian dates",
|
|
169
|
+
description: "Convert a date between the Gregorian calendar and the Umm al-Qura Hijri calendar, offline, with no API key. Returns both dates, month and weekday names in Arabic and English, and pre-formatted Hijri strings. The tabulated Umm al-Qura range is 1300-1600 AH; dates outside it are rejected rather than approximated.",
|
|
170
|
+
inputSchema: convertHijriGregorianSchema.shape,
|
|
171
|
+
annotations: READ_ONLY,
|
|
172
|
+
}, defineTool(convertHijriGregorianSchema, (input) => convertHijriGregorian(input.date, input.direction)));
|
|
173
|
+
server.registerTool("clean_rtl_text", {
|
|
174
|
+
title: "Clean RTL text",
|
|
175
|
+
description: "Clean Arabic text copied from forms, web pages, PDFs or chat: remove invisible bidi control and zero-width characters, fix spacing before and after Arabic punctuation, normalize spaces, and optionally wrap the result in RLI/PDI isolation marks. Reports the Arabic/Latin letter counts and the dominant direction so mixed text is visible.",
|
|
176
|
+
inputSchema: cleanRtlTextSchema.shape,
|
|
177
|
+
annotations: READ_ONLY,
|
|
178
|
+
}, defineTool(cleanRtlTextSchema, (input) => cleanRtlText(input.text, {
|
|
179
|
+
stripInvisible: input.strip_invisible ?? DEFAULT_CLEAN_RTL_OPTIONS.stripInvisible,
|
|
180
|
+
keepJoiners: input.keep_joiners ?? DEFAULT_CLEAN_RTL_OPTIONS.keepJoiners,
|
|
181
|
+
normalizeSpaces: input.normalize_spaces ?? DEFAULT_CLEAN_RTL_OPTIONS.normalizeSpaces,
|
|
182
|
+
fixPunctuationSpacing: input.fix_punctuation_spacing ?? DEFAULT_CLEAN_RTL_OPTIONS.fixPunctuationSpacing,
|
|
183
|
+
isolate: input.isolate ?? DEFAULT_CLEAN_RTL_OPTIONS.isolate,
|
|
184
|
+
removeControlChars: input.remove_control_chars ?? DEFAULT_CLEAN_RTL_OPTIONS.removeControlChars,
|
|
185
|
+
})));
|
|
186
|
+
return server;
|
|
187
|
+
}
|
|
188
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"server.js","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,yCAAyC,CAAC;AACpE,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,gCAAgC,EAAE,eAAe,EAAE,MAAM,sBA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AK,EAAE,EAAE,CAChD,qBAAqB,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,CAAC,SAAS,CAAC,CACnD,CACF,CAAC;IAEF,MAAM,CAAC,YAAY,CACjB,gBAAgB,EAChB;QACE,KAAK,EAAE,gBAAgB;QACvB,WAAW,EACT,kVAAkV;QACpV,WAAW,EAAE,kBAAkB,CAAC,KAAK;QACrC,WAAW,EAAE,SAAS;KACvB,EACD,UAAU,CAAC,kBAAkB,EAAE,CAAC,KAAK,EAAE,EAAE,CACvC,YAAY,CAAC,KAAK,CAAC,IAAI,EAAE;QACvB,cAAc,EAAE,KAAK,CAAC,eAAe,IAAI,yBAAyB,CAAC,cAAc;QACjF,WAAW,EAAE,KAAK,CAAC,YAAY,IAAI,yBAAyB,CAAC,WAAW;QACxE,eAAe,EAAE,KAAK,CAAC,gBAAgB,IAAI,yBAAyB,CAAC,eAAe;QACpF,qBAAqB,EACnB,KAAK,CAAC,uBAAuB,IAAI,yBAAyB,CAAC,qBAAqB;QAClF,OAAO,EAAE,KAAK,CAAC,OAAO,IAAI,yBAAyB,CAAC,OAAO;QAC3D,kBAAkB,EAChB,KAAK,CAAC,oBAAoB,IAAI,yBAAyB,CAAC,kBAAkB;KAC7E,CAAC,CACH,CACF,CAAC;IAEF,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Arabic diacritic (tashkeel) removal.
|
|
3
|
+
*
|
|
4
|
+
* The ranges below are the Unicode Arabic mark blocks: harakat, tanween, shadda,
|
|
5
|
+
* sukun, superscript alef, and the Quranic annotation signs. Tatweel (kashida) is
|
|
6
|
+
* a letter-stretching glyph rather than a mark, so it has its own option.
|
|
7
|
+
*/
|
|
8
|
+
export interface StripDiacriticsOptions {
|
|
9
|
+
/** Keep the shadda (U+0651) that marks geminated consonants. */
|
|
10
|
+
keepShadda: boolean;
|
|
11
|
+
/** Keep the sukun (U+0652) that marks a vowelless consonant. */
|
|
12
|
+
keepSukun: boolean;
|
|
13
|
+
/** Remove tatweel / kashida (U+0640) used to stretch a word. */
|
|
14
|
+
removeTatweel: boolean;
|
|
15
|
+
/** Remove Quranic annotation signs (U+06D6..U+06ED) and small high letters. */
|
|
16
|
+
removeQuranicMarks: boolean;
|
|
17
|
+
}
|
|
18
|
+
export interface StripDiacriticsResult {
|
|
19
|
+
/** Text with the requested marks removed. */
|
|
20
|
+
text: string;
|
|
21
|
+
/** Number of code points removed. */
|
|
22
|
+
removed: number;
|
|
23
|
+
/** Number of code points in the input. */
|
|
24
|
+
inputLength: number;
|
|
25
|
+
}
|
|
26
|
+
export declare const DEFAULT_STRIP_DIACRITICS_OPTIONS: StripDiacriticsOptions;
|
|
27
|
+
/** True when the code point is a mark this option set asks to remove. */
|
|
28
|
+
export declare function isArabicMark(codePoint: number, options: StripDiacriticsOptions): boolean;
|
|
29
|
+
/** Remove Arabic diacritics and (optionally) tatweel from a string. */
|
|
30
|
+
export declare function stripDiacritics(input: string, options?: Partial<StripDiacriticsOptions>): StripDiacriticsResult;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Arabic diacritic (tashkeel) removal.
|
|
3
|
+
*
|
|
4
|
+
* The ranges below are the Unicode Arabic mark blocks: harakat, tanween, shadda,
|
|
5
|
+
* sukun, superscript alef, and the Quranic annotation signs. Tatweel (kashida) is
|
|
6
|
+
* a letter-stretching glyph rather than a mark, so it has its own option.
|
|
7
|
+
*/
|
|
8
|
+
export const DEFAULT_STRIP_DIACRITICS_OPTIONS = {
|
|
9
|
+
keepShadda: false,
|
|
10
|
+
keepSukun: false,
|
|
11
|
+
removeTatweel: true,
|
|
12
|
+
removeQuranicMarks: true,
|
|
13
|
+
};
|
|
14
|
+
const SHADDA = 0x0651;
|
|
15
|
+
const SUKUN = 0x0652;
|
|
16
|
+
const TATWEEL = 0x0640;
|
|
17
|
+
const HARAKAT_FIRST = 0x064b;
|
|
18
|
+
const HARAKAT_LAST = 0x065f;
|
|
19
|
+
const SUPERSCRIPT_ALEF = 0x0670;
|
|
20
|
+
const QURANIC_FIRST = 0x06d6;
|
|
21
|
+
const QURANIC_LAST = 0x06ed;
|
|
22
|
+
/** True when the code point is a mark this option set asks to remove. */
|
|
23
|
+
export function isArabicMark(codePoint, options) {
|
|
24
|
+
if (codePoint === SHADDA)
|
|
25
|
+
return !options.keepShadda;
|
|
26
|
+
if (codePoint === SUKUN)
|
|
27
|
+
return !options.keepSukun;
|
|
28
|
+
if (codePoint >= HARAKAT_FIRST && codePoint <= HARAKAT_LAST)
|
|
29
|
+
return true;
|
|
30
|
+
if (codePoint === SUPERSCRIPT_ALEF)
|
|
31
|
+
return true;
|
|
32
|
+
if (codePoint === TATWEEL)
|
|
33
|
+
return options.removeTatweel;
|
|
34
|
+
if (codePoint >= QURANIC_FIRST && codePoint <= QURANIC_LAST)
|
|
35
|
+
return options.removeQuranicMarks;
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
/** Remove Arabic diacritics and (optionally) tatweel from a string. */
|
|
39
|
+
export function stripDiacritics(input, options = {}) {
|
|
40
|
+
const merged = {
|
|
41
|
+
keepShadda: options.keepShadda ?? DEFAULT_STRIP_DIACRITICS_OPTIONS.keepShadda,
|
|
42
|
+
keepSukun: options.keepSukun ?? DEFAULT_STRIP_DIACRITICS_OPTIONS.keepSukun,
|
|
43
|
+
removeTatweel: options.removeTatweel ?? DEFAULT_STRIP_DIACRITICS_OPTIONS.removeTatweel,
|
|
44
|
+
removeQuranicMarks: options.removeQuranicMarks ?? DEFAULT_STRIP_DIACRITICS_OPTIONS.removeQuranicMarks,
|
|
45
|
+
};
|
|
46
|
+
let text = "";
|
|
47
|
+
let removed = 0;
|
|
48
|
+
let inputLength = 0;
|
|
49
|
+
for (const char of input) {
|
|
50
|
+
inputLength += 1;
|
|
51
|
+
const codePoint = char.codePointAt(0) ?? 0;
|
|
52
|
+
if (isArabicMark(codePoint, merged)) {
|
|
53
|
+
removed += 1;
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
text += char;
|
|
57
|
+
}
|
|
58
|
+
return { text, removed, inputLength };
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=diacritics.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"diacritics.js","sourceRoot":"","sources":["../../src/text/diacritics.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAsBH,MAAM,CAAC,MAAM,gCAAgC,GAA2B;IACtE,UAAU,EAAE,KAAK;IACjB,SAAS,EAAE,KAAK;IAChB,aAAa,EAAE,IAAI;IACnB,kBAAkB,EAAE,IAAI;CACzB,CAAC;AAEF,MAAM,MAAM,GAAG,MAAM,CAAC;AACtB,MAAM,KAAK,GAAG,MAAM,CAAC;AACrB,MAAM,OAAO,GAAG,MAAM,CAAC;AACvB,MAAM,aAAa,GAAG,MAAM,CAAC;AAC7B,MAAM,YAAY,GAAG,MAAM,CAAC;AAC5B,MAAM,gBAAgB,GAAG,MAAM,CAAC;AAChC,MAAM,aAAa,GAAG,MAAM,CAAC;AAC7B,MAAM,YAAY,GAAG,MAAM,CAAC;AAE5B,yEAAyE;AACzE,MAAM,UAAU,YAAY,CAAC,SAAiB,EAAE,OAA+B;IAC7E,IAAI,SAAS,KAAK,MAAM;QAAE,OAAO,CAAC,OAAO,CAAC,UAAU,CAAC;IACrD,IAAI,SAAS,KAAK,KAAK;QAAE,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC;IACnD,IAAI,SAAS,IAAI,aAAa,IAAI,SAAS,IAAI,YAAY;QAAE,OAAO,IAAI,CAAC;IACzE,IAAI,SAAS,KAAK,gBAAgB;QAAE,OAAO,IAAI,CAAC;IAChD,IAAI,SAAS,KAAK,OAAO;QAAE,OAAO,OAAO,CAAC,aAAa,CAAC;IACxD,IAAI,SAAS,IAAI,aAAa,IAAI,SAAS,IAAI,YAAY;QAAE,OAAO,OAAO,CAAC,kBAAkB,CAAC;IAC/F,OAAO,KA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