Skip to content

Commit e7eb0a0

Browse files
committed
fix wordful line break important bug
2 parents 8d9e5d2 + 08bc328 commit e7eb0a0

File tree

9 files changed

+27
-124
lines changed

9 files changed

+27
-124
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## v1.1.1
2+
3+
- Fix wordful line break bug (important).
4+
- Link English doc to root readme file.
5+
16
## v1.1.0
27

38
- Optimization for emoji spaces.

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ which will disable all spaces when line break
2020

2121
```ts
2222
import md from "markdown-it"
23-
md.renderer.rules.softbreak = () => ""
23+
md.renderer.rules.softbreak = () => "" // [!code focus]
2424
```
2525

2626
But once working with multi-languages,
@@ -33,7 +33,7 @@ and you can use it like this:
3333
```ts
3434
import md from "markdown-it"
3535
import {Options} from "markdown-it-wordless"
36-
md.use(wordless)
36+
md.use(wordless) // [!code focus]
3737
```
3838

3939
## Basic rules
@@ -62,7 +62,7 @@ import {wordless} from "markdown-it-wordless"
6262
export default defineConfig({
6363
markdown: {
6464
config(md) {
65-
md.use(wordless)
65+
md.use(wordless) // [!code focus]
6666
},
6767
},
6868
// Other configs...
@@ -85,7 +85,7 @@ if you will only use Chinese or Japanese as wordless languages:
8585
```ts
8686
import md from "markdown-it"
8787
import {wordless, chineseAndJapanese, Options} from "markdown-it-wordless"
88-
md.use<Options>(wordless, {supportWordless: [chineseAndJapanese]})
88+
md.use<Options>(wordless, {supportWordless: [chineseAndJapanese]}) // [!code focus]
8989
```
9090

9191
Such optimization is unnecessary in most cases,

data.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,9 @@ export function langIndexOf(code: number, options?: Options): number {
237237
if (import.meta.vitest) {
238238
const {expect, test} = import.meta.vitest
239239

240-
test("basic function", function () {
240+
test("zh,ja punctuations", function () {
241241
expect(langIndexOf(",".charCodeAt(0))).toBe(-3)
242+
expect(langIndexOf("。".charCodeAt(0))).toBe(-3)
243+
expect(langIndexOf("、".charCodeAt(0))).toBe(-3)
242244
})
243245
}

docs/.vitepress/theme/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import DefaultTheme from "vitepress/theme"
2+
import "./root.css"
3+
4+
export default DefaultTheme

docs/.vitepress/theme/root.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
html:lang("zh") div.vp-doc p {
2+
text-indent: 2rem;
3+
text-align: justify;
4+
}

docs/index.md

Lines changed: 1 addition & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1 @@
1-
# Markdown-it Wordless
2-
3-
A [markdown-it](https://markdown-it.github.io) plugin
4-
to optimize wordless multi-language line-break render.
5-
6-
When a paragraph is long in markdown, we usually separate them into lines,
7-
and it will finally be rendered into a single line inside HTML.
8-
But for wordless languages (such as Chinese and Japanese),
9-
they do not use spaces to separate words,
10-
that they don't need a space to be added when processing line-break.
11-
12-
If you are only working with a single wordless language,
13-
you can definitely use the following code,
14-
which will disable all spaces when line break
15-
(render single `\n` into an empty string rather than a space):
16-
17-
```ts
18-
import md from "markdown-it"
19-
md.renderer.rules.softbreak = () => ""
20-
```
21-
22-
But once working with multi-languages,
23-
especially when there's a mix of wordless and wordful languages,
24-
such as using Chinese and English in a single markdown document,
25-
such options cannot handle all cases.
26-
So here comes this `"markdown-it-wordless"` plugin,
27-
and you can use it like this:
28-
29-
```ts
30-
import md from "markdown-it"
31-
import {Options} from "markdown-it-wordless"
32-
md.use(wordless)
33-
```
34-
35-
## Basic rules
36-
37-
1. Wordful languages (such as English and Arabic) will be rendered as usual.
38-
2. It won't add a space when line break between the same wordless language.
39-
3. It will add a space when line break between different wordless languages.
40-
4. Specially, Chinese and Japanese will be treated as a same language,
41-
as there are many shared characters between them,
42-
and their character styles are almost the same.
43-
5. Although Korean characters are like Chinese and Japanese (CJK),
44-
Korean is not a wordless language, it uses spaces to separate words.
45-
46-
## Use it with VitePress
47-
48-
[VitePress](https://vitepress.dev) is an excellent static site generator,
49-
and this package is also inspired when the author using VitePress.
50-
It's strongly recommended to add such plugin to VitePress
51-
if you are using wordless languages. And here's how to config:
52-
53-
```ts
54-
// <root>/.vitepress/config.ts
55-
import {defineConfig} from "vitepress"
56-
import {wordless} from "markdown-it-wordless"
57-
58-
export default defineConfig({
59-
markdown: {
60-
config(md) {
61-
md.use(wordless)
62-
},
63-
},
64-
// Other configs...
65-
})
66-
```
67-
68-
## Customize to optimize performance
69-
70-
The default option will enable optimization
71-
for all registered wordless languages inside this package.
72-
If you want to optimize performance,
73-
you can specify what exactly wordless language you are using.
74-
You may also specify what wordful language you are using,
75-
because there's only optimization for wordful languages
76-
which unicode is less than `0x0dff`.
77-
78-
Here's a simple example
79-
if you will only use Chinese or Japanese as wordless languages:
80-
81-
```ts
82-
import md from "markdown-it"
83-
import {wordless, chineseAndJapanese, Options} from "markdown-it-wordless"
84-
md.use<Options>(wordless, {supportWordless: [chineseAndJapanese]})
85-
```
86-
87-
Such optimization is unnecessary in most cases,
88-
because this plugin will not slow down the rendering process a lot
89-
in common cases (only a few milliseconds).
90-
And if you do want to customize,
91-
please make sure you've understand the source code. Please refer to
92-
[`data.ts`](https://github.com/treeinfra/markdown-it-wordless/blob/main/data.ts)
93-
for more details,
94-
and here's documentation for each item in details.
95-
96-
## About the supported languages
97-
98-
You can find all supported languages
99-
in the source code of
100-
[`data.ts`](https://github.com/treeinfra/markdown-it-wordless/blob/main/data.ts).
101-
Each language or language series is an exported const
102-
that you can import and call.
103-
104-
The languages series are based on the [Unicode](https://unicode.org/charts/).
105-
Most of the languages are coded manually and some of them are
106-
generated by several AI models. So that there might be mistakes,
107-
and the author cannot guarantee the accuracy of the data
108-
because it's almost impossible for a single person to learn all such languages.
109-
110-
If you are native speaker of one of the those wordless languages
111-
and you find there are some mistakes,
112-
or if there's even some wordless languages not included in this package,
113-
please feel free to open an issue.
1+
<!-- @include: ../README.md -->

docs/zh/index.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
但 Markdown 在渲染时会默认将换行渲染为空格,
66
而这样的空格在中文这种不用空格分割词汇的语言中显然是不合适的。
77

8-
```ts
8+
```ts:line-numbers
99
import md from "markdown-it"
10-
md.renderer.rules.softbreak = () => ""
10+
md.renderer.rules.softbreak = () => "" // [!code focus]
1111
```
1212

1313
在使用 [markdown-it](https://markdown-it.github.io) 时,
@@ -22,8 +22,8 @@ md.renderer.rules.softbreak = () => ""
2222
使用这个插件后,使用 Markdown 编辑中文这样的语言时,
2323
就可以随意的换行来而不必担心句子里被添加不美观的空格的问题了。
2424

25-
```ts
25+
```ts:line-numbers
2626
import md from "markdown-it"
2727
import {Options} from "markdown-it-wordless"
28-
md.use(wordless)
28+
md.use(wordless) // [!code focus]
2929
```

index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export function wordless(md: md, options?: Options) {
4545
const before = langIndexOf(prefix.charCodeAt(prefix.length - 1), options)
4646
const after = langIndexOf(suffix.charCodeAt(0), options)
4747

48-
if (before === after) return "" // Same wordless language.
48+
if (before === after && before >= 0) return "" // Same wordless language.
4949
if (before === -3 || after === -3) return "" // Special punctuations.
5050
if ((before === -2 && after >= 0) || (after === -2 && before >= 0))
5151
return "" // Resolve emoji.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "markdown-it-wordless",
33
"description": "A markdown-it plugin for wordless languages line-break.",
4-
"version": "1.1.0",
4+
"version": "1.1.1",
55
"type": "module",
66
"license": "MIT",
77
"keywords": [

0 commit comments

Comments
 (0)