prettier-plugin-md-nocjsp v1.5.1
Prettier Makdown nocjsp (do NOt insert SPaces between Chinese or Japanese characters and alphabets or numbers) plugin
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 空ããåŠçã®åªå é äœ ãåç §ïŒïŒ
â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.
äžããŸãšãããšãäžè¡šã®ããã«ãªããŸããå欧æåéã»è±åèªéã®ã¹ããŒã¹ã®å¹
ãç°ãªãããšãèŠãŠåããŸãã
Japanese | Chinese | |
---|---|---|
æâ£A | 1/4 of âæâ | 1/4 of âæâ |
Substitute for U+0020 | No stipulations | One of the options |
FYI: Aâ£B | 1/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)
次ã®ç»åã¯ã 平仮åã»æŒ¢åãšè±æåã®éãååã¢ãã§ãããè±åèªéã®ã¹ããŒã¹ãšã¯å¹
ãç°ãªãããšãè¡šããŠããŸãã
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
ãåšèŸºã®ã¹ããŒã¹ã¯åå®ããŸãããäž¡æ¹ã®è¡ã«çŸããããã§ããïŒ
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.