Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

pangu

vinta138.4kMIT4.0.7TypeScript support: definitely-typed

Paranoid text spacing for good readability, to automatically insert whitespace between CJK (Chinese, Japanese, Korean) and half-width characters (alphabetical letters, numerical digits and symbols).

pangu, text-spacing, spacing, text, html, typesetting, readability, cli, chinese, japanese, korean, obsessive-compulsive-disorder, ocd, paranoia

readme

為什麼你們就是不能加個空格呢?

如果你跟我一樣,每次看到網頁上的中文字和英文、數字、符號擠在一塊,就會坐立難安,忍不住想在它們之間加個空格。這個外掛(支援 Chrome 和 Firefox)正是你在網路世界走跳所需要的東西,它會自動替你在網頁中所有的中文字和半形的英文、數字、符號之間插入空白。

漢學家稱這個空白字元為「盤古之白」,因為它劈開了全形字和半形字之間的混沌。另有研究顯示,打字的時候不喜歡在中文和英文之間加空格的人,感情路都走得很辛苦,有七成的比例會在 34 歲的時候跟自己不愛的人結婚,而其餘三成的人最後只能把遺產留給自己的貓。畢竟愛情跟書寫都需要適時地留白。

與大家共勉之。

Installation

For Users

For Developers

Usage

$ npm install pangu --save
# or
$ yarn add pangu

Browser

Files are located in ./node_modules/pangu/dist/browser/.

<head>
  <script src="pangu.min.js"></script>
</head>
<script>
  const text = pangu.spacing("當你凝視著bug,bug也凝視著你");
  // text = '當你凝視著 bug,bug 也凝視著你'

  pangu.spacingElementById('main');
  pangu.spacingElementByClassName('comment');
  pangu.spacingElementByTagName('p');

  document.addEventListener('DOMContentLoaded', () => {
    // listen to any DOM change and automatically perform spacing via MutationObserver()
    pangu.autoSpacingPage();
  });
</script>

pangu.js is also available on jsDelivr and cdnjs:

<script src="https://cdn.jsdelivr.net/npm/pangu@4.0.7/dist/browser/pangu.js"></script>
<script src="https://cdn.jsdelivr.net/npm/pangu@4.0.7/dist/browser/pangu.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/pangu/4.0.7/pangu.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pangu/4.0.7/pangu.min.js"></script>

Node.js

Learn more on npm.

const pangu = require('pangu');

const text = pangu.spacing('與PM戰鬥的人,應當小心自己不要成為PM');
// text = '與 PM 戰鬥的人,應當小心自己不要成為 PM'

pangu.spacingFile('/path/to/text.txt', (err, data) => {
  console.log(data);
});

pangu.spacingFile('/path/to/text.txt')
  .then((data) => {
    console.log(data);
  })
  .catch((err) => {
    console.error(err);
  });

const data = pangu.spacingFileSync('/path/to/text.txt');

You SHOULD NOT use pangu.js directly to spacing Markdown documents, this library is specially designed for HTML webpages and plain texts without any markup language. See issue #127.

CLI

$ pangu "不能信任那些Terminal或Editor用白底的人"
不能信任那些 Terminal 或 Editor 用白底的人

$ pangu --help
usage: pangu [-h] [-v] [-t] [-f] text_or_path

pangu.js -- Paranoid text spacing for good readability, to automatically insert whitespace
between CJK and half-width characters (alphabetical letters, numerical digits and symbols).

positional arguments:
  text_or_path   the text or file path to perform spacing

optional arguments:
  -h, --help     show this help message and exit
  -v, --version  show program's version number and exit
  -t, --text     specify the input value is a text
  -f, --file     specify the input value is a file path

Testing

You need to install Node.js.

$ git clone git@github.com:vinta/pangu.js.git && cd pangu.js
$ npm install
$ npm run test

License

Released under the MIT License.

Author

changelog

History

v4.0.7 / 2019-02-15

  • 修正 “ ” 的問題

v4.0.6 / 2019-02-04

  • 修正 , 的問題
  • 新增 pangu 指令,詳見 cli.js

v4.0.5 / 2019-01-30

  • 修正 <pre> 的問題

v4.0.4 / 2019-01-29

  • 修正 Bilibili 影片會消失的問題

v4.0.3 / 2019-01-29

  • 修正 node.isContentEditable 的問題

v4.0.2 / 2019-01-29

  • 修正 <code> 的問題
  • 修正 <i> 的問題
  • 修正 的問題

v4.0.0 / 2019-01-27

  • 各位觀眾,Paranoid Text Spacing 演算法 v4!
  • 大幅地改進 Chrome extension 的效能,使用 MutationObserverdebounce
  • 忍痛拿掉「空格之神顯靈了」
  • 修正 Pangu.spacingText() 的 error callback
  • 新增 BrowserPangu.autoSpacingPage()

v3.3.0 / 2016-12-28

  • 修個 bug 好過年
  • 修正在 Twitter 上跟 Buffer 一起使用時會隨機出現的錯誤問題

v3.2.1 / 2016-06-26

  • 又他媽改善效能問題

v3.2.0 / 2016-06-26

  • 修正效能問題

v3.1.1 / 2016-06-26

  • 剛吃完烤肉來改進一下 Paranoid Text Spacing 演算法

v3.1.0 / 2016-06-25

  • NodePangu 新增 spacingFile(),支援 callback 與 promise
  • NodePangu 新增 spacingFileSync()

v3.0.0 / 2016-01-10

  • Isomorphic!

v2.5.6 / 2015-05-13

  • 大家好,很抱歉這麼快又跟大家見面了

v2.5.5 / 2015-05-13

  • 持續改進 Paranoid Text Spacing 演算法

v2.5.1 / 2015-05-11

  • 再次改進 Paranoid Text Spacing 演算法

v2.5.0 / 2015-05-11

  • 改進 Paranoid Text Spacing 演算法

v2.4.2 / 2014-12-10

  • 修正 Facebook 留言框的空格錯位

v2.4.1 / 2014-12-10

  • 修正 <title> 網頁標題的加空格
  • 修正 ' 單引號的加空格

v2.4.0 / 2014-12-08

  • 改進效能
  • 完善對雙引號的處理
  • 修正 Popup Page 的 CSS 問題

v2.3.4 / 2014-03-01

  • 再度完善 Paranoid Text Spacing 演算法
  • 修正 Options Page 的小錯誤

v2.3.3 / 2014-02-16

  • 完善 Paranoid Text Spacing 演算法

v2.3.2 / 2014-02-12

  • 心血來潮,加個版本號!

v2.3.1 / 2014-02-10

  • 真的不會對 <code><pre> 裡的文字加空格了

v2.3.0 / 2014-02-08

  • 威力加強版!
  • 解決特定情況下在同一個地方會一直加空格的問題

v2.2.3 / 2014-02-07

  • 不會在 Google+ 的輸入框裡加空格
  • 記事本不懂 Vim 的黑

v2.2.2 / 2014-02-07

  • 銀河大客車指南!

v2.2.1 / 2014-02-05

  • 改進 Paranoid Text Spacing 演算法

v2.1.2 / 2014-02-01

  • 不會對 <textarea> 裡的文字加空格!

v2.1.1 / 2014-01-31

  • 不對 _ 加空格
  • | 加空格
  • 新增 Popup Page
  • 空格之神 姍姍來遲

v2.1.0 / 2014-01-29

  • 解決在 Facebook、Twitter、QQ 空间、百度贴吧等網站輸入文字時游標會亂衝的問題
  • 支援 file:/// 開頭的網頁

v2.0.2 / 2014-01-26

  • 遇到 <br> 就不加空格

v2.0.1 / 2014-01-25

  • 拿掉 console.log()
  • 修正 Option Page footer 裡的超連結

v2.0.0 / 2014-01-24

  • 新年新氣象,翻新 Option Page 的 UI 和 Icon
  • 網址黑白名單可以使用 Chrome 的同步功能(Chrome Storage API)
  • 修正在 Gmail 中加空格的問題
  • 改善效率問題

v1.8 / 2013-09-17

  • 修正在 Google Docs 中游標錯位的問題
  • 網址黑、白名單支援 // 前綴

v1.7 / 2012-04-08

  • 減少 Chrome extension 的大小

v1.6 / 2012-04-04

  • 感謝 @jiefoxi,現在英文超連結和中文之間也會加上空格了

v1.5 / 2012-04-04

  • 新增 Firefox 版本
  • 目前支援「中文(繁體、簡體)」、「日文(漢字、平假名、片假名)」加空格
  • 修正網址黑白名單沒有作用的問題

v1.3 / 2012-03-20

  • 根據 @Fenng 的回報,修正了「新浪微博的 @ 符號之後不要加空格」的問題
  • 新增「簡體中文」語系

v1.0 / 第一份工作的某一個下班日

  • Paranoid Text Spacing 演算法橫空出世