Ejemplo n.º 1
0
        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("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");
        }