1.5.1 • Published 1 year ago

prettier-plugin-md-nocjsp v1.5.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Prettier Makdown nocjsp (do NOt insert SPaces between Chinese or Japanese characters and alphabets or numbers) plugin

Prettier Logo Logo

CI (master) CI (Release) npm version

This plugin prevents Prettier from inserting spaces between Chinese or Japanese letters (e.g. Han aka Kanji & Hiragana & Katakana) and alphabets or numbers in your Markdown documents.

🇯🇵

このプラグむンは、Prettierによっお、Markdown文曞䞭内の挢字仮名ず英数字の間に半角スペヌスが挿入されないようにしたす。

Target versions

The modification by this plugin has been merged into the branch for next major version (3.x) of Prettier. You do not have to install this plugin with Prettier 3.x, and you are required to uninstall this plugin after you upgrade Prettier to 3.x.
本プラグむンによる修正点は、Prettierの次期メゞャヌバヌゞョン3.x甚ブランチにマヌゞされたした。このプラグむンをPrettier 3.xず䞀緒にむンストヌルする必芁はなく、Prettierのバヌゞョンを3.xに䞊げたのならばこのプラグむンは甚枈みのためアンむンストヌルする必芁がありたす。

How to use

First, run yarn init or npm init in the project root directory unless you have already done or are going to install this plugin globally.
たず、プロゞェクトルヌトディレクトリ䞊で、yarn initたたはnpm initを実行しおください。すでにやっおいる堎合や、グロヌバル環境にむンストヌル予定の堎合は陀く

Next, install Prettier by:
次に、Prettierを次のコマンドでむンストヌルしおください。

yarn add -D prettier

# or (↓ when you prefer npm to yarn)

npm install -D prettier

You can install and try this plugin by:
このプラグむンは次のコマンドでむンストヌル及びお詊しが可胜です。

# or `npm install -D prettier-plugin-md-nocjsp`
yarn add -D prettier-plugin-md-nocjsp

yarn prettier --parser markdown-nocjsp *.md | less

If you like the output by this plugin, you may want to add .prettierrc (or .prettier.yml) like the below to your project root:
出力が気に入りたしたら、次のような.prettierrcたたは.prettierrc.ymlをプロゞェクトルヌトに远加しおください。

# *snip*
overrides:
  - files:
      - "*.md"
      - README
    options:
      parser: markdown-nocjsp
  - files:
      - "*.mdx"
    options:
      parser: mdx-nocjsp

Then you can format documents using this plugin just by:
次のコマンドさえ打おば、本プラグむンを甚いおMarkdown文曞を敎圢するこずができたす。

# format only Markdown documents in the current directory
#
# `yarn` should be removed in `scripts` in `package.json`
yarn prettier -w *.md

You may want to check whether all documents have been formatted by:
次のようなコマンドで、党文曞が敎圢されおいるかどうかを確認できたす。

yarn prettier -l *.md

Help! Prettier put extra spaces and messed up my document!

Since the version 1.4.0, this plugin has provided the private option named quickFix that allows us to remove extra spaces between han(kanji)/kana and alphanumerics forced into your document by the (plain) Prettier.
バヌゞョン1.4.0から、本プラグむンはquickFixずいう名前の独自オプションを実装しおいたす。これは、玠のPrettierによりねじ蟌たれた挢字・仮名ず英数字の間の䜙蚈なスペヌスを取り陀きたす。

To use this option, edit .prettierrc like the following (i.e. add quickFix: true) and re-format your document just once.
このオプションを利甚するには、.prettierrcを以䞋のように線集quickFix: trueを加えるしお、1回だけ再フォヌマットしたす。

# *snip*
overrides:
  - files:
      - "*.md"
      - README
    options:
      parser: markdown-nocjsp
      quickFix: true
  - files:
      - "*.mdx"
    options:
      parser: mdx-nocjsp
      quickFix: true

After re-format, you do not have to keep the quickFix: true option that you have just added any longer and can remove it. 再フォヌマットが枈んだら、远加したquickFix: trueオプションは䞍芁なので削陀しおください。

Note that this cannot be specified from command line options. You are required to use e.g. .prettier instead.
泚意点ずしお、コマンドラむンオプションから指定するこずはできたせん。代わりに.prettierrcなどを利甚する必芁がありたす。

Also, as the name "quick-fix" suggests, the behavior of this option is not perfect. There is a risk of removing even necessary spaces. After applying this option, please look over the formatted document carefully.
たた、「応急措眮」quick-fixの名が衚すように、このオプションの動䜜は完璧ではありたせん。必芁なスペヌスたで削陀しおしたうおそれがありたす。このオプションを適甚した埌は、敎圢した文曞によく目を通しおください。

proseWrap value

This plugin mainly supports only the "preserve" value (default) for the proseWrap option. However, from 1.5.0, this plugin supports the other values "always" and "never". The behavior when these additional values are given is based on that of the incoming upstream Prettier 3+.
本プラグむンはproseWrapオプションにおいお基本的に"preserve"の倀のみに察応しおいたす。しかし、1.5.0から、他の倀、すなわち"always"・"never"の倀にも察応しおいたす。その時の挙動は、Prettier 3以䞊将来利甚可胜のものに準じおいたす。

Formatting Examples

See the Markdown documents in the assets directory.
assetsディレクトリ内のMarkdown文曞を埡芧ください。

Why this plugin is needed

Here is an example of markdown document:
Markdown文曞の䟋を挙げたす。

# 第1章

Without this plugin, Prettier will insert spaces (U+0020) like as follows:
このプラグむンがないず、Prettierは、次のように半角スペヌスを入れおしたしたす。

# 第 1 章

Indeed, Japanese and Chinese typography stipulate that spaces similar to those inserted by Prettier.
確かに、日本語・䞭囜語組版では、Prettierが挿入するものず䌌たスペヌスが挿入されたす。

Let us refer to the official text style guide lines. They say:
公匏の組版芏則を芋おみるず次のようになっおいたす。

Japanese:

W3C:

Inter-character spacing, between hiragana (cl-15), katakana (cl-16) or ideographic characters (cl-19) and Western characters or European numerals, is quarter em spacing (see Figure 102). The issue as to whether the quarter em spacing can be used for line end adjustment or not is discussed in § 3.8.2 Reduction and Addition of Inter-Character Spacing 詰める凊理ず空ける凊理 and § 3.8.4 Procedures for Inter-Character Space Expansion 空ける凊理の優先順䜍 .

欧字・アラビア数字の前埌に配眮される平仮名cl-15片仮名cl-16又は挢字等cl-19ずの字間は四分アキずするFigure 102この四分アキを行の調敎凊理に䜿甚する堎合の詳现に぀いおは§ 3.8.2 Reduction and Addition of Inter-Character Spacing 詰める凊理ず空ける凊理 及び§ 3.8.4 Procedures for Inter-Character Space Expansion 空ける凊理の優先順䜍 を参照

https://www.w3.org/TR/jlreq/#handling_of_western_text_in_japanese_text_using_proportional_western_fonts

“one quarter em” means quarter size of full-width. (JIS Z 8125)
“one quarter em” means amount of space that is a quarter of an em space in size.
四分 = 党角の4分の1の長さJIS Z 8125
四分アキ = 四分の空き量

https://www.w3.org/TR/jlreq/#term.quarter-em https://www.w3.org/TR/jlreq/#term.quarter-em-space

参考欧文間隔
FYI: Western word space

Note that Western word space (cl-26) is a one third em space, in principle, except at line head, line head of warichu, line end and line end of warichu.

なお欧文間隔cl-26は䞉分アキを原則ずする

https://www.w3.org/TR/jlreq/#mixed_text_composition_in_horizontal_writing_mode

“one third em” means one third of the full-width size. (JIS Z 8125)
“one third em space” means amount of space that is one third size of em space.
䞉分 = 党角の3分の1の長さJIS Z 8125
䞉分アキ = 䞉分の空き量

https://www.w3.org/TR/jlreq/#term.one-third-em https://www.w3.org/TR/jlreq/#term.one-third-em-space

JIS X 4051:2004:

3.b.61 四分アキしぶあきしぶんあき 空き量を党角アキの1/4ずするもの。
One quarter em: space/width with 1/4 of full-width space.
4.6d 暪曞きでは和文ず欧文ずの間の空き量は四分アキを原則ずする。
In horizontal writing, the space between Japanese and European text should be one quarter em, as a rule.
(Translation based on DeepL)

参考欧文間隔
FYI: Western word space

3.b6 欧文間隔 欧文の単語間の空きを衚珟する文字。
“欧文間隔” means a character that represents the space between words in European text.

4.7a 欧文間隔は䞉分アキを原則ずする。
The space between European words should be one third em, as a rule.

https://kikakurui.com/x4/X4051-2004-02.html

Chinese (W3C):

In principle, there is tracking or spacing between an adjacent Han character and a Western character of up to one quarter of a Han character width, except at the line start or end.
暪排时西文䜿甚比䟋字䜓阿拉䌯数字则垞甚比䟋字䜓或等宜字䜓。原则䞊汉字䞎西文字母、数字闎䜿甚䞍倚于四分之䞀䞪汉字宜的字距或空癜。䜆西文出现圚行銖或行尟时则无须加入空癜。
NOTE: Another approach is to use a Western word space (U+0020 SPACE), in which case the width depends on the font in use.
或可䜿甚西文词闎空栌U+0020 SPACE 其宜床随䞍同字䜓有所变化。

https://www.w3.org/TR/clreq/#mixed_text_composition_in_horizontal_writing_mode

The following table summarizes the above quotes. It shows that the widths of spaces between Han and western characters, and between western words in both languages are different.
䞊をたずめるず、䞋衚のようになりたす。和欧文字間・英単語間のスペヌスの幅が異なるこずが芋お取れたす。

JapaneseChinese
文␣A1/4 of “文”1/4 of “文”
Substitute for U+0020No stipulationsOne of the options
FYI: A␣B1/3 of “文”Depends on fonts

Moreover, in text style guide lines for both languages (especially Japanese), using U+0020 is not the only rule.
さらに、半角スペヌスを䜿甚するこずは、䞡蚀語特に日本語では唯䞀絶察の芏則ではありたせん。

The image below shows that the spacing between hiragana or kanji and alphabets is 1/4 of the length of the former and that it is different from the spacing between English words. (used: Microsoft Word + Yu Mincho)
次の画像は、 平仮名・挢字ず英文字の間が四分アキであり、英単語間のスペヌスずは幅が異なるこずを衚しおいたす。

Japanese rendering in Word

3 underlined Hiraganas “あ” have the same width as 12 Han-alphabet spaces (do not consider those around “m”; they appear in both lines)
䞋線が匕いおある平仮名「あ」3文字は、挢字・アルファベット間のスペヌス12個分に盞圓したす。「m」呚蟺のスペヌスは勘定したせん。䞡方の行に珟れるからです。

Space width in Word

In MS Word, the width of a space between Western words is not one quarter or third of that of Hiragana or Han. (= Chinese rule)
MS Wordでは、英単語間のアキは䞉分でも四分でもありたせん。䞭囜語の芏則のようです

Even in Chinese, the act of inserting U+0020 is not the only standard, and takes away the option of document viewers and converters to insert spaces a quarter of the width of Han characters, which is unacceptable. The spacing between Han characters and the alphabet should be left to them. At least Prettier must not manage spacing in place of them (idealy).
䞭囜語であっおも、半角スペヌスを挿入する行為は唯䞀の暙準ではなく、文曞衚瀺゜フトや倉換゜フトが党角の1/4幅四分のアキを挿入できなくしおしたいたす。圓然容認できるものではありたせん。挢字仮名・アルファベット間のアキはそれらの゜フトに任せるべきです。最䜎でも、Prettierはそれらを差し眮いおスペヌスを管理しおはなりたせんならないのが理想的です。

As described above, U+0020 must not be inserted between Han (or hiragana or katakana) and alphanumerics. Then, can we remove those that have already injected easily? NO!
このように、挢字仮名・英数字の間に半角スペヌスは挿入しおはなりたせん。ならば、既に挿入されおしたったものは簡単に陀去できるのでしょうか無理

The following sentence is correct; we must not remove any spaces in it.
次の英文は正しく、どのスペヌスも削陀しおはなりたせん。

䜜る means “make” in Japanese.

As you can see, formatter like Prettier cannot tell if the spaces should be removed and help leaving them. Once it did, it cannot be undone anymore.
このように、Pretierのようなフォヌマットは半角スペヌスを陀去するかはわからず、攟眮する他ありたせん。䞀回やったら、二床ず戻せたせん。

TL;DR: inserting spaces (but not U+0020 itself) is the job of viewers and such, not formatters like Prettier!!! PRETTIER MUST """NEVER""" BREAK DOCUMENTS BY DOING SUCH A THING!!!! ONCE IT DO, IT CAN """NEVER""" UNDO!!!
芁点: アキ半角スペヌス自䜓ではないを挿入する行為は衚瀺゜フトの責務であり、Prettierのようなフォヌマッタの責務ではありたせんPrettierがこんなこずをやらかすこずにより、文曞を砎壊する行為は到底認められるものではありたせん。

Use this repository directly

Clone and build this repository by:

git clone --recursive https://github.com/tats-u/prettier-plugin-md-nocjsp.git
cd prettier-plugin-md-nocjsp
yarn install
yarn build

Note that you must make sure to add a --recursive option to git clone (See “Use this repository directly”) because this repository uses the Git submodule feature.

Then you can try it by:

yarn prettier test.md --parser markdown-nocjsp --plugin path/to/prettier-plugin-md-nocjsp

You can test it without yarn build:

yarn prettier test.md --parser markdown-nocjsp --plugin path/to/prettier-plugin-md-nocjsp/src/index.js

IMPORTANT: you must not run yarn install in src/prettier/, or you will get the following error after running yarn build && yarn test:

[error] Could not dynamically require "./data.generated/Script/Han". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.

This is because Rollup tries to import JSON data in the package unicode-regex in ./src/prettier/node_modules/ duplicated to that in ./node_modules/ dynamically. To fix this error, you must erase ./src/prettier/node_modules/ by:

rm -re -fo .\src\prettier\node_modules

↑Windows (PowerShell) / Unix↓

rm -rf ./src/prettier/node_modules

After this, make sure to re-run yarn build to fix the broken ./dist/main.js.

License

MIT License (same as Prettier itself)

This plugin reuses Prettier's code.

1.5.1

1 year ago

1.5.0

1 year ago

1.4.0

2 years ago

1.3.0

2 years ago

1.2.0

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago