-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
React support with seperate usfm-grammar-web (#256)
* Bundle tree-sitter.js instead of keeping web-tree-sitter dependency * Fix the parser init to use input wasm path * Keep a seperate usfm-grammar-web * keep a separate usfm-grammar for node w/o using wasm files * Test publish another alpha.8 version of usfm-grammar at npm
- Loading branch information
1 parent
f0e73d5
commit 90263b6
Showing
35 changed files
with
1,395 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
## How to use the usfm-grammar npm package from React | ||
|
||
To use the library from a react app, there are a few extra handling required. | ||
1. The modules `fs`, `path` and `process` used by tree-sitter is not required when using from front end. But bundling may cause issue. Hence use `react-app-rewired` and in the `config-overrides.js` file add following settings: | ||
|
||
```javascript | ||
const { override } = require('customize-cra'); | ||
|
||
module.exports = override( | ||
config => { | ||
config.resolve.fallback = { | ||
fs: false, | ||
path: false, | ||
process: false | ||
}; | ||
return config; | ||
}, | ||
); | ||
``` | ||
2. When initializing the `USFMParser` class use the links to required wasm files as shown below: | ||
```javascript | ||
import React, { useEffect } from 'react'; | ||
import { USFMParser } from 'usfm-grammar'; | ||
|
||
function App() { | ||
... | ||
useEffect(() => { | ||
const initParser = async () => { | ||
await USFMParser.init("https://cdn.jsdelivr.net/npm/[email protected]/tree-sitter-usfm.wasm", | ||
"https://cdn.jsdelivr.net/npm/[email protected]/tree-sitter.wasm"); | ||
}; | ||
initParser(); | ||
}, []); | ||
... | ||
} | ||
``` |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "usfm-grammar", | ||
"version": "3.0.0-alpha.8", | ||
"description": "Parser using tree-sitter-usfm3, to convert usfm to usj format.", | ||
"main": "./dist/cjs/index.cjs", | ||
"module": "./dist/es/index.mjs", | ||
"scripts": { | ||
"build": "parcel build ./src/index.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/Bridgeconn/usfm-grammar/js-usfm-parser" | ||
}, | ||
"keywords": [ | ||
"USFM", | ||
"tree-sitter", | ||
"USJ", | ||
"Parser" | ||
], | ||
"license": "MIT", | ||
"author": "BCS Team", | ||
"contributors": [ | ||
"Kavitha Raju <[email protected]> (https://github.com/kavitharaju)", | ||
"Joel Mathew <[email protected]> (https://github.com/joelthe1)", | ||
"Samuel JD <[email protected]> (https://github.com/samueljd)" | ||
], | ||
"dependencies": { | ||
"tree-sitter": "0.21.1", | ||
"tree-sitter-usfm3": "^3.0.0-beta.10" | ||
}, | ||
"devDependencies": { | ||
"parcel": "^2.12.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
const MARKERS_WITH_DISCARDABLE_CONTENTS = [ | ||
"ide", "usfm", "h", "toc", "toca", "imt", "is", "ip", "ipi", "im", "imi", | ||
"ipq", "imq", "ipr", "iq", "ib", "ili", "iot", "io", "iex", "imte", "ie", | ||
"mt", "mte", "cl", "cd", "ms", "mr", "s", "sr", "r", "d", "sp", "sd", | ||
"sts", "rem", "lit", "restore", "f", "fe", "ef", "efe", "x", "ex", | ||
"fr", "ft", "fk", "fq", "fqa", "fl", "fw", "fp", "fv", "fdc", | ||
"xo", "xop", "xt", "xta", "xk", "xq", "xot", "xnt", "xdc", | ||
"jmp", "fig", "cat", "esb", "b" | ||
]; | ||
|
||
const trailingNumPattern = /\d+$/; | ||
const punctPatternNoSpaceBefore = /^[,.\-—/;:!?@$%^)}\]>”»]/; | ||
const punctPatternNoSpaceAfter = /[\-—/`@^&({[<“«]$/; | ||
|
||
function combineConsecutiveTextContents(contentsList) { | ||
let textCombinedContents = []; | ||
let textContents = ''; | ||
contentsList.forEach(item => { | ||
if (typeof item === 'string') { | ||
if (!(textContents.endsWith(" ") || item.startsWith(" ") || textContents === '' || | ||
punctPatternNoSpaceBefore.test(item) || punctPatternNoSpaceAfter.test(textContents))) { | ||
textContents += " "; | ||
} | ||
textContents += item; | ||
} else { | ||
if (textContents !== "") { | ||
textCombinedContents.push(textContents); | ||
textContents = ""; | ||
} | ||
textCombinedContents.push(item); | ||
} | ||
}); | ||
if (textContents !== "") { | ||
textCombinedContents.push(textContents); | ||
} | ||
return textCombinedContents; | ||
} | ||
|
||
function excludeMarkersInUsj(inputUsj, excludeMarkers, combineTexts = true, excludedParent = false) { | ||
if (typeof inputUsj === 'string') { | ||
if (excludedParent && excludeMarkers.includes('text-in-excluded-parent')) { | ||
return []; | ||
} | ||
return [inputUsj]; | ||
} | ||
|
||
let cleanedKids = []; | ||
let cleanedMarkers = excludeMarkers.map(marker => marker.replace(trailingNumPattern, '')); | ||
let thisMarker = 'marker' in inputUsj ? inputUsj.marker.replace(trailingNumPattern, '') : ''; | ||
let thisMarkerNeeded = true; | ||
let innerContentNeeded = true; | ||
excludedParent = false; | ||
|
||
if (cleanedMarkers.includes(thisMarker)) { | ||
thisMarkerNeeded = false; | ||
excludedParent = true; | ||
if (MARKERS_WITH_DISCARDABLE_CONTENTS.includes(thisMarker)) { | ||
innerContentNeeded = false; | ||
} | ||
} | ||
|
||
if ((thisMarkerNeeded || innerContentNeeded) && "content" in inputUsj) { | ||
inputUsj.content.forEach(item => { | ||
let cleaned = excludeMarkersInUsj(item, excludeMarkers, combineTexts, excludedParent); | ||
if (Array.isArray(cleaned)) { | ||
cleanedKids.push(...cleaned); | ||
} else { | ||
cleanedKids.push(cleaned); | ||
} | ||
}); | ||
if (combineTexts) { | ||
cleanedKids = combineConsecutiveTextContents(cleanedKids); | ||
} | ||
} | ||
|
||
if (thisMarkerNeeded) { | ||
let cleanedUsj = { ...inputUsj, content: cleanedKids }; | ||
return cleanedUsj; | ||
} | ||
return innerContentNeeded ? cleanedKids : []; | ||
} | ||
|
||
function includeMarkersInUsj(inputUsj, includeMarkers, combineTexts = true, excludedParent = false) { | ||
if (typeof inputUsj === 'string') { | ||
return excludedParent ? [] : [inputUsj]; | ||
} | ||
let cleanedKids = []; | ||
let cleanedMarkers = includeMarkers.map(marker => marker.replace(trailingNumPattern, '')); | ||
let thisMarker = 'marker' in inputUsj ? inputUsj.marker.replace(trailingNumPattern, '') : ''; | ||
let thisMarkerNeeded = cleanedMarkers.includes(thisMarker) || thisMarker === ''; | ||
let innerContentNeeded = thisMarkerNeeded || MARKERS_WITH_DISCARDABLE_CONTENTS.includes(thisMarker); | ||
|
||
if (innerContentNeeded && "content" in inputUsj) { | ||
inputUsj.content.forEach(item => { | ||
let cleaned = includeMarkersInUsj(item, includeMarkers, combineTexts, !thisMarkerNeeded); | ||
if (Array.isArray(cleaned)) { | ||
cleanedKids.push(...cleaned); | ||
} else { | ||
cleanedKids.push(cleaned); | ||
} | ||
}); | ||
if (combineTexts) { | ||
cleanedKids = combineConsecutiveTextContents(cleanedKids); | ||
} | ||
} | ||
|
||
if (thisMarkerNeeded) { | ||
let cleanedUsj = { ...inputUsj, content: cleanedKids }; | ||
return cleanedUsj; | ||
} | ||
return innerContentNeeded ? cleanedKids : []; | ||
} | ||
|
||
exports.excludeMarkersInUsj = excludeMarkersInUsj; | ||
exports.includeMarkersInUsj = includeMarkersInUsj; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
const {USFMParser} = require("./usfmParser"); | ||
exports.USFMParser = USFMParser; |
Oops, something went wrong.