markdown-tree-parser icon indicating copy to clipboard operation
markdown-tree-parser copied to clipboard

CJK characters are stripped from markdown headings

Open circusvoid opened this issue 1 year ago • 2 comments

Problem

When parsing markdown files with Chinese, Japanese, or Korean (CJK) headings, all non-ASCII characters are stripped due to the current implementation of sanitizeText() in bin/md-tree.js.

Example:

sanitizeText("标题一") // => ''

This makes it difficult to use non-English markdown documents with this tool, as all CJK headings are reduced to empty or meaningless slugs.


Proposed Fix

Update the regular expression to preserve common CJK character ranges:

- .replace(/[^a-z0-9\s-]/g, '')
+ .replace(/[^a-z0-9\u4e00-\u9fff\u3040-\u309f\u30a0-\u30ff\uac00-\ud7af\s-]/g, '')

This will allow:

  • Chinese characters: \u4E00–\u9FFF
  • Hiragana: \u3040–\u309F
  • Katakana: \u30A0–\u30FF
  • Hangul syllables: \uAC00–\uD7AF

Benefit

This change improves multilingual compatibility while preserving the intended ASCII-based slug behavior.

Let me know if you're open to this change — I’d be happy to submit a PR if needed.

circusvoid avatar Jun 18 '25 03:06 circusvoid

Thanks for this, @circus1990666 - I'll put in the suggested fix (and add tests).

ksylvan avatar Jun 18 '25 14:06 ksylvan

Fixed in 1.6.0

ksylvan avatar Jun 18 '25 14:06 ksylvan