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