Skip to content

Commit

Permalink
Remove markdown in preview without triggering bug (Automattic#1088)
Browse files Browse the repository at this point in the history
  • Loading branch information
mirka authored Dec 11, 2018
1 parent 56629e8 commit 8ac560f
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 6 deletions.
25 changes: 19 additions & 6 deletions lib/utils/note-utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import removeMarkdown from 'remove-markdown';

/**
* Matches the title and excerpt in a note's content
*
Expand All @@ -23,6 +25,12 @@ const noteTitleAndPreviewMdRegExp = /(?:#{0,6}\s+)?(\S[^\n]*)\s*(?:#{0,6}\s+)?(\
// Ample amount of characters for 'Expanded' list view
const characterLimit = 300;

// Defaults for notes with empty content
export const defaults = {
title: 'New Note...',
preview: '',
};

/**
* Returns the title and excerpt for a given note
*
Expand All @@ -37,16 +45,21 @@ export const noteTitleAndPreview = note => {
? noteTitleAndPreviewMdRegExp.exec(content || '')
: noteTitleAndPreviewRegExp.exec(content || '');

const defaults = {
title: 'New Note...',
preview: '',
};

if (!match) {
return defaults;
}

const [, title, preview] = match;
let [, title, preview] = match;

if (preview && isMarkdown(note)) {
// Workaround for a bug in `remove-markdown`
// See https://github.com/stiang/remove-markdown/issues/35
const previewWithSpacesTrimmed = preview.replace(/(\s)\s+/g, '$1');

preview = removeMarkdown(previewWithSpacesTrimmed, {
stripListLeaders: false,
});
}

return {
title: title.slice(0, 200) || defaults.title,
Expand Down
50 changes: 50 additions & 0 deletions lib/utils/note-utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import noteTitleAndPreview, { defaults } from './note-utils';

describe('noteTitleAndPreview', () => {
let note;

beforeEach(() => {
note = {
data: {
content: '',
systemTags: ['markdown'],
},
};
});

it('should return default values if note content is empty', () => {
const result = noteTitleAndPreview(note);
expect(result).toEqual(defaults);
});

it('should return the title and preview when note is not Markdown', () => {
note.data.content = 'My title\nThe preview';
note.data.systemTags = [];
const result = noteTitleAndPreview(note);
expect(result).toEqual({
title: 'My title',
preview: 'The preview',
});
});

it('should return the title and preview with Markdown removed when note is Markdown', () => {
note.data.content = '# My title\nThe [preview](https://test.com)';
const result = noteTitleAndPreview(note);
expect(result).toEqual({
title: 'My title',
preview: 'The preview',
});
});

// Test that it doesn't trigger the bug in `remove-markdown`
// See https://github.com/stiang/remove-markdown/issues/35
it('should complete in a reasonable amount of time', () => {
const bugInducingString =
'# aaa bbb';
note.data.content = bugInducingString + '\n' + bugInducingString;
const startTime = Date.now();
noteTitleAndPreview(note);
const elapsedMs = Date.now() - startTime;
expect(elapsedMs).toBeLessThanOrEqual(1);
});
});
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
"redux": "3.7.2",
"redux-localstorage": "0.4.1",
"redux-thunk": "2.2.0",
"remove-markdown": "0.3.0",
"sanitize-filename": "1.6.1",
"sax": "1.2.4",
"semver": "^5.5.1",
Expand Down

0 comments on commit 8ac560f

Please sign in to comment.