Skip to content

Commit

Permalink
Add linter and bundler (#8)
Browse files Browse the repository at this point in the history
* Add bundler and linter

* Update opencc-data to 1.0.5

* Remove old speed test

* Add linter to check

* Update README

* Update documentation
  • Loading branch information
sgalal authored Jan 11, 2021
1 parent 48f2195 commit 5363080
Show file tree
Hide file tree
Showing 15 changed files with 1,969 additions and 396 deletions.
17 changes: 17 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
env: {
browser: true,
es2021: true,
node: true,
},
extends: [
'airbnb-base',
],
parserOptions: {
ecmaVersion: 12,
sourceType: 'module',
},
rules: {
'no-restricted-syntax': ['error', 'ForInStatement', 'LabeledStatement', 'WithStatement'],
},
};
38 changes: 15 additions & 23 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages

name: Node.js Package

on:
release:
types: [created]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 14
- run: npm ci
- run: npm test

publish-npm:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 14
registry-url: https://registry.npmjs.org/
- run: npm ci
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: 14
registry-url: https://registry.npmjs.org/
- name: Install bundler
run: npm install -g rollup
- name: Install dependencies
run: npm ci
- name: Bundle
run: npm run bundle
- name: Publish
run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
11 changes: 9 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@ jobs:
- uses: actions/setup-node@v1
with:
node-version: 14.x
- run: npm ci
- name: Install bundler
run: npm install -g rollup
- name: Install dependencies
run: npm ci
- name: Bundle
run: npm run bundle
- name: Test
run: npm test
- name: Speed Test
run: |
test/prepare.sh
node test/speed.js
npm run speedtest
- name: Lint
run: npm run lint
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ node_modules

/test/神雕侠侣.txt
/test/天龙八部.txt

/bundle.js
31 changes: 18 additions & 13 deletions README-en.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ Pure JavaScript implementation of OpenCC

## Import

In HTML file:
In HTML:

```html
<script src="https://cdn.jsdelivr.net/npm/[email protected].6"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected].7"></script>
```

Or in Node.js:
Expand All @@ -19,7 +19,6 @@ const OpenCC = require('opencc-js');
## Usage

```javascript

OpenCC.Converter('hk', 'cn') // Traditional Chinese (Hong Kong) to Simplified Chinese
.then(convert => console.log(convert('漢字,簡體字'))); // output: 汉字,简体字
```
Expand All @@ -33,25 +32,31 @@ The first argument is the source type, the second argument is the destination ty
- Simplified Chinese (Mainland China):`cn`
- Japanese _Shinjitai_`jp`

Trad (Hong Kong, with Hong Kong phrases) is currently not supported.
Traditional Chinese (Hong Kong, with Hong Kong phrases) is currently not supported.

## Custom Converter

```javascript
const convert = OpenCC.CustomConverter({ '香蕉': '🍌️', '蘋果': '🍎️', '': '🍐️' });
console.log(convert('香蕉蘋果梨')); // output: 🍌️🍎️🍐️
const dict = {
'香蕉': 'banana',
'蘋果': 'apple',
'': 'pear',
};
const convert = OpenCC.CustomConverter(dict);
console.log(convert('香蕉 蘋果 梨'));
// outputs: banana apple pear
```

## DOM operation

```javascript
(async () => {
const convert = await OpenCC.Converter('hk', 'cn');
const startNode = document.documentElement; // Convert the whole page
const HTMLConvertHandler = OpenCC.HTMLConverter(convert, startNode, 'zh-HK', 'zh-CN'); // Convert all zh-HK to zh-CN
HTMLConvertHandler.convert(); // Start conversion
HTMLConvertHandler.restore(); // Restore
})()
((async () => {
const convert = await OpenCC.Converter('hk', 'cn');
const startNode = document.documentElement; // Convert the whole page
const HTMLConvertHandler = OpenCC.HTMLConverter(convert, startNode, 'zh-HK', 'zh-CN'); // Convert all zh-HK to zh-CN
HTMLConvertHandler.convert(); // Start conversion
HTMLConvertHandler.restore(); // Restore
})());
```

The conversion is skipped if the class list of a node contains `ignore-opencc`. All child nodes of the node will not be converted.
26 changes: 16 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
在 HTML 中引入:

```html
<script src="https://cdn.jsdelivr.net/npm/[email protected].6"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected].7"></script>
```

或在 Node.js 中引入:
Expand Down Expand Up @@ -39,20 +39,26 @@ OpenCC.Converter('hk', 'cn') // 香港繁體轉簡體
## 自訂轉換器

```javascript
const convert = OpenCC.CustomConverter({ '香蕉': '🍌️', '蘋果': '🍎️', '': '🍐️' });
console.log(convert('香蕉蘋果梨')); // output: 🍌️🍎️🍐️
const dict = {
'香蕉': 'banana',
'蘋果': 'apple',
'': 'pear',
};
const convert = OpenCC.CustomConverter(dict);
console.log(convert('香蕉 蘋果 梨'));
// outputs: banana apple pear
```

## DOM 操作

```javascript
(async () => {
const convert = await OpenCC.Converter('hk', 'cn');
const startNode = document.documentElement; // 轉換整個頁面
const HTMLConvertHandler = OpenCC.HTMLConverter(convert, startNode, 'zh-HK', 'zh-CN'); // 將所有 zh-HK 標籤轉為 zh-CN 標籤
HTMLConvertHandler.convert(); // 開始轉換
HTMLConvertHandler.restore(); // 回到原貌
})()
((async () => {
const convert = await OpenCC.Converter('hk', 'cn');
const startNode = document.documentElement; // 轉換整個頁面
const HTMLConvertHandler = OpenCC.HTMLConverter(convert, startNode, 'zh-HK', 'zh-CN'); // 將所有 zh-HK 標籤轉為 zh-CN 標籤
HTMLConvertHandler.convert(); // 開始轉換
HTMLConvertHandler.restore(); // 回到原貌
})());
```

class list 包含 `ignore-opencc` 的元素會跳過後續的轉換,該節點的所有子節點都不會被轉換。
Loading

0 comments on commit 5363080

Please sign in to comment.