From 446a9ade8cb6e1162bba7510bbc7f8aec94b879d Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sun, 24 May 2020 23:10:55 -0700 Subject: [PATCH] fix: Imported documents should get a best-guess title --- app/utils/importFile.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/app/utils/importFile.js b/app/utils/importFile.js index 5ace59ce5..9afff572d 100644 --- a/app/utils/importFile.js +++ b/app/utils/importFile.js @@ -1,6 +1,7 @@ // @flow import Document from 'models/Document'; import DocumentsStore from 'stores/DocumentsStore'; +import parseTitle from 'shared/utils/parseTitle'; type Options = { file: File, @@ -19,13 +20,27 @@ const importFile = async ({ const reader = new FileReader(); reader.onload = async ev => { - const text = ev.target.result; + let text = ev.target.result; + let title; + + // If the first line of the imported file looks like a markdown heading + // then we can use this as the document title + if (text.trim().startsWith('# ')) { + const result = parseTitle(text); + title = result.title; + text = text.replace(`# ${title}\n`, ''); + + // otherwise, just use the filename without the extension as our best guess + } else { + title = file.name.replace(/\.[^/.]+$/, ''); + } let document = new Document( { parentDocumentId: documentId, collectionId, text, + title, }, documents );