fix: Imported documents should get a best-guess title
This commit is contained in:
@@ -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
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user