Skip to content

Commit 3c47539

Browse files
authored
Merge pull request #6 from ksylvan/0618-support-cjk-characters
CJK Support Added for Markdown Slug Generation
2 parents 5ca2c62 + ac6f641 commit 3c47539

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

bin/md-tree.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,10 @@ class MarkdownCLI {
107107
sanitizeText(text) {
108108
return text
109109
.toLowerCase()
110-
.replace(/[^a-z0-9\s-]/g, '')
110+
.replace(
111+
/[^a-z0-9\u4e00-\u9fff\u3040-\u309f\u30a0-\u30ff\uac00-\ud7af\s-]/g,
112+
''
113+
)
111114
.replace(/\s+/g, '-')
112115
.replace(/-+/g, '-')
113116
.replace(/^-|-$/g, '');

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@kayvan/markdown-tree-parser",
3-
"version": "1.5.1",
3+
"version": "1.6.0",
44
"description": "A powerful JavaScript library and CLI tool for parsing and manipulating markdown files as tree structures using the remark/unified ecosystem",
55
"type": "module",
66
"main": "index.js",

test/test-cjk.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Document Title
2+
3+
## 章节一
4+
5+
This is the first section.
6+
7+
## 章二
8+
9+
This is the second section.
10+
11+
### セクション 2.1
12+
13+
This is a subsection.
14+
15+
## Another Section
16+
17+
This is another section.

test/test-cli.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,42 @@ This document has links too:
445445
);
446446
});
447447

448+
await test('CLI explode command with CJK characters', async () => {
449+
const cjkTestFile = path.resolve(__dirname, 'test-cjk.md');
450+
const outputDir = path.join(testDir, 'cjk-exploded');
451+
const result = await runCLI(['explode', cjkTestFile, outputDir]);
452+
453+
assert(
454+
result.code === 0,
455+
`Command should succeed, got exit code ${result.code}\nstderr: ${result.stderr}`
456+
);
457+
458+
const files = await fs.readdir(outputDir);
459+
assert(files.includes('index.md'), 'Should create index.md');
460+
assert(files.includes('章节一.md'), 'Should create file with Chinese slug');
461+
assert(files.includes('章二.md'), 'Should create file with Chinese slug');
462+
assert(
463+
files.includes('another-section.md'),
464+
'Should create file with English slug'
465+
);
466+
467+
// Check the index file for correct links
468+
const indexPath = path.join(outputDir, 'index.md');
469+
const indexContent = await fs.readFile(indexPath, 'utf-8');
470+
assert(
471+
indexContent.includes('[章节一](./章节一.md)'),
472+
'Should link to Chinese filename'
473+
);
474+
assert(
475+
indexContent.includes('[章二](./章二.md)'),
476+
'Should link to Chinese filename'
477+
);
478+
assert(
479+
indexContent.includes('[セクション 2.1](./章二.md#セクション-21)'),
480+
'Should link to Japanese subsection'
481+
);
482+
});
483+
448484
await cleanupTests();
449485

450486
// Summary

0 commit comments

Comments
 (0)