-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
454 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using NUnit.Framework; | ||
using ObsidianTools.plugins; | ||
|
||
namespace ObsidianTools.Test | ||
{ | ||
public class TestPluginCleanupAssets : BaseTest | ||
{ | ||
[ Test ] | ||
public void Test() | ||
{ | ||
PrepareFile(Path.Join(VaultDirectory, "index.md"), "this is a test for ![[image askdl - 27.png]] image embedding"); | ||
PrepareFile(Path.Join(VaultDirectory, "image askdl - 27.png"), "a"); | ||
PrepareFile(Path.Join(VaultDirectory, "image2.png"), "b"); | ||
PrepareFile(Path.Join(VaultDirectory, "image 3 more.png"), "d"); | ||
PrepareFile(Path.Join(VaultDirectory, "style.css"), "d"); | ||
new PluginCleanupAssets().Execute(new[] | ||
{ | ||
VaultDirectory | ||
} | ||
, VaultDirectory); | ||
|
||
String targetDir = Path.Join(VaultDirectory, "_UNUSED"); | ||
Assert.IsTrue(Directory.Exists(targetDir)); | ||
String[] files = Directory.GetFiles(targetDir); | ||
Assert.AreEqual(2, files.Length); | ||
Assert.IsTrue(files.Contains(Path.Join(targetDir, "image2.png"))); | ||
Assert.IsFalse(files.Contains(Path.Join(targetDir, "style.css"))); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.IO; | ||
using NUnit.Framework; | ||
using ObsidianTools.plugins; | ||
|
||
namespace ObsidianTools.Test | ||
{ | ||
public class TestPluginIdentifyHotspots : BaseTest | ||
{ | ||
[ Test ] | ||
public void Test() | ||
{ | ||
PrepareFile(GetFilePathForName("Index"), "[[a1]] [[a2]] [[b]]"); | ||
PrepareFile(GetFilePathForName("a1"), "[[a1.1]] [[a1.2]] [[a1.3]], siehe auch: [[a2]]"); | ||
PrepareFile(GetFilePathForName("a2"), "[[a2.1]] [[a2.2]]"); | ||
PrepareFile(GetFilePathForName("b"), "[[b1]] [[b2]] [[b3]] [[b4]] [[b5]]"); | ||
PrepareFile(GetFilePathForName("b1"), "[[b]] [[b1.1]]"); | ||
PrepareFile(GetFilePathForName("b2"), "[[b]] [[b2.1]]"); | ||
PrepareFile(GetFilePathForName("b3"), "[[b]] [[b3.1]]"); | ||
PrepareFile(GetFilePathForName("b4"), "[[b]] [[b4.1]]"); | ||
PrepareFile(GetFilePathForName("b5"), "[[b]] [[b5.1]]"); | ||
PrepareFile(GetFilePathForName("b5.1"), "[[b5]] [[b5.1]]"); | ||
new PluginIdentifyHotspots().Execute(new[] | ||
{ | ||
VaultDirectory | ||
} | ||
, VaultDirectory); | ||
|
||
MarkdownFile file = GetPluginOutputFile(); | ||
Assert.IsTrue(file.IsHealthy()); | ||
Assert.AreEqual(10, file.Links.Count); | ||
Assert.IsTrue(file.Content.Contains("6x [[b]]")); | ||
Assert.IsTrue(file.Content.Contains("2x [[b5.1]]")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using NUnit.Framework; | ||
using ObsidianTools.plugins; | ||
|
||
namespace ObsidianTools.Test | ||
{ | ||
public class TestPluginReduceNoise : BaseTest | ||
{ | ||
[ Test ] | ||
public void Test() | ||
{ | ||
PrepareFile(GetFilePathForName("Index"), "[[a1]] [[a2]] [[b]]"); | ||
PrepareFile(GetFilePathForName("a1"), "[[a1.1]] [[a1.2]] [[a1.3]], siehe auch: [[a2]]"); | ||
PrepareFile(GetFilePathForName("a2"), "[[a2.1]] [[a2.2]]"); | ||
PrepareFile(GetFilePathForName("b"), "[[b1]] [[b2]] [[b3]] [[b4]] [[b5]]"); | ||
PrepareFile(GetFilePathForName("b1"), "[[b]] [[b1.1]]"); | ||
PrepareFile(GetFilePathForName("b2"), "[[b]] [[b2.1]]"); | ||
PrepareFile(GetFilePathForName("b3"), "[[b]] [[b3.1]]"); | ||
PrepareFile(GetFilePathForName("b4"), "[[b]] [[b4.1]]"); | ||
PrepareFile(GetFilePathForName("b5"), "[[b]] [[b5.1]]"); | ||
PrepareFile(GetFilePathForName("b5.1"), "[[b5]] [[b5.1]]"); | ||
|
||
new PluginReduceNoise().Execute(new[] | ||
{ | ||
VaultDirectory, | ||
"b", "b" | ||
} | ||
, VaultDirectory); | ||
|
||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace ObsidianTools.plugins | ||
{ | ||
public class PluginCleanupAssets : Plugin | ||
{ | ||
public PluginCleanupAssets() : base("--cleanup-assets", "Move all unreferenced [jpg|jpeg|pdf|png] files into subfolder .\\_UNUSED\\") { } | ||
|
||
protected override void Handle(PluginPayload payload) | ||
{ | ||
Console.WriteLine("Starting to cleanup-assets..."); | ||
List<String> unusedVaultFiles = GetVaultFilePaths(payload.VaultDirectory).ToList(); | ||
unusedVaultFiles.RemoveAll(p => p.EndsWith(".md")); | ||
List<String> markdownFiles = GetMarkdownFilePaths(payload.VaultDirectory).ToList(); | ||
List<MarkdownFile> files = ReadFiles(markdownFiles); | ||
Int32 movementsTotal = 0; | ||
List<String> relevantFileExtensions = new List<String> | ||
{ | ||
".jpg" | ||
, ".jpeg" | ||
, ".pdf" | ||
, ".png" | ||
}; | ||
unusedVaultFiles.RemoveAll(f => | ||
{ | ||
String lowerName = f.ToLower(); | ||
return !relevantFileExtensions.Any(e => lowerName.EndsWith(e)); | ||
}); | ||
foreach (MarkdownFile file in files) | ||
{ | ||
try | ||
{ | ||
if (!file.IsHealthy() && FileHelper.TryDeleteEmptyFile(file.Info.FullName)) | ||
{ | ||
continue; | ||
} | ||
|
||
// get all links | ||
IEnumerable<String> referencedFiles = file.LinksInklEmbedded.Select(l => FileHelper.GetAbsoluteFileName(payload.VaultDirectory, l.FileName)); | ||
unusedVaultFiles = unusedVaultFiles.Except(referencedFiles).ToList(); | ||
} | ||
catch (Exception x) | ||
{ | ||
LogHelper.LogException($"An error occurred cleaning up in path '{file.Info.FullName}'", x); | ||
} | ||
} | ||
|
||
String targetDirectory = Path.Join(payload.VaultDirectory, "_UNUSED"); | ||
if (!Directory.Exists(targetDirectory)) | ||
{ | ||
Console.WriteLine("Creating \\_UNUSED target directory..."); | ||
Directory.CreateDirectory(targetDirectory); | ||
Console.WriteLine("Creating \\_UNUSED target directory done"); | ||
} | ||
|
||
Console.WriteLine($"Moving assets ({unusedVaultFiles.Count}x) assets:"); | ||
|
||
foreach (String unusedVaultFile in unusedVaultFiles) | ||
{ | ||
String fileName = Path.GetFileName(unusedVaultFile); | ||
String newFileName = Path.Join(targetDirectory, fileName); | ||
Console.WriteLine($" > Moving asset {unusedVaultFiles} -> {newFileName}..."); | ||
File.Move(unusedVaultFile, newFileName); | ||
Console.WriteLine($" < Moving asset done"); | ||
} | ||
|
||
Console.WriteLine("Cleanup of unreferenced assets done"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace ObsidianTools.plugins | ||
{ | ||
public class PluginIdentifyHotspots : Plugin | ||
{ | ||
public PluginIdentifyHotspots() : base("--identify-hotspots", "Find the top nodes which are most often linked to") { } | ||
|
||
protected override void Handle(PluginPayload payload) | ||
{ | ||
Console.WriteLine("Starting to identify-hotspots..."); | ||
List<String> markdownFiles = GetMarkdownFilePaths(payload.VaultDirectory).ToList(); | ||
List<MarkdownFile> files = ReadFiles(markdownFiles); | ||
Dictionary<String, HashSet<String>> referenceCount = CreateReferenceDict(files); | ||
|
||
Console.WriteLine($"Found the following reference counds:"); | ||
List<KeyValuePair<String, HashSet<String>>> orderedResult = referenceCount.OrderByDescending(kv => kv.Value.Count).ToList(); | ||
|
||
String outputPath = $"obsidiantools-output-hotspots-{DateTime.Now:yyyyMMdd-HHmmss}.md"; | ||
using (StreamWriter writer = File.CreateText(outputPath)) | ||
{ | ||
writer.WriteLine("### Identified Hotspots"); | ||
writer.WriteLine("The following were counted:"); | ||
foreach ((String source, HashSet<String> linkedTo) in orderedResult.Take(10)) | ||
{ | ||
writer.WriteLine($" - {linkedTo.Count}x [[{source}]]"); | ||
Console.WriteLine($" - {linkedTo.Count}x {source}"); | ||
} | ||
} | ||
|
||
Console.WriteLine("Identify-hotspots done"); | ||
Console.WriteLine($"Creating result file done, you can find it here: {outputPath}"); | ||
} | ||
|
||
public static Dictionary<String, HashSet<String>> CreateReferenceDict(List<MarkdownFile> files) | ||
{ | ||
Dictionary<String, HashSet<String>> referenceCount = new Dictionary<String, HashSet<String>>(); | ||
foreach (MarkdownFile file in files) | ||
{ | ||
try | ||
{ | ||
if (!file.IsHealthy() && FileHelper.TryDeleteEmptyFile(file.Info.FullName)) | ||
{ | ||
continue; | ||
} | ||
|
||
foreach (MarkdownLink link in file.Links) | ||
{ | ||
if (referenceCount.ContainsKey(link.FileName)) | ||
{ | ||
referenceCount[link.FileName].Add(file.Info.Name); | ||
} | ||
else | ||
{ | ||
referenceCount[link.FileName] = new HashSet<String> | ||
{ | ||
file.Info.Name | ||
}; | ||
} | ||
} | ||
} | ||
catch (Exception x) | ||
{ | ||
LogHelper.LogException($"An error occurred cleaning up in path '{file.Info.FullName}'", x); | ||
} | ||
} | ||
|
||
return referenceCount; | ||
} | ||
} | ||
} |
Oops, something went wrong.