Skip to content

Commit

Permalink
fix: capitalizeEachWords separates them into words based on all spe…
Browse files Browse the repository at this point in the history
…cial characters (close #175)
  • Loading branch information
jooy2 committed Sep 19, 2024
1 parent 0e36f02 commit 9812907
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 10 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Changelog

## 1.26.3 (2024-09-19)
## 1.27.0 (2024-09-19)

- Do not create an empty items array
- Now `capitalizeEachWords` separates them into words based on all special characters

## 1.26.2 (2024-09-19)

Expand Down
4 changes: 3 additions & 1 deletion docs/en/guide/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,9 @@ If the value is `true`, the first letter of the menu name is forced to uppercase
- Type: `boolean`
- Default: `false`

If the value is `true`, Capitalize all first letters of words separated by spaces. This option is also affected when the menu name is imported via a MarkDown heading or frontmatter.
If the value is `true`, will capitalize all first letters of words separated by special characters. This option is also affected when menu names are imported via markdown headers or Frontmatter.

For example, `abc def ghi` and `abc-def ghi` change to `Abc Def Ghi` and `Abc-Def Ghi`, respectively.

## `excludePattern`

Expand Down
4 changes: 3 additions & 1 deletion docs/ko/guide/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,9 @@ name: 이 것은 Frontmatter의 제목값입니다.
- Type: `boolean`
- Default: `false`

값이 `true`이면 공백으로 구분된 단어의 첫 글자를 모두 대문자로 표시합니다. 이 옵션은 메뉴 이름을 마크다운 머리글 또는 앞부분을 통해 가져올 때도 영향을 받습니다.
값이 `true`이면 특수문자로 구분된 단어의 첫 글자를 모두 대문자로 표시합니다. 이 옵션은 메뉴 이름을 마크다운 머리글 또는 Frontmatter을 통해 가져올 때도 영향을 받습니다.

예를 들어, `abc def ghi``abc-def ghi`는 각각 `Abc Def Ghi``Abc-Def Ghi`로 변경됩니다.

## `excludePattern`

Expand Down
4 changes: 3 additions & 1 deletion docs/zhHans/guide/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,9 @@ name: This is frontmatter title value.
- Type: `boolean`
- Default: `false`

如果值为 `true`,则单词的所有首字母大写,并用空格分隔。通过 Markdown 标题或 frontmatter 导入菜单名称时,该选项也会受到影响。
如果值为 `true`,将大写由特殊字符分隔的单词的所有首字母。当菜单名称通过 markdown 标头或 Frontmatter 导入时,该选项也会受到影响。

例如,`abc def ghi``abc-def ghi`将分别变为`Abc Def Ghi``Abc-Def Ghi`

## `excludePattern`

Expand Down
12 changes: 7 additions & 5 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -754,13 +754,15 @@ export default class VitePressSidebar {
text = text.replace(/_/g, ' ');
}
if (options.capitalizeEachWords) {
const splitStr = text.trim().split(' ');
let lastChar = '';

for (let i = 0; i < splitStr.length; i += 1) {
splitStr[i] = VitePressSidebar.capitalizeFirst(splitStr[i]);
}
for (let i = 0; i < text.length; i += 1) {
if ((i === 0 || !/[a-zA-Z]/.test(lastChar)) && /[a-z]/.test(text[i])) {
text = text.slice(0, i) + text[i].toUpperCase() + text.slice(i + 1);
}

text = splitStr.join(' ');
lastChar = text[i];
}
} else if (options.capitalizeFirst) {
text = VitePressSidebar.capitalizeFirst(text);
}
Expand Down
1 change: 1 addition & 0 deletions test/resources/capitalize/1-abc-def.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
empty file
1 change: 1 addition & 0 deletions test/resources/capitalize/2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# a hello world abc-def
24 changes: 23 additions & 1 deletion test/specs/apis.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1329,7 +1329,7 @@ describe('Test: APIs', () => {
done();
});

it('API: capitalizeEachWords', (done) => {
it('API: capitalizeEachWords (A)', (done) => {
assert.deepEqual(
generateSidebar({
hyphenToSpace: true,
Expand Down Expand Up @@ -1390,6 +1390,28 @@ describe('Test: APIs', () => {
done();
});

it('API: capitalizeEachWords (B)', (done) => {
assert.deepEqual(
generateSidebar({
documentRootPath: `${TEST_DIR_BASE}/capitalize`,
useTitleFromFileHeading: true,
capitalizeEachWords: true
}),
[
{
text: '1-Abc-Def',
link: '/1-abc-def'
},
{
text: 'A Hello World Abc-Def',
link: '/2'
}
]
);

done();
});

it('API: sortMenusByFrontmatterDate', (done) => {
assert.deepEqual(
generateSidebar({
Expand Down

0 comments on commit 9812907

Please sign in to comment.