From 57cf826e2cf2df793cdc3e7af0676d09666a1f79 Mon Sep 17 00:00:00 2001 From: anirudh murali <49116134+anihm136@users.noreply.github.com> Date: Sat, 9 May 2020 20:25:00 +0530 Subject: [PATCH] All: Resolves #2915: Improve automatic title generation (#2955) --- CliClient/tests/markdownUtils.js | 19 +++++++++++++++++++ ReactNativeClient/lib/markdownUtils.js | 10 ++++++++++ ReactNativeClient/lib/models/Note.js | 18 ++---------------- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/CliClient/tests/markdownUtils.js b/CliClient/tests/markdownUtils.js index 5afd4ea84..062aa9fa0 100644 --- a/CliClient/tests/markdownUtils.js +++ b/CliClient/tests/markdownUtils.js @@ -66,4 +66,23 @@ describe('markdownUtils', function() { } })); + it('replace markdown link with description', asyncTest(async () => { + + const testCases = [ + ['Test case [one](link)', 'Test case one'], + ['Test case ![two](imagelink)', 'Test case two'], + ['**# -Test case three', 'Test case three'], + ['This is a looooooong tiiitlllle with moore thaaaaaaan eighty characters and a [link](linkurl) at the end', 'This is a looooooong tiiitlllle with moore thaaaaaaan eighty characters and a li'], + ['', ''], + ['These are [link1](one), [link2](two) and ![link3](three)', 'These are link1, link2 and link3'], + ['No description link to [](https://joplinapp.org)', 'No description link to https://joplinapp.org'], + ]; + + for (let i = 0; i < testCases.length; i++) { + const md = testCases[i][0]; + const expected = testCases[i][1]; + expect(markdownUtils.titleFromBody(md)).toBe(expected); + } + })); + }); diff --git a/ReactNativeClient/lib/markdownUtils.js b/ReactNativeClient/lib/markdownUtils.js index e4ae0ad63..898e41bf9 100644 --- a/ReactNativeClient/lib/markdownUtils.js +++ b/ReactNativeClient/lib/markdownUtils.js @@ -91,6 +91,16 @@ const markdownUtils = { return output.join('\n'); }, + + titleFromBody(body) { + if (!body) return ''; + const mdLinkRegex = /!?\[([^\]]+?)\]\(.+?\)/g; + const emptyMdLinkRegex = /!?\[\]\((.+?)\)/g; + const filterRegex = /^[# \n\t*`-]*/; + const lines = body.trim().split('\n'); + const title = lines[0].trim(); + return title.replace(filterRegex, '').replace(mdLinkRegex, '$1').replace(emptyMdLinkRegex, '$1').substring(0,80); + }, }; module.exports = markdownUtils; diff --git a/ReactNativeClient/lib/models/Note.js b/ReactNativeClient/lib/models/Note.js index 26e03697a..091bbf83b 100644 --- a/ReactNativeClient/lib/models/Note.js +++ b/ReactNativeClient/lib/models/Note.js @@ -11,6 +11,7 @@ const { _ } = require('lib/locale.js'); const ArrayUtils = require('lib/ArrayUtils.js'); const lodash = require('lodash'); const urlUtils = require('lib/urlUtils.js'); +const markdownUtils = require('lib/markdownUtils.js'); const { MarkupToHtml } = require('lib/joplin-renderer'); const { ALL_NOTES_FILTER_ID } = require('lib/reserved-ids'); @@ -82,22 +83,7 @@ class Note extends BaseItem { } static defaultTitleFromBody(body) { - if (body && body.length) { - const lines = body.trim().split('\n'); - let output = lines[0].trim(); - // Remove the first #, *, etc. - while (output.length) { - const c = output[0]; - if (['#', ' ', '\n', '\t', '*', '`', '-'].indexOf(c) >= 0) { - output = output.substr(1); - } else { - break; - } - } - return output.substr(0, 80).trim(); - } - - return _('Untitled'); + return markdownUtils.titleFromBody(body); } static geolocationUrl(note) {