1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

All: Resolves #2915: Improve automatic title generation (#2955)

This commit is contained in:
anirudh murali 2020-05-09 20:25:00 +05:30 committed by GitHub
parent dbe8d3a68e
commit 57cf826e2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 16 deletions

View File

@ -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);
}
}));
});

View File

@ -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;

View File

@ -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) {