Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Conditionally strip-out markdown in note previews #1839

Merged
merged 2 commits into from
Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASE-NOTES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Fixes

- Fixed tag rename functionality [#1834](https://github.com/Automattic/simplenote-electron/pull/1834)
- Only remove markdown syntax in note list if markdown enabled for note [#1839](https://github.com/Automattic/simplenote-electron/pull/1839)

## [v1.14.0]

Expand Down
13 changes: 9 additions & 4 deletions lib/utils/note-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,15 @@ describe('noteTitleAndPreview', () => {
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);
const count = 100;
let sentinel = '';
const tic = process.hrtime();
for (let i = 0; i < count; i++) {
sentinel += noteTitleAndPreview(note);
}
const [s, ns] = process.hrtime(tic);
expect(sentinel.length).toBeGreaterThan(0);
expect((s * 1000 + ns / 1000 / 1000) / count).toBeLessThan(1);
});

it('should have enough text for an Expanded preview, even if the title is very long', () => {
Expand Down
21 changes: 14 additions & 7 deletions lib/utils/note-utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import removeMarkdown from 'remove-markdown';
import { isFunction } from 'lodash';

import * as T from '../types';

export interface TitleAndPreview {
title: string;
preview: string;
}

export const maxTitleChars = 64;
export const maxPreviewChars = 200;
const maxPreviewLines = 4; // probably need to adjust if we're in the comfy view
Expand Down Expand Up @@ -48,24 +55,24 @@ const getPreview = content => {
return matchUntilLimit(previewPattern, content);
};

const removeMarkdownFromOutput = isMarkdown
? s => removeMarkdownWithFix(s) || s
: s => s;
const formatPreview = (stripMarkdown: boolean, s: string): string =>
stripMarkdown ? removeMarkdownWithFix(s) || s : s;

/**
* Returns the title and excerpt for a given note
*
* @param {Object} note a note object
* @returns {Object} title and excerpt (if available)
*/
export const noteTitleAndPreview = note => {
export const noteTitleAndPreview = (note: T.NoteEntity): TitleAndPreview => {
const content = (note && note.data && note.data.content) || '';
const title = removeMarkdownFromOutput(getTitle(content));
const preview = removeMarkdownFromOutput(getPreview(content));
const stripMarkdown = isMarkdown(note);
const title = formatPreview(stripMarkdown, getTitle(content));
const preview = formatPreview(stripMarkdown, getPreview(content));
return { title, preview };
};

function isMarkdown(note) {
function isMarkdown(note: T.NoteEntity): boolean {
return note && note.data && note.data.systemTags.includes('markdown');
}

Expand Down