Skip to content

fix: render emphasis next to CJK text (CJK-friendly amendment) - #300

Merged
pchuri merged 1 commit into
pchuri:mainfrom
k-k1:feature/cjk-friendly-emphasis
Nov 16, 2026
Merged

pchuri merged 1 commit into
pchuri:mainfrom
k-k1:feature/cjk-friendly-emphasis

Conversation

@k-k1

@k-k1 k-k1 commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

Pull Request Template

Description

Closes #299

Markdown in Chinese, Japanese or Korean often loses its emphasis. CommonMark's flanking rules check the punctuation on each side of a */**/~~ run. In **「括弧」**の直後, the closing ** follows 」 and precedes a letter, so it cannot close. The page then shows the literal asterisks. Korean hits the same problem whenever a particle follows emphasis that ends in punctuation or code, for example **스크립트(script)**라고, **`npm publish`**를, **(주)**는 and **100%**의. Japanese has the same problem without any spaces: **「括弧」**の直後, これは**私のやりたかったこと。**だから, 税込**¥10,000**で.

This PR applies the CJK-friendly amendment to CommonMark, the rule that markdown-it-cjk-friendly implements, and adds no runtime dependency.

Why a port instead of the package: markdown-it-cjk-friendly 2.x/3.x and its get-east-asian-width dependency are ESM-only. This CLI is CommonJS:

  • Jest cannot require() them before Node 24.9 (I checked on Node 22).
  • Node 20.0–20.18, which engines allows, cannot require() them at runtime.
  • The last CommonJS release (1.3.2) fails to load against markdown-it 15.

So the change is:

  • lib/cjk-friendly-emphasis.js: a CommonJS port of markdown-it-cjk-friendly 3.0.0. It replaces scanDelims on a subclass of md.inline.State, the same way the plugin does. The file keeps that package's MIT notice (Tatsunori Uchino; markdown-it's authors).
  • lib/east-asian-width-data.js: East Asian Width ranges, generated by scripts/generate-east-asian-width.js from get-east-asian-width 1.7.0 through its public API. That package is a devDependency only, so the shipped dependency tree and npm-shrinkwrap.json do not change. The generated file keeps that package's MIT notice.
  • MacroConverter uses it: new MarkdownIt().use(cjkFriendlyEmphasis). That covers convert, create, create-child, update and the other Markdown inputs.

Behaviour:

  • *, ** and ~~ now open and close next to CJK characters.
  • _/__ still never open or close inside a word.
  • Characters are read exactly as markdown-it 15's own scanDelims reads them, so a lone surrogate counts as U+FFFD.
  • One exception applies without CJK text, the same as in the original plugin. A general-use variation selector (U+FE00–U+FE0E) right before a delimiter is looked through to the character it modifies, so input like a**x”︀**b changes.
  • Any other input without CJK next to a delimiter parses as before. README has a short section on this.

A known limitation that predates this PR: text in a page that contains literal emphasis markers is not escaped when the page is read as Markdown. Storage → Markdown → storage already turns <p>**a**</p> and <p>~~x~~</p> into formatting on main. With this change, the same applies next to CJK text. A literal **「…」**の left on a page by the current behaviour will become bold when written back, which is usually what its author meant. Escaping literal markers in renderText would change the output for every user, so I left it out of this PR.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Performance improvement
  • Code refactoring

Testing

  • Tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Beyond the new tests/cjk-friendly-emphasis.test.js, I compared outputs offline:

  • All 652 CommonMark spec examples render identically with and without the rule.
  • The plugin's own testcases are 812 inputs, including Korean, non-BMP, emoji and variation selectors. They render byte-for-byte like markdown-it-cjk-friendly 3.0.0. The only exceptions are two lone-surrogate inputs, where this port matches stock markdown-it instead.
  • The generated data matches get-east-asian-width 1.7.0 at all 1,114,112 code points.
  • With the CJK relaxation removed, the new tests fail.
  • The full jest suite and npx eslint . pass on Node 22.

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • Any dependent changes have been merged and published in downstream modules

Additional Context

We use confluence-cli for Japanese pages, and this is the last gap that keeps us from using its Markdown conversion directly. I'm happy to switch to depending on the package instead, if you'd rather move toward ESM, or to put the rule behind an option.

🤖 Generated with Claude Code

markdown-it follows CommonMark's flanking rules, which decide whether
`*`, `**` and `~~` can open or close from the punctuation next to the
run. In Chinese, Japanese and Korean text written without spaces that
fails: `**「括弧」**の直後` keeps its literal `**` on the page, because
the closing run follows `」` and precedes a letter.

Apply the CJK-friendly amendment (tats-u/markdown-cjk-friendly) through
a CommonJS port of markdown-it-cjk-friendly 3.0.0 in
lib/cjk-friendly-emphasis.js. The package and its get-east-asian-width
dependency are ESM-only, which this CommonJS CLI cannot require under
Jest or on Node 20 before 20.19. The East Asian Width ranges are
generated into lib/east-asian-width-data.js by
scripts/generate-east-asian-width.js; get-east-asian-width is a
devDependency only, so the shipped dependency tree is unchanged. Both
files keep the MIT notices of the code and data they derive from.

The port reads characters as markdown-it 15's own scanDelims does (a
lone surrogate reads as U+FFFD). `_` still never opens or closes inside
a word. As in the original, a general-use variation selector right
before a delimiter run is looked through to the character it modifies,
so a few inputs such as `a**x”︀**b` change even without CJK; any
other input without a CJK character next to a delimiter parses as
before.

Verified:
- every CommonMark spec example (652) renders identically with and
  without the rule;
- the plugin's own testcases (812 inputs incl. Korean, non-BMP, emoji
  and variation selectors) render byte-for-byte like
  markdown-it-cjk-friendly 3.0.0, except two lone-surrogate inputs where
  this port matches stock markdown-it instead;
- the generated data matches get-east-asian-width 1.7.0 at all
  1,114,112 code points;
- new tests fail (10 of 23 at the time) with the CJK relaxation
  removed; the full jest suite and eslint pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pchuri
pchuri merged commit d7ca4cb into pchuri:main Nov 16, 2026
6 checks passed
github-actions Bot pushed a commit that referenced this pull request Nov 16, 2026
## [2.31.2](v2.31.1...v2.31.2) (2026-11-16)

### Bug Fixes

* render emphasis next to CJK text (CJK-friendly amendment) ([#300](#300)) ([d7ca4cb](d7ca4cb))
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 2.31.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Emphasis next to Korean/Japanese/Chinese text stays literal after Markdown conversion

2 participants