protected override void Handle(PluginPayload payload)
        {
            Console.WriteLine("Searching for dead links...");
            IEnumerable <String> filePaths = GetMarkdownFilePaths(payload.VaultDirectory);
            List <MarkdownFile>  files     = ReadFiles(filePaths);
            String outputPath = $"obsidiantools-output-dead-{DateTime.Now:yyyyMMdd-HHmmss}.md";

            using (StreamWriter writer = File.CreateText(outputPath))
            {
                writer.WriteLine("### Dead Links");
                writer.WriteLine("The following dead links were found:");
                foreach (MarkdownFile file in files)
                {
                    MarkdownLink fileLink = MarkdownLink.ForFile(file);
                    foreach (MarkdownLink link in file.Links)
                    {
                        String filePath = FileHelper.GetAbsoluteFileName(payload.VaultDirectory, link.FileName);
                        if (!File.Exists(filePath))
                        {
                            writer.WriteLine($" - Referenced file NOT FOUND @ {fileLink} -> {link} (`{filePath}`)");
                        }
                        else if (!FileHelper.HasContent(filePath))
                        {
                            writer.WriteLine($" - Referenced file IS EMPTY @ {fileLink} -> {link} (`{filePath}`)");
                        }
                    }
                }
            }

            Console.WriteLine("Searching for dead links done");
            Console.WriteLine($"Creating result file done, you can find it here: {outputPath}");
        }
        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}");
        }
        protected override void Handle(PluginPayload payload)
        {
            IEnumerable <String> filePaths = GetMarkdownFilePaths(payload.VaultDirectory).ToList();
            List <MarkdownFile>  files     = ReadFiles(filePaths);
            List <WordStatistic> stats     = AnalyzeMarkdownFiles(files);

            FindSimilarWords(stats);
            FindWordReferences(files, stats);
            CreateResultFile(stats);
        }
Beispiel #4
0
        protected override void Handle(PluginPayload payload)
        {
            Console.WriteLine("Starting to cleanup...");
            List <String>       filePaths = GetMarkdownFilePaths(payload.VaultDirectory).ToList();
            List <MarkdownFile> files     = ReadFiles(filePaths);
            Int32 replacementsTotal       = 0;

            foreach (MarkdownFile file in files)
            {
                try
                {
                    if (!file.IsHealthy() && FileHelper.TryDeleteEmptyFile(file.Info.FullName))
                    {
                        continue;
                    }

                    // get all links
                    String text         = file.Content;
                    Int32  replacements = 0;
                    foreach (MarkdownLink link in file.Links)
                    {
                        // validate existance
                        String filePath = FileHelper.GetAbsoluteFileName(payload.VaultDirectory, link.FileName);
                        if (!FileHelper.TryDeleteEmptyFile(filePath))
                        {
                            continue;
                        }

                        // cleanup text
                        Console.WriteLine($"Dead link found: link '{filePath}' for match '{link}'");
                        text = text.Replace(link.ToString(), link.GetCleanedDisplayName()).Trim();
                        replacements++;
                    }

                    if (0 < replacements)
                    {
                        // persist cleaned up text
                        File.WriteAllText(file.Info.FullName, text, new UTF8Encoding(false));
                        replacementsTotal += replacements;
                    }
                }
                catch (Exception x)
                {
                    LogHelper.LogException($"An error occurred cleaning up in path '{file.Info.FullName}'", x);
                }
            }

            Console.WriteLine($"Cleaning up done, removed {replacementsTotal} dead links");
        }
        protected override void Handle(PluginPayload payload)
        {
            HashSet <String> keywords = GetKeywords(payload.ConsoleArguments);

            if (1 > keywords.Count)
            {
                Console.WriteLine("No valid keyword found");
                return;
            }

            IEnumerable <String> filePaths = GetMarkdownFilePaths(payload.VaultDirectory);
            List <MarkdownFile>  files     = FindReferences(filePaths, keywords).ToList();

            CreateResultFile(keywords, files);
        }
        protected override void Handle(PluginPayload payload)
        {
            Console.WriteLine("Creating dead links...");
            IEnumerable <String> filePaths = GetMarkdownFilePaths(payload.VaultDirectory);
            List <MarkdownFile>  files     = ReadFiles(filePaths);
            List <MarkdownLink>  newLinks  = new List <MarkdownLink>();
            String outputPath = $"obsidiantools-output-created-{DateTime.Now:yyyyMMdd-HHmmss}.md";

            foreach (MarkdownFile file in files)
            {
                MarkdownLink fileLink = MarkdownLink.ForFile(file);
                foreach (MarkdownLink link in file.Links)
                {
                    String filePath = FileHelper.GetAbsoluteFileName(payload.VaultDirectory, link.FileName);
                    if (File.Exists(filePath))
                    {
                        continue;
                    }

                    File.WriteAllText(filePath
                                      , "_automatically created by [obsidian-tools](https://github.com/tobiaswuerth/obsidian-tools)_ ==#todo==");
                    MarkdownLink newLink = MarkdownLink.ForFile(link.FileName);
                    newLinks.Add(newLink);
                }
            }

            using (StreamWriter writer = File.CreateText(outputPath))
            {
                writer.WriteLine("Created the following files and tagged them with #todo");
                writer.WriteLine("### Newly Created Files");
                String references = CollectionHelper.CommaSeparatedList(newLinks.Select(n => n.ToString()));
                writer.WriteLine(references);
            }

            Console.WriteLine("Created dead links");
            Console.WriteLine($"Creating result file done, you can find it here: {outputPath}");
            Console.WriteLine(
                "Note: You might have to restart your Obsidian.md client in order to correctly index all new files");
        }
Beispiel #7
0
        protected override void Handle(PluginPayload payload)
        {
            Console.WriteLine("Creating references...");
            IEnumerable <String> filePaths = GetMarkdownFilePaths(payload.VaultDirectory);
            List <MarkdownFile>  files     = ReadFiles(filePaths);

            files.Sort((a, b) => - 1 * a.Info.Name.Length.CompareTo(b.Info.Name.Length));
            foreach (MarkdownFile file in files)
            {
                FileInfo info = file.Info;
                String   word = info.Name.Replace(info.Extension, String.Empty);
                Console.WriteLine($"Processing '{word}'...");
                files.Except(new [] { file }).ToList().ForEach(f =>
                {
                    ProcessFile(f, word, word);
                    ProcessFile(f, word, word.ToLower());
                    ProcessFile(f, word, word.ToUpper());
                });
            }

            Console.WriteLine("Updating files...");
            files.ForEach(f => f.SaveChanges());
            Console.WriteLine("Created references");
        }
Beispiel #8
0
 protected override void Handle(PluginPayload payload)
 {
     LinkCount = GetMarkdownFilePaths(payload.VaultDirectory).Count();
     Console.WriteLine($"Found {LinkCount} markdown files in given directory");
 }
Beispiel #9
0
        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");
        }
Beispiel #10
0
 protected abstract void Handle(PluginPayload payload);