Skip to content

Commit

Permalink
Added frontmatter export
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikescher committed Nov 12, 2023
1 parent 9e49cf7 commit e462199
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 12 deletions.
10 changes: 7 additions & 3 deletions Source/AlephNote.App/AlephNote.App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
<AssemblyProduct>AlephNote.App</AssemblyProduct>
<AssemblyCopyright>Copyright © 2021</AssemblyCopyright>

<AssemblyVersion>1.7.15.0</AssemblyVersion>
<AssemblyFileVersion>1.7.15.0</AssemblyFileVersion>
<AssemblyInformationalVersion>1.7.15.0-master</AssemblyInformationalVersion>
<AssemblyVersion>1.7.16.0</AssemblyVersion>
<AssemblyFileVersion>1.7.16.0</AssemblyFileVersion>
<AssemblyInformationalVersion>1.7.16.0-master</AssemblyInformationalVersion>

<OutDir>..\..\Bin\$(Configuration)\</OutDir>
</PropertyGroup>
Expand Down Expand Up @@ -97,4 +97,8 @@
<PackageReference Include="WPFToolkit" Version="3.5.50211.1" />
</ItemGroup>

<ItemGroup>
<Reference Include="System.Web" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions Source/AlephNote.App/Resources/themes/default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ Repository: https://github.com/Mikescher/AlephNote
<theme>
<meta>
<name>Default</name>
<version>1.7.15</version>
<compatibility>1.7.15</compatibility>
<version>1.7.16</version>
<compatibility>1.7.16</compatibility>
<author>Mike Schwörer</author>
<type>default</type>
</meta>
Expand Down
10 changes: 6 additions & 4 deletions Source/AlephNote.App/WPF/Windows/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,12 @@
<ctrl:AutoActionMenuItem Header="Duplicate Note" AlephAction="DuplicateNote" Settings="{Binding Settings}" />
<ctrl:AutoActionMenuItem Header="Pin / Unpin Note" AlephAction="PinUnpinNote" Settings="{Binding Settings}" />
<ctrl:AutoActionMenuItem Header="Lock / Unlock Note" AlephAction="LockUnlockNote" Settings="{Binding Settings}" />
<ctrl:AutoActionMenuItem Header="Export Note" AlephAction="ExportNote" Settings="{Binding Settings}" />
<ctrl:AutoActionMenuItem Header="Delete Note" AlephAction="DeleteNote" Settings="{Binding Settings}" />
<Separator />
<ctrl:AutoActionMenuItem Header="Exit" AlephAction="AppExit" Settings="{Binding Settings}" />
<ctrl:AutoActionMenuItem Header="Export Note" AlephAction="ExportNote" Settings="{Binding Settings}" />
<ctrl:AutoActionMenuItem Header="Delete Note" AlephAction="DeleteNote" Settings="{Binding Settings}" />
<Separator />
<ctrl:AutoActionMenuItem Header="Export all as Markdown + Front Matter" AlephAction="ExportFrontmatter" Settings="{Binding Settings}" />
<Separator />
<ctrl:AutoActionMenuItem Header="Exit" AlephAction="AppExit" Settings="{Binding Settings}" />
</MenuItem>
<MenuItem Header="_Edit">
<ctrl:AutoActionMenuItem Header="Settings" AlephAction="ShowSettings" Settings="{Binding Settings}" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using MSHC.Lang.Collections;
using Ookii.Dialogs.Wpf;
using ScintillaNET;
using System.Web;

namespace AlephNote.WPF.Windows
{
Expand All @@ -30,7 +31,8 @@ public partial class MainWindowViewmodel
public ICommand DuplicateNoteCommand => new RelayCommand(DuplicateNote);
public ICommand PinUnpinNoteCommand => new RelayCommand(PinUnpinNote);
public ICommand LockUnlockNoteCommand => new RelayCommand(LockUnlockNote);
public ICommand InsertHyperlinkCommand => new RelayCommand(InsertHyperlink);
public ICommand ExportFrontmatterCommand => new RelayCommand(ExportFrontmatter);
public ICommand InsertHyperlinkCommand => new RelayCommand(InsertHyperlink);
public ICommand InsertFilelinkCommand => new RelayCommand(InsertFilelink);
public ICommand InsertNotelinkCommand => new RelayCommand(InsertNotelink);
public ICommand InsertMaillinkCommand => new RelayCommand(InsertMaillink);
Expand Down Expand Up @@ -247,7 +249,59 @@ private void LockUnlockNote()
}
}

private void AddTagToNote(string t)
private void ExportFrontmatter()
{
if (SelectedNote == null) return;

var dialog = new VistaFolderBrowserDialog();
if (!(dialog.ShowDialog() ?? false)) return;

try
{
var tnow = DateTime.UtcNow;

var directory = dialog.SelectedPath;
foreach (var note in Repository.Notes)
{
var subpath = note.Path.Enumerate().Select(p => ANFilenameHelper.StripStringForFilename(p)).ToArray();

var filename = ANFilenameHelper.StripStringForFilename(note.Title) + ".md";

var filedir = Path.Combine((new string[] { directory }).AsEnumerable().Concat(subpath).ToArray());

Directory.CreateDirectory(filedir);

var filedest = Path.Combine((new string[] { directory }).AsEnumerable().Concat(subpath).Concat(new string[] { filename }).ToArray());

var mdfm = "---\n";
mdfm += $"id: {note.UniqueName}\n";
mdfm += $"title: {note.Title}\n";
mdfm += $"updated: {note.ModificationDate.ToUniversalTime():yyyy-MM-dd HH:mm:ss}Z\n";
mdfm += $"created: {note.CreationDate.ToUniversalTime():yyyy-MM-dd HH:mm:ss}Z\n";
mdfm += $"exported: {tnow.ToUniversalTime():yyyy-MM-dd HH:mm:ss}Z\n";
mdfm += $"locked: {note.IsLocked.ToString().ToLower()}\n";
mdfm += $"pinned: {note.IsPinned.ToString().ToLower()}\n";
mdfm += $"conflict: {note.IsConflictNote.ToString().ToLower()}\n";
mdfm += $"tags: [{string.Join(", ", note.Tags.Select(p => '"' + HttpUtility.JavaScriptStringEncode(p) + '"'))}]\n";
mdfm += $"path: [{string.Join(", ", note.Path.Enumerate().Select(p => '"' + HttpUtility.JavaScriptStringEncode(p) + '"'))}]\n";
mdfm += $"---\n";
mdfm += $"\n";
mdfm += note.Text;

if (File.Exists(filedest)) { throw new Exception($"File {filedest} already exists"); }

File.WriteAllText(filedest, mdfm, Encoding.UTF8);
}
}
catch (Exception e)
{
App.Logger.Error("Main", "Could not write to file", e);
ExceptionDialog.Show(Owner, "Could not write to file", e, string.Empty);
}
}


private void AddTagToNote(string t)
{
if (SelectedNote == null) return;
if (Settings.IsReadOnlyMode) return;
Expand Down
3 changes: 2 additions & 1 deletion Source/AlephNote.Common/Shortcuts/IShortcutHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public interface IShortcutHandler
ICommand DuplicateNoteCommand { get; }
ICommand PinUnpinNoteCommand { get; }
ICommand LockUnlockNoteCommand { get; }
ICommand InsertHyperlinkCommand { get; }
ICommand ExportFrontmatterCommand { get; }
ICommand InsertHyperlinkCommand { get; }
ICommand InsertFilelinkCommand { get; }
ICommand InsertNotelinkCommand { get; }
ICommand InsertMaillinkCommand { get; }
Expand Down
1 change: 1 addition & 0 deletions Source/AlephNote.Common/Shortcuts/ShortcutManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ static ShortcutManager()
AddCommand(ASS.Window, "DuplicateNote", h => h.DuplicateNoteCommand, "Create a new note as a copy of the current note", ActionModifier.AccessControl);
AddCommand(ASS.Window, "PinUnpinNote", h => h.PinUnpinNoteCommand, "Pin the note to the top (or un-pin the note)", ActionModifier.AccessControl);
AddCommand(ASS.Window, "LockUnlockNote", h => h.LockUnlockNoteCommand, "Lock/Unlock the note (prevent editing)", ActionModifier.AccessControl);
AddCommand(ASS.Window, "ExportFrontmatter", h => h.ExportFrontmatterCommand, "Export all notes as markdown files");

AddCommand(ASS.NoteEdit, "InsertHyperlink", h => h.InsertHyperlinkCommand, "Insert a Hyperlink", ActionModifier.AccessControl);
AddCommand(ASS.NoteEdit, "InsertFilelink", h => h.InsertFilelinkCommand, "Insert a link to a local file", ActionModifier.AccessControl);
Expand Down

0 comments on commit e462199

Please sign in to comment.