Skip to content

Commit 742b595

Browse files
committed
Publish to npm
1 parent b3861ed commit 742b595

File tree

12 files changed

+2015
-18
lines changed

12 files changed

+2015
-18
lines changed

.github/workflows/publish.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Publish to NPM
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
- main
8+
9+
jobs:
10+
build-and-publish:
11+
runs-on: ubuntu-latest
12+
permissions:
13+
contents: read
14+
id-token: write
15+
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@v4
18+
19+
- name: Set up Node.js
20+
uses: actions/setup-node@v4
21+
with:
22+
node-version: '20'
23+
registry-url: 'https://registry.npmjs.org'
24+
25+
- name: Install dependencies
26+
run: npm install
27+
28+
- name: Build
29+
run: npm run build
30+
31+
- name: Publish to NPM
32+
run: npm publish --provenance
33+
env:
34+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
.vscode/
2+
node_modules/
3+
dist/

README.md

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,42 @@
1-
# opencc-rust-lib
1+
# OpenCC Rust Library
22

3-
[polyproline/opencc-rust](https://github.com/polyproline/opencc-rust) 封裝為 ES6 模組,使 OpenCC 可以直接被瀏覽器引用。
3+
這是基於 Rust + WebAssembly 的 OpenCC 中文轉換工具。
4+
5+
## 安裝
6+
7+
```sh
8+
npm install opencc-rust
9+
```
10+
11+
## 使用方式
12+
13+
### ESM/現代瀏覽器/Node.js
14+
15+
```js
16+
import { initOpenccRust, getConverter } from 'opencc-rust';
17+
// 或是 import { initOpenccRust, getConverter } from 'https://cdn.jsdelivr.net/npm/opencc-rust/dist/opencc-rust.mjs';
18+
19+
await initOpenccRust();
20+
const converter = getConverter();
21+
const result = await converter.convert('漢字');
22+
```
23+
24+
### UMD/CDN
25+
26+
```html
27+
<script src="https://cdn.jsdelivr.net/npm/opencc-rust/dist/opencc-rust.umd.js"></script>
28+
<script>
29+
OpenccRust.initOpenccRust().then(() => {
30+
const converter = OpenccRust.getConverter();
31+
converter.convert('漢字').then(result => {
32+
console.log(result);
33+
});
34+
});
35+
</script>
36+
```
37+
## 授權
38+
MIT
39+
40+
## 致謝
41+
42+
[polyproline/opencc-rust](https://github.com/polyproline/opencc-rust)
Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Converter, ConverterBuild, init } from "./opencc-rust-lib.mjs"
1+
import { Converter, ConverterBuild, init } from "./opencc-rust-lib.mjs";
22

33
const RESOURCES = {
44
opencc_wasm: "https://cdn.jsdelivr.net/gh/polyproline/opencc-wasm@main/opencc_gc.wasm",
@@ -47,21 +47,29 @@ const CONVERTION_MAP = {
4747

4848
const SELECTED_DICT = CONVERTION_MAP.ToTaiwan;
4949

50-
const wasm = await fetch(RESOURCES.opencc_wasm);
51-
await init(wasm);
50+
let _converter = null;
5251

53-
const build = ConverterBuild.new();
54-
for (let dict of SELECTED_DICT) {
55-
if (Array.isArray(dict)) {
56-
for(let d of dict) {
57-
build.adddict(await fetch(RESOURCES[d]).then(response => response.text()));
52+
async function initOpenccRust() {
53+
const wasm = await fetch(RESOURCES.opencc_wasm);
54+
await init(wasm);
55+
56+
const build = ConverterBuild.new();
57+
for (let dict of SELECTED_DICT) {
58+
if (Array.isArray(dict)) {
59+
for(let d of dict) {
60+
build.adddict(await fetch(RESOURCES[d]).then(response => response.text()));
61+
}
62+
} else {
63+
build.adddict(await fetch(RESOURCES[dict]).then(response => response.text()));
5864
}
59-
} else {
60-
build.adddict(await fetch(RESOURCES[dict]).then(response => response.text()));
65+
build.group();
6166
}
62-
build.group();
67+
_converter = build.build();
6368
}
6469

65-
const converter = build.build();
70+
function getConverter() {
71+
if (!_converter) throw new Error("You must call and await initOpenccRust() first.");
72+
return _converter;
73+
}
6674

67-
export { converter };
75+
export { initOpenccRust, getConverter };

index.html

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,21 @@
4646

4747
</style>
4848
<script type="module">
49-
import { converter } from "./js/opencc-rust.mjs"
49+
import { initOpenccRust, getConverter } from "./dist/opencc-rust.mjs";
50+
51+
let converter;
52+
(async () => {
53+
await initOpenccRust();
54+
converter = getConverter();
55+
})();
5056

5157
window.convert = async function() {
52-
const text = document.getElementById("text").value
58+
if (!converter) {
59+
alert("OpenCC 尚未初始化完成,請稍後再試。");
60+
return;
61+
}
62+
const text = document.getElementById("text").value;
5363
const converted = await converter.convert(text);
54-
5564
document.getElementById("result").value = converted;
5665
}
5766
</script>

package.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "opencc-rust",
3+
"version": "1.1.0",
4+
"description": "OpenCC Rust WebAssembly/JS Converter Library",
5+
"main": "dist/opencc-rust.mjs",
6+
"module": "dist/opencc-rust.mjs",
7+
"exports": {
8+
".": "./dist/opencc-rust.mjs"
9+
},
10+
"files": [
11+
"dist/"
12+
],
13+
"keywords": [
14+
"opencc",
15+
"rust",
16+
"wasm",
17+
"converter",
18+
"chinese"
19+
],
20+
"author": "",
21+
"license": "MIT",
22+
"repository": {
23+
"type": "git",
24+
"url": "https://github.com/lekoOwO/opencc-rust-lib"
25+
},
26+
"scripts": {
27+
"build:umd": "rollup -c",
28+
"build:esm": "npx cpx \"src/**/*\" dist/",
29+
"build": "npm run build:esm && npm run build:umd"
30+
},
31+
"devDependencies": {
32+
"@rollup/plugin-node-resolve": "^16.0.1",
33+
"cpx": "^1.5.0",
34+
"rollup": "^4.43.0"
35+
}
36+
}

0 commit comments

Comments
 (0)