Skip to content

Commit

Permalink
Merge pull request #662 from outline/issue-660
Browse files Browse the repository at this point in the history
Updated parseTitle to remove escape characters
  • Loading branch information
tommoor committed Jun 4, 2018
2 parents 1c081f8 + 0adb881 commit 4469cb7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -31,7 +31,8 @@
"jest": {
"verbose": false,
"roots": [
"app"
"app",
"shared"
],
"moduleNameMapper": {
"^.*[.](s?css|css)$": "<rootDir>/__mocks__/styleMock.js",
Expand Down
5 changes: 4 additions & 1 deletion shared/utils/parseTitle.js
Expand Up @@ -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, '$1');

// find and extract first emoji
const matches = regex.exec(title);
Expand Down
28 changes: 28 additions & 0 deletions shared/utils/parseTitle.test.js
@@ -0,0 +1,28 @@
/* 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');
expect(parseTitle(`# \\> Title`).title).toBe('> 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);
});

0 comments on commit 4469cb7

Please sign in to comment.