From 66b0d9df660f4d14de4a472c303dfcd12b8d2112 Mon Sep 17 00:00:00 2001 From: "Kavitha.Raju" Date: Tue, 20 Aug 2024 17:10:36 +0530 Subject: [PATCH] Test publish alpha.4 using web-tree-sitter, parcel, wasms etc in cjs and esm formats. Tested in node, and NextJs --- docs/Dev_notes.md | 2 ++ js-usfm-parser/README.md | 9 +++++---- js-usfm-parser/package.json | 9 +++------ js-usfm-parser/src/usfmParser.js | 8 ++++++-- js-usfm-parser/src/utils/base64ToUint8Array.js | 14 -------------- js-usfm-parser/test.js | 12 ++++++------ 6 files changed, 22 insertions(+), 32 deletions(-) delete mode 100644 js-usfm-parser/src/utils/base64ToUint8Array.js diff --git a/docs/Dev_notes.md b/docs/Dev_notes.md index 1ce50574..e1f8ae95 100644 --- a/docs/Dev_notes.md +++ b/docs/Dev_notes.md @@ -58,6 +58,8 @@ export PATH=$PATH:./node_modules/.bin tree-sitter generate cp tree-sitter-usfm.wasm ../js-usfm-parser/ ``` +After npm install, copy the `tree-sitter.wasm` file from `node_modules/web=tree-sitter` to the `js-usfm-parser` folder to include it with the npm packaging. + Build the code base generating both cjs and esm versions of the same code base. The configs are in `.babelrc` file. Upon running the commands two folders `dist/cjs/` and `dist/esm` would be created. diff --git a/js-usfm-parser/README.md b/js-usfm-parser/README.md index 89b2b099..fd6eb9cc 100644 --- a/js-usfm-parser/README.md +++ b/js-usfm-parser/README.md @@ -14,10 +14,11 @@ npm install usfm-grammar Here's how you can use USFM Grammar in your JavaScript/TypeScript projects: ```javascript -import USFMParser from 'usfm-grammar'; +import pkg from 'usfm-grammar'; +const USFMParser = pkg.USFMParser; (async () => { - await USFMParser.init("./node_modules/usfm-grammar/tree-sitter-usfm.wasm"); + await USFMParser.init(); const usfmParser = new USFMParser() const output = usfmParser.usfmToUsj('\\id GEN\n\\c 1\n\\p\n\\v 1 In the begining..\\v 2 more text') console.log({ output }) @@ -28,14 +29,14 @@ import USFMParser from 'usfm-grammar'; If you are using node the import part can be change as below: ```javascript -const USFMParser = require('usfm-grammar').default; +const { USFMParser} = require('usfm-grammar'); ``` ## API Documentation ### `USFMParser.init()` -Initializes the USFMParser. This function must be called before creating instances of `USFMParser`. And it should take the grammar file(in wasm format) that is included in the package passing the file. +Initializes the USFMParser. This function must be called before creating instances of `USFMParser`. And can take the grammar and the tree-sitter files (in wasm format) as arguments, that is included in the package. ### `USFMParser.usfmToUsj(usfmString: string): Object` Converts a USFM string to a USJ object. diff --git a/js-usfm-parser/package.json b/js-usfm-parser/package.json index a45131dc..d8bd2547 100644 --- a/js-usfm-parser/package.json +++ b/js-usfm-parser/package.json @@ -1,6 +1,6 @@ { "name": "usfm-grammar", - "version": "3.0.0-alpha.19", + "version": "3.0.0-alpha.4", "description": "Parser using tree-sitter-usfm3, to convert usfm to usj format.", "type": "module", "main": "dist/cjs/index.cjs", @@ -10,13 +10,10 @@ "scripts": { "build": "parcel build src/index.js" }, - "alias": { - "fs": false, - "path": false - }, "files": [ "dist/", - "tree-sitter-usfm.wasm" + "tree-sitter-usfm.wasm", + "tree-sitter.wasm" ], "repository": { "type": "git", diff --git a/js-usfm-parser/src/usfmParser.js b/js-usfm-parser/src/usfmParser.js index b4e4cea3..342bd197 100644 --- a/js-usfm-parser/src/usfmParser.js +++ b/js-usfm-parser/src/usfmParser.js @@ -3,11 +3,15 @@ import Parser from 'web-tree-sitter'; import USFMGenerator from "./usfmGenerator.js"; import USJGenerator from "./usjGenerator.js"; import { includeMarkersInUsj, excludeMarkersInUsj } from "./filters.js"; +const locateFile = (scriptName, scriptDirectory) => { + return `node_modules/usfm-grammar/${scriptName}`; +}; class USFMParser { static language = null; - static async init(grammarPath="tree-sitter-usfm.wasm") { - await Parser.init(); + static async init(grammarPath="node_modules/usfm-grammar/tree-sitter-usfm.wasm", + parserPath="node_modules/web-tree-sitter/tree-sitter.wasm") { + await Parser.init({ parserPath }); USFMParser.language = await Parser.Language.load(grammarPath); } diff --git a/js-usfm-parser/src/utils/base64ToUint8Array.js b/js-usfm-parser/src/utils/base64ToUint8Array.js deleted file mode 100644 index 49a6b5ec..00000000 --- a/js-usfm-parser/src/utils/base64ToUint8Array.js +++ /dev/null @@ -1,14 +0,0 @@ -function base64ToUint8Array(encoded) { - var binaryString; - if (typeof atob === 'function') { - binaryString = atob(encoded); - } else { - binaryString = Buffer.from(encoded, 'base64').toString('binary'); - } - var uint8Array = new Uint8Array(binaryString.length); - for (var i = 0; i < binaryString.length; i++) { - uint8Array[i] = binaryString.charCodeAt(i); - } - return { uint8Array }; -} -export { base64ToUint8Array }; \ No newline at end of file diff --git a/js-usfm-parser/test.js b/js-usfm-parser/test.js index fc54275e..f571efb5 100644 --- a/js-usfm-parser/test.js +++ b/js-usfm-parser/test.js @@ -1,11 +1,11 @@ import {USFMParser} from './src/index.js'; (async () => { - await USFMParser.init(); - const usfmParser = new USFMParser() - const output = usfmParser.usfmToUsj('\\id GEN\n\\c 1\n\\p\n\\v 1 In the begining..\\v 2 more text') - console.log({ output }) - const usfm = usfmParser.usjToUsfm(output) - console.log({ usfm }) + await USFMParser.init("tree-sitter-usfm.wasm"); + const usfmParser = new USFMParser(); + const output = usfmParser.usfmToUsj('\\id GEN\n\\c 1\n\\p\n\\v 1 In the begining..\\v 2 more text'); + console.log({ output }); + const usfm = usfmParser.usjToUsfm(output); + console.log({ usfm }); })();