Skip to content

Commit

Permalink
Test publish alpha.4 using web-tree-sitter, parcel, wasms etc in cjs …
Browse files Browse the repository at this point in the history
…and esm formats. Tested in node, and NextJs
  • Loading branch information
kavitharaju committed Aug 20, 2024
1 parent 5a0c7a7 commit 66b0d9d
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 32 deletions.
2 changes: 2 additions & 0 deletions docs/Dev_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
9 changes: 5 additions & 4 deletions js-usfm-parser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand All @@ -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.
Expand Down
9 changes: 3 additions & 6 deletions js-usfm-parser/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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",
Expand Down
8 changes: 6 additions & 2 deletions js-usfm-parser/src/usfmParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
14 changes: 0 additions & 14 deletions js-usfm-parser/src/utils/base64ToUint8Array.js

This file was deleted.

12 changes: 6 additions & 6 deletions js-usfm-parser/test.js
Original file line number Diff line number Diff line change
@@ -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 });
})();

0 comments on commit 66b0d9d

Please sign in to comment.