1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-11-23 22:36:32 +02:00

Desktop: Fixes #11662: Prevent the default note title from being " " (#11785)

This commit is contained in:
Henry Heino
2025-02-06 10:00:40 -08:00
committed by GitHub
parent c7031568a8
commit c55c8d62ec
2 changed files with 22 additions and 21 deletions

View File

@@ -216,12 +216,19 @@ const markdownUtils = {
titleFromBody(body: string) {
if (!body) return '';
const spaceEntities = / /g;
body = body.replace(spaceEntities, ' ');
const lines = body.trim().split('\n');
const title = lines[0].trim();
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);
return title
.replace(filterRegex, '')
.replace(mdLinkRegex, '$1')
.replace(emptyMdLinkRegex, '$1')
.substring(0, 80);
},
};

View File

@@ -106,23 +106,17 @@ describe('markdownUtils', () => {
}
}));
it('replace markdown link with description', (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);
}
}));
it.each([
['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'],
['&nbsp;\n\nThis is a test &nbsp; test.', 'This is a test test.'],
])('should create a default note title from the note body (%j -> %j)', (body, expectedTitle) => {
expect(markdownUtils.titleFromBody(body)).toBe(expectedTitle);
});
});