Skip to content

Commit 76961d6

Browse files
committed
Fixes #33 - escape commit message
1 parent cc827f4 commit 76961d6

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/git/formatters/commit.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { GitCommit } from '../models/commit';
44
import { GitDiffLine } from '../models/diff';
55
import * as moment from 'moment';
66

7+
const escapeMarkdownRegEx = /[`\>\#\*\_\-\+\.]/g;
8+
// const sampleMarkdown = '## message `not code` *not important* _no underline_ \n> don\'t quote me \n- don\'t list me \n+ don\'t list me \n1. don\'t list me \nnot h1 \n=== \nnot h2 \n---\n***\n---\n___';
9+
710
export interface ICommitFormatOptions {
811
dateFormat?: string | null;
912
tokenOptions?: {
@@ -163,7 +166,17 @@ export class CommitFormatter {
163166
dateFormat = 'MMMM Do, YYYY h:MMa';
164167
}
165168

166-
const message = commit.isUncommitted ? '' : `\n\n> ${commit.message.replace(/\n/g, ' \n')}`;
169+
let message = '';
170+
if (!commit.isUncommitted) {
171+
message = commit.message
172+
// Escape markdown
173+
.replace(escapeMarkdownRegEx, '\\$&')
174+
// Escape markdown header (since the above regex won't match it)
175+
.replace(/^===/gm, '\u200b===')
176+
// Keep under the same block-quote
177+
.replace(/\n/g, ' \n');
178+
message = `\n\n> ${message}`;
179+
}
167180
return `\`${commit.shortSha}\`   __${commit.author}__, ${moment(commit.date).fromNow()}   _(${moment(commit.date).format(dateFormat)})_${message}`;
168181
}
169182

0 commit comments

Comments
 (0)