diff --git a/package.json b/package.json index d9e38157..6c147976 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,8 @@ "jest": { "verbose": false, "roots": [ - "app" + "app", + "shared" ], "moduleNameMapper": { "^.*[.](s?css|css)$": "/__mocks__/styleMock.js", diff --git a/shared/utils/parseTitle.js b/shared/utils/parseTitle.js index ebb3e0dc..9faa2929 100644 --- a/shared/utils/parseTitle.js +++ b/shared/utils/parseTitle.js @@ -6,7 +6,10 @@ export default function parseTitle(text: string = '') { // find and extract title const firstLine = text.trim().split(/\r?\n/)[0]; - const title = firstLine.replace(/^#/, '').trim(); + const trimmedTitle = firstLine.replace(/^#/, '').trim(); + + // remove any escape characters + const title = trimmedTitle.replace(/\\([\\`*{}[\]()#+\-.!_>])/g, '$&'); // find and extract first emoji const matches = regex.exec(title); diff --git a/shared/utils/parseTitle.test.js b/shared/utils/parseTitle.test.js new file mode 100644 index 00000000..60d35738 --- /dev/null +++ b/shared/utils/parseTitle.test.js @@ -0,0 +1,27 @@ +/* eslint-disable flowtype/require-valid-file-annotation */ +import parseTitle from './parseTitle'; + +it('should trim the title', () => { + expect(parseTitle(`# Lots of space `).title).toBe('Lots of space'); +}); + +it('should extract first title', () => { + expect(parseTitle(`# Title one\n# Title two`).title).toBe('Title one'); +}); + +it('should remove escape characters', () => { + expect(parseTitle(`# Thing \- one`).title).toBe('Thing - one'); + expect(parseTitle(`# \[wip\] Title`).title).toBe('[wip] Title'); +}); + +it('should parse emoji if first character', () => { + const parsed = parseTitle(`# 😀 Title`); + expect(parsed.title).toBe('😀 Title'); + expect(parsed.emoji).toBe('😀'); +}); + +it('should not parse emoji if not first character', () => { + const parsed = parseTitle(`# Title 🌈`); + expect(parsed.title).toBe('Title 🌈'); + expect(parsed.emoji).toBe(undefined); +});