initial support for .org files

This commit is contained in:
Alen Šiljak
2021-03-14 01:33:59 +01:00
committed by Vishesh Handa
parent 1b6c76abb2
commit 171126aa87
4 changed files with 64 additions and 8 deletions

View File

@ -54,16 +54,15 @@ enum NoteType {
class NoteFileFormatInfo {
static List<String> allowedExtensions() {
return [
'.md',
'.txt',
];
return ['.md', '.org', '.txt'];
}
static String defaultExtension(NoteFileFormat format) {
switch (format) {
case NoteFileFormat.Markdown:
return ".md";
case NoteFileFormat.OrgMode:
return '.org';
case NoteFileFormat.Txt:
return ".txt";
default:
@ -75,13 +74,15 @@ class NoteFileFormatInfo {
var noteFilePath = filePath.toLowerCase();
var isMarkdownFile = noteFilePath.endsWith('.md');
var isTxtFile = noteFilePath.endsWith('.txt');
var isOrgFile = noteFilePath.endsWith('.org');
return isMarkdownFile || isTxtFile;
return isMarkdownFile || isTxtFile || isOrgFile;
}
}
enum NoteFileFormat {
Markdown,
OrgMode,
Txt,
}
@ -159,11 +160,18 @@ class Note with NotesNotifier {
_filePath = p.join(parent.folderPath, Uuid().v4());
}
switch (_fileFormat) {
case NoteFileFormat.OrgMode:
if (!_filePath.toLowerCase().endsWith('.org')) {
_filePath += '.org';
}
break;
case NoteFileFormat.Txt:
if (!_filePath.toLowerCase().endsWith('.txt')) {
_filePath += '.txt';
}
break;
case NoteFileFormat.Markdown:
default:
if (!_filePath.toLowerCase().endsWith('.md')) {
@ -288,7 +296,8 @@ class Note with NotesNotifier {
}
bool get canHaveMetadata {
if (_fileFormat == NoteFileFormat.Txt) {
if (_fileFormat == NoteFileFormat.Txt ||
_fileFormat == NoteFileFormat.OrgMode) {
return false;
}
return parent.config.yamlHeaderEnabled;
@ -356,6 +365,7 @@ class Note with NotesNotifier {
var fpLowerCase = _filePath.toLowerCase();
var isMarkdown = fpLowerCase.endsWith('.md');
var isTxt = fpLowerCase.endsWith('.txt');
var isOrg = fpLowerCase.endsWith('.org');
if (isMarkdown) {
try {
@ -379,6 +389,17 @@ class Note with NotesNotifier {
} catch (e, stackTrace) {
logExceptionWarning(e, stackTrace);
_loadState = NoteLoadState.Error;
_notifyModified();
return _loadState;
}
} else if (isOrg) {
try {
body = await File(_filePath).readAsString();
_fileFormat = NoteFileFormat.OrgMode;
} catch (e, stackTrace) {
logExceptionWarning(e, stackTrace);
_loadState = NoteLoadState.Error;
_notifyModified();
return _loadState;
@ -419,9 +440,17 @@ class Note with NotesNotifier {
await file.delete();
}
///
/// Do not let the user rename it to a different file-type.
///
void rename(String newName) {
// Do not let the user rename it to a non-markdown file
switch (_fileFormat) {
case NoteFileFormat.OrgMode:
if (!newName.toLowerCase().endsWith('.org')) {
newName += '.org';
}
break;
case NoteFileFormat.Txt:
if (!newName.toLowerCase().endsWith('.txt')) {
newName += '.txt';

View File

@ -144,6 +144,12 @@ class NoteEditorState extends State<NoteEditor> with WidgetsBindingObserver {
}
}
// Org files
if (note.fileFormat == NoteFileFormat.OrgMode &&
editorType == EditorType.Markdown) {
editorType = EditorType.Raw;
}
// Txt files
if (note.fileFormat == NoteFileFormat.Txt &&
editorType == EditorType.Markdown) {

View File

@ -68,6 +68,7 @@ class LinkResolver {
var lowerCaseTerm = term.toLowerCase();
var termEndsWithMd = lowerCaseTerm.endsWith('.md');
var termEndsWithTxt = lowerCaseTerm.endsWith('.txt');
var termEndsWithOrg = lowerCaseTerm.endsWith('.org');
var rootFolder = inputNote.parent.rootFolder;
for (var note in rootFolder.getAllNotes()) {
@ -85,6 +86,19 @@ class LinkResolver {
if (f == term) {
return note;
}
} else if (fileName.toLowerCase().endsWith('.org')) {
if (termEndsWithOrg) {
if (fileName == term) {
return note;
} else {
continue;
}
}
var f = fileName.substring(0, fileName.length - 4);
if (f == term) {
return note;
}
} else if (fileName.toLowerCase().endsWith('.txt')) {
if (termEndsWithTxt) {
if (fileName == term) {
@ -124,6 +138,13 @@ class LinkResolver {
}
}
if (!spec.endsWith('.org')) {
linkedNote = folder.getNoteWithSpec(spec + '.org');
if (linkedNote != null) {
return linkedNote;
}
}
if (!spec.endsWith('.txt')) {
linkedNote = folder.getNoteWithSpec(spec + '.txt');
if (linkedNote != null) {

View File

@ -32,7 +32,7 @@ class NoteEditorSelector extends StatelessWidget {
Widget build(BuildContext context) {
var list = Column(
children: <Widget>[
if (fileFormat != NoteFileFormat.Txt)
if (fileFormat == NoteFileFormat.Markdown)
_buildTile(
context,
EditorType.Markdown,