From 76961d6697159f88b59227e31fc31766597667a0 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Mon, 12 Jun 2017 12:06:10 -0400 Subject: [PATCH] Fixes #33 - escape commit message --- src/git/formatters/commit.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/git/formatters/commit.ts b/src/git/formatters/commit.ts index 27736ab..be8d3a5 100644 --- a/src/git/formatters/commit.ts +++ b/src/git/formatters/commit.ts @@ -4,6 +4,9 @@ import { GitCommit } from '../models/commit'; import { GitDiffLine } from '../models/diff'; import * as moment from 'moment'; +const escapeMarkdownRegEx = /[`\>\#\*\_\-\+\.]/g; +// 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___'; + export interface ICommitFormatOptions { dateFormat?: string | null; tokenOptions?: { @@ -163,7 +166,17 @@ export class CommitFormatter { dateFormat = 'MMMM Do, YYYY h:MMa'; } - const message = commit.isUncommitted ? '' : `\n\n> ${commit.message.replace(/\n/g, ' \n')}`; + let message = ''; + if (!commit.isUncommitted) { + message = commit.message + // Escape markdown + .replace(escapeMarkdownRegEx, '\\$&') + // Escape markdown header (since the above regex won't match it) + .replace(/^===/gm, '\u200b===') + // Keep under the same block-quote + .replace(/\n/g, ' \n'); + message = `\n\n> ${message}`; + } return `\`${commit.shortSha}\`   __${commit.author}__, ${moment(commit.date).fromNow()}   _(${moment(commit.date).format(dateFormat)})_${message}`; }