twl-generator 1.0.1 → 1.0.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "twl-generator",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "Generate term-to-article lists from unfoldingWord en_tw archive for Bible books. Works in both Node.js (CLI) and React.js (browser) environments.",
5
5
  "main": "src/index.js",
6
6
  "bin": {
@@ -57,4 +57,4 @@
57
57
  "optional": true
58
58
  }
59
59
  }
60
- }
60
+ }
@@ -1,6 +1,3 @@
1
- import fs from 'fs';
2
- import path from 'path';
3
-
4
1
  /**
5
2
  * Generate morphological variants of a term
6
3
  */
@@ -1,8 +1,28 @@
1
1
  /* eslint-disable no-async-promise-executor, no-throw-literal */
2
2
 
3
- import fetch from 'node-fetch';
4
3
  import { BibleBookData } from '../common/books.js';
5
4
 
5
+ // Environment detection
6
+ const isNode = typeof window === 'undefined' && typeof process !== 'undefined' && process.versions?.node;
7
+
8
+ // Get appropriate fetch implementation
9
+ async function getFetch() {
10
+ if (isNode) {
11
+ const nodeFetch = await import('node-fetch');
12
+ return nodeFetch.default;
13
+ }
14
+ return globalThis.fetch;
15
+ }
16
+
17
+ // Get appropriate base64 decoder
18
+ function decodeBase64(base64String) {
19
+ if (isNode) {
20
+ return Buffer.from(base64String, 'base64').toString('utf-8');
21
+ }
22
+ // Browser implementation
23
+ return atob(base64String);
24
+ }
25
+
6
26
  // Note: This version doesn't use usfm-js to avoid external dependencies
7
27
  // It implements a simple USFM alignment remover for the specific case
8
28
 
@@ -53,11 +73,12 @@ export const removeAllTagsExceptChapterVerse = (usfmContent) => {
53
73
  export async function processUsfmForBook(book) {
54
74
  if (!BibleBookData[book]) throw new Error(`Unknown book: ${book}`);
55
75
 
76
+ const fetch = await getFetch();
56
77
  const usfmUrl = `https://git.door43.org/api/v1/repos/unfoldingWord/en_ult/contents/${BibleBookData[book].usfm}.usfm?ref=master`;
57
78
  const usfmRes = await fetch(usfmUrl);
58
79
  if (!usfmRes.ok) throw new Error(`Failed to download USFM file for ${book}`);
59
80
  const usfmData = await usfmRes.json();
60
- const usfmContent = Buffer.from(usfmData.content, 'base64').toString('utf-8');
81
+ const usfmContent = decodeBase64(usfmData.content);
61
82
 
62
83
  // Remove alignments from USFM
63
84
  const cleanUsfm = removeAllTagsExceptChapterVerse(usfmContent);