word-def 1.1.2 → 1.1.3
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/index.js +24 -10
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -43,8 +43,9 @@ for (let i = 0; i < args.length; i++) {
|
|
|
43
43
|
}
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
// Make search case-insensitive
|
|
46
|
+
// Make search case-insensitive - try original first, then lowercase
|
|
47
47
|
const searchWord = word.toLowerCase();
|
|
48
|
+
const originalWord = word;
|
|
48
49
|
|
|
49
50
|
if (!word) {
|
|
50
51
|
showHelp();
|
|
@@ -55,23 +56,36 @@ const langName = wiktionaryLanguages[lang] || lang;
|
|
|
55
56
|
fetchFromWiktionary(searchWord, lang);
|
|
56
57
|
|
|
57
58
|
function fetchFromWiktionary(word, targetLang) {
|
|
59
|
+
// Try original case first, then lowercase
|
|
60
|
+
const wordsToTry = [originalWord, searchWord];
|
|
61
|
+
|
|
58
62
|
// Try the language-specific Wiktionary first
|
|
59
63
|
const langCodes = [
|
|
60
64
|
targetLang,
|
|
61
65
|
targetLang === 'en' ? null : 'en' // fallback to English
|
|
62
66
|
];
|
|
63
67
|
|
|
64
|
-
|
|
68
|
+
tryNextWord(0);
|
|
65
69
|
|
|
66
|
-
function
|
|
70
|
+
function tryNextWord(wordIndex) {
|
|
71
|
+
if (wordIndex >= wordsToTry.length) {
|
|
72
|
+
tryNextLanguage(0, null);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
tryNextLanguage(0, wordsToTry[wordIndex]);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function tryNextLanguage(index, currentWord) {
|
|
67
80
|
if (index >= langCodes.length || !langCodes[index]) {
|
|
68
|
-
|
|
69
|
-
|
|
81
|
+
// Try next word variant
|
|
82
|
+
tryNextWord(wordsToTry.indexOf(currentWord) + 1);
|
|
83
|
+
return;
|
|
70
84
|
}
|
|
71
85
|
|
|
72
86
|
const currentLang = langCodes[index];
|
|
73
87
|
const hostname = currentLang === 'en' ? 'en.wiktionary.org' : `${currentLang}.wiktionary.org`;
|
|
74
|
-
const path = `/api/rest_v1/page/definition/${encodeURIComponent(
|
|
88
|
+
const path = `/api/rest_v1/page/definition/${encodeURIComponent(currentWord)}`;
|
|
75
89
|
|
|
76
90
|
const options = {
|
|
77
91
|
hostname,
|
|
@@ -103,18 +117,18 @@ function fetchFromWiktionary(word, targetLang) {
|
|
|
103
117
|
const definitions = targetDefs || fallbackDefs;
|
|
104
118
|
|
|
105
119
|
if (!definitions || definitions.length === 0) {
|
|
106
|
-
tryNextLanguage(index + 1);
|
|
120
|
+
tryNextLanguage(index + 1, currentWord);
|
|
107
121
|
return;
|
|
108
122
|
}
|
|
109
123
|
|
|
110
|
-
displayDefinitions(
|
|
124
|
+
displayDefinitions(currentWord, targetLang, definitions, parsed);
|
|
111
125
|
|
|
112
126
|
} catch (e) {
|
|
113
|
-
tryNextLanguage(index + 1);
|
|
127
|
+
tryNextLanguage(index + 1, currentWord);
|
|
114
128
|
}
|
|
115
129
|
});
|
|
116
130
|
}).on('error', () => {
|
|
117
|
-
tryNextLanguage(index + 1);
|
|
131
|
+
tryNextLanguage(index + 1, currentWord);
|
|
118
132
|
});
|
|
119
133
|
}
|
|
120
134
|
}
|