Skip to main content

markdown-it-py plugin that makes CommonMark friendly to Japanese/Chinese/Korean: no spurious spaces at soft line breaks, working emphasis next to full-width punctuation.

Project description

mdit-py-cjk-friendly

日本語 | English | 繁體中文 | 한국어

markdown-it-py を 日本語・中国語・韓国語 (CJK) フレンドリーにするプラグイン。

pip install mdit-py-cjk-friendly
from markdown_it import MarkdownIt
from mdit_py_cjk_friendly import cjk_friendly

md = MarkdownIt("commonmark").use(cjk_friendly)

md.render("これは**「重要」**です。")
# <p>これは<strong>「重要」</strong>です。</p>

md.render("これは長い文章なので\n途中で改行しています。")
# <p>これは長い文章なので途中で改行しています。</p>   (不要な空白が入らない)

直る問題

CommonMark は空白で単語を区切る言語を前提に設計されており、 CJK テキストでは有名な問題が2つ起きる:

  1. 文中改行が空白になる — Markdown ソースを日本語文の途中で改行すると、 描画後に不要な半角スペースが入る。本プラグインは改行の前後がともに CJK 文字なら詰めて結合する (英文の折り返しは従来どおり)。
  2. 全角約物に隣接した強調が効かない**「重要」**です は CommonMark の flanking 規則で不成立になりリテラルの ** が残る。本プラグインは flanking 判定で CJK 文字を punctuation 互換として扱い、パーサレベルで 解決する (コードスパン・コードブロックには影響しない)。

強調の挙動は CommonMark CJK-friendly 仕様ドラフト の考え方に従った、markdown-it-py 向けの独立・簡易実装です (JavaScript は本家プロジェクトのプラグインをどうぞ)。

ふりがな(ルビ)— オプション

構文の追加になるため、別プラグイン ruby として opt-in で提供する (でんでんマークダウン形式):

from mdit_py_cjk_friendly import cjk_friendly, ruby

md = MarkdownIt("commonmark").use(cjk_friendly).use(ruby)
md.render("{漢字|かんじ}")          # → <ruby>漢字<rp>(</rp><rt>かんじ</rt><rp>)</rp></ruby>
md.render("{東京|とう|きょう}")      # → モノルビ (読みの数=文字数のとき)
  • 読みの数が文字数と合わない・空の要素がある場合は変換しない(推測しない)
  • \{ でエスケープ。コードスパン・コードブロック内は変換されない
  • <rp> 括弧つきで出力するので、ルビ非対応の環境では「漢字(かんじ)」に落ちる

補足

  • 効くのは .use(cjk_friendly) したパーサだけ。同一プロセスの他の MarkdownIt インスタンスは上流と完全に同じ挙動を保つ
  • markdown-it-py 2.x / 3.x 対応

ライセンス

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mdit_py_cjk_friendly-0.2.0.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mdit_py_cjk_friendly-0.2.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file mdit_py_cjk_friendly-0.2.0.tar.gz.

File metadata

  • Download URL: mdit_py_cjk_friendly-0.2.0.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mdit_py_cjk_friendly-0.2.0.tar.gz
Algorithm Hash digest
SHA256 f95c9f0daa151878138135d521dcf7c5c60f6c782d5d7c1fc6966816813b1d4a
MD5 70295ce5ed31adafb59a4939ba4c5564
BLAKE2b-256 7d714d3627179695405df3b3c023a25fc57643b2c84888626c5c2644b4f4ae12

See more details on using hashes here.

Provenance

The following attestation bundles were made for mdit_py_cjk_friendly-0.2.0.tar.gz:

Publisher: publish.yml on aiseed-dev/mdit-py-cjk-friendly

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mdit_py_cjk_friendly-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for mdit_py_cjk_friendly-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 35d0cbd78007bd50d7d79a349c5444c250332eed94cafcb4ff5fba97418c1995
MD5 6abfd65894f9457853be674692a957a6
BLAKE2b-256 ccb4d61e4d651cfc559d9725a89fa44b04eae564a119107374fa4990ae8edf1e

See more details on using hashes here.

Provenance

The following attestation bundles were made for mdit_py_cjk_friendly-0.2.0-py3-none-any.whl:

Publisher: publish.yml on aiseed-dev/mdit-py-cjk-friendly

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page