1212using MSHC . Lang . Collections ;
1313using Ookii . Dialogs . Wpf ;
1414using ScintillaNET ;
15+ using System . Web ;
1516
1617namespace AlephNote . WPF . Windows
1718{
@@ -30,7 +31,8 @@ public partial class MainWindowViewmodel
3031 public ICommand DuplicateNoteCommand => new RelayCommand ( DuplicateNote ) ;
3132 public ICommand PinUnpinNoteCommand => new RelayCommand ( PinUnpinNote ) ;
3233 public ICommand LockUnlockNoteCommand => new RelayCommand ( LockUnlockNote ) ;
33- public ICommand InsertHyperlinkCommand => new RelayCommand ( InsertHyperlink ) ;
34+ public ICommand ExportFrontmatterCommand => new RelayCommand ( ExportFrontmatter ) ;
35+ public ICommand InsertHyperlinkCommand => new RelayCommand ( InsertHyperlink ) ;
3436 public ICommand InsertFilelinkCommand => new RelayCommand ( InsertFilelink ) ;
3537 public ICommand InsertNotelinkCommand => new RelayCommand ( InsertNotelink ) ;
3638 public ICommand InsertMaillinkCommand => new RelayCommand ( InsertMaillink ) ;
@@ -247,7 +249,59 @@ private void LockUnlockNote()
247249 }
248250 }
249251
250- private void AddTagToNote ( string t )
252+ private void ExportFrontmatter ( )
253+ {
254+ if ( SelectedNote == null ) return ;
255+
256+ var dialog = new VistaFolderBrowserDialog ( ) ;
257+ if ( ! ( dialog . ShowDialog ( ) ?? false ) ) return ;
258+
259+ try
260+ {
261+ var tnow = DateTime . UtcNow ;
262+
263+ var directory = dialog . SelectedPath ;
264+ foreach ( var note in Repository . Notes )
265+ {
266+ var subpath = note . Path . Enumerate ( ) . Select ( p => ANFilenameHelper . StripStringForFilename ( p ) ) . ToArray ( ) ;
267+
268+ var filename = ANFilenameHelper . StripStringForFilename ( note . Title ) + ".md" ;
269+
270+ var filedir = Path . Combine ( ( new string [ ] { directory } ) . AsEnumerable ( ) . Concat ( subpath ) . ToArray ( ) ) ;
271+
272+ Directory . CreateDirectory ( filedir ) ;
273+
274+ var filedest = Path . Combine ( ( new string [ ] { directory } ) . AsEnumerable ( ) . Concat ( subpath ) . Concat ( new string [ ] { filename } ) . ToArray ( ) ) ;
275+
276+ var mdfm = "---\n " ;
277+ mdfm += $ "id: { note . UniqueName } \n ";
278+ mdfm += $ "title: { note . Title } \n ";
279+ mdfm += $ "updated: { note . ModificationDate . ToUniversalTime ( ) : yyyy-MM-dd HH:mm:ss} Z\n ";
280+ mdfm += $ "created: { note . CreationDate . ToUniversalTime ( ) : yyyy-MM-dd HH:mm:ss} Z\n ";
281+ mdfm += $ "exported: { tnow . ToUniversalTime ( ) : yyyy-MM-dd HH:mm:ss} Z\n ";
282+ mdfm += $ "locked: { note . IsLocked . ToString ( ) . ToLower ( ) } \n ";
283+ mdfm += $ "pinned: { note . IsPinned . ToString ( ) . ToLower ( ) } \n ";
284+ mdfm += $ "conflict: { note . IsConflictNote . ToString ( ) . ToLower ( ) } \n ";
285+ mdfm += $ "tags: [{ string . Join ( ", " , note . Tags . Select ( p => '"' + HttpUtility . JavaScriptStringEncode ( p ) + '"' ) ) } ]\n ";
286+ mdfm += $ "path: [{ string . Join ( ", " , note . Path . Enumerate ( ) . Select ( p => '"' + HttpUtility . JavaScriptStringEncode ( p ) + '"' ) ) } ]\n ";
287+ mdfm += $ "---\n ";
288+ mdfm += $ "\n ";
289+ mdfm += note . Text ;
290+
291+ if ( File . Exists ( filedest ) ) { throw new Exception ( $ "File { filedest } already exists") ; }
292+
293+ File . WriteAllText ( filedest , mdfm , Encoding . UTF8 ) ;
294+ }
295+ }
296+ catch ( Exception e )
297+ {
298+ App . Logger . Error ( "Main" , "Could not write to file" , e ) ;
299+ ExceptionDialog . Show ( Owner , "Could not write to file" , e , string . Empty ) ;
300+ }
301+ }
302+
303+
304+ private void AddTagToNote ( string t )
251305 {
252306 if ( SelectedNote == null ) return ;
253307 if ( Settings . IsReadOnlyMode ) return ;
0 commit comments