Skip to content

Commit 77b8c47

Browse files
committed
docs(cz-commitlint): add emoji to title
1 parent 71e8e36 commit 77b8c47

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed
-25.9 KB
Loading
9.79 KB
Loading
26.1 KB
Loading

docs/reference/examples.md

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,132 @@ These examples show common usages of how commitlint can be configured.
2222
// ...
2323
}
2424
```
25+
26+
:::
27+
28+
## Customizing Emojis and Alignment in VS Code
29+
30+
Some terminals have trouble correctly calculating the width of Unicode emojis, which can cause a missing space after the emoji, leading to misaligned text in the commit prompt.
31+
32+
![cz-commitlint questions](/assets/vs-code-emoji.png)
33+
34+
To fix this issue in VS Code, you can specify an additional space after each emoji in your `commitlint.config.ts` file.
35+
36+
::: code-group
37+
38+
```ts [commitlint.config.ts]
39+
import { type UserConfig } from "@commitlint/types";
40+
41+
export default {
42+
// Use the conventional commit rules as a base.
43+
extends: ["@commitlint/config-conventional"],
44+
prompt: {
45+
questions: {
46+
type: {
47+
enum: {
48+
// Add a space to a few common types for better alignment.
49+
build: {
50+
emoji: "🛠️ ", // The extra space fixes the alignment.
51+
},
52+
chore: {
53+
emoji: "♻️ ",
54+
},
55+
ci: {
56+
emoji: "⚙️ ",
57+
},
58+
revert: {
59+
emoji: "🗑️ ",
60+
},
61+
},
62+
},
63+
},
64+
},
65+
} satisfies UserConfig;
66+
```
67+
68+
:::
69+
70+
## Include Emojis in Commit Messages
71+
72+
By default, emojis are only shown in the commit message prompt. To include them in the actual commit header, you need a custom parser and a setting to enable them.
73+
74+
This configuration is based on the conventional commit rules and uses a _parser preset_ to validate commit headers that start with an emoji.
75+
76+
::: code-group
77+
78+
```ts [commitlint.config.ts]
79+
import type { ParserPreset, UserConfig } from "@commitlint/types";
80+
import config from "@commitlint/config-conventional";
81+
import createPreset from "conventional-changelog-conventionalcommits";
82+
import { merge } from "lodash-es";
83+
84+
// A helper function to create the custom emoji parser preset.
85+
async function createEmojiParser(): Promise<ParserPreset> {
86+
// Generates the regex from the emojis defined in the conventional config.
87+
const emojiRegexPart = Object.values(config.prompt.questions.type.enum)
88+
.map((value) => value.emoji.trim())
89+
.join("|");
90+
91+
const parserOpts = {
92+
// This regular expression validates commit headers with an emoji.
93+
breakingHeaderPattern: new RegExp(
94+
`^(?:${emojiRegexPart})\\s+(\\w*)(?:\\((.*)\\))?!:\\s+(.*)$`,
95+
),
96+
headerPattern: new RegExp(
97+
`^(?:${emojiRegexPart})\\s+(\\w*)(?:\\((.*)\\))?!?:\\s+(.*)$`,
98+
),
99+
};
100+
101+
const emojiParser = merge({}, await createPreset(), {
102+
conventionalChangelog: { parserOpts },
103+
parserOpts,
104+
recommendedBumpOpts: { parserOpts },
105+
});
106+
107+
return emojiParser;
108+
}
109+
110+
const emojiParser = await createEmojiParser();
111+
112+
export default {
113+
extends: ["@commitlint/config-conventional"],
114+
parserPreset: emojiParser,
115+
prompt: {
116+
questions: {
117+
type: {
118+
enum: {
119+
// Customize emojis and add the extra space for better alignment.
120+
build: { emoji: "🛠️ " },
121+
chore: { emoji: "♻️ " },
122+
ci: { emoji: "⚙️ " },
123+
revert: { emoji: "🗑️ " },
124+
},
125+
// This setting includes the emoji in the final commit header.
126+
headerWithEmoji: true,
127+
},
128+
},
129+
},
130+
} satisfies UserConfig;
131+
```
132+
133+
:::
134+
135+
Although some emojis may appear without a trailing space in the terminal, the commit message itself is submitted with the correct formatting.
136+
137+
![cz-commitlint questions](/assets/vs-code-commit-msg.png)
138+
139+
You can verify this with `git log -4 --format=%B > commits.txt`.
140+
141+
:::code-group
142+
143+
```text [commits.txt]
144+
⚙️ ci(scope): short
145+
146+
🛠 build(scope): short
147+
148+
🐛 fix(scope): short
149+
150+
✨ feat(scope): short
151+
```
152+
153+
:::

0 commit comments

Comments
 (0)