From 905bb14b15fbbccde2861f2fe4ee663edee003fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20W=C3=BCrth?= Date: Thu, 17 Sep 2020 20:35:58 +0200 Subject: [PATCH] fix links --- .../TestPluginCreateReferences.cs | 30 +++++++++++++++++-- .../plugins/PluginCreateReferences.cs | 17 +++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/ObsidianTools.Test/TestPluginCreateReferences.cs b/ObsidianTools.Test/TestPluginCreateReferences.cs index 005ae9d..216b0de 100644 --- a/ObsidianTools.Test/TestPluginCreateReferences.cs +++ b/ObsidianTools.Test/TestPluginCreateReferences.cs @@ -28,8 +28,34 @@ public void Test() files.ForEach(f => PrepareFile(GetFilePathForName(f.Name), f.Before)); - PluginCreateReferences plugin = new PluginCreateReferences(); - plugin.Execute(new[] + new PluginCreateReferences().Execute(new[] + { + VaultDirectory + } + , VaultDirectory); + + files.ForEach(f => + { + String path = Path.Combine(VaultDirectory, $"{f.Name}.md"); + String content = File.ReadAllText(path, Encoding.UTF8); + Assert.AreEqual(f.After, content); + }); + } + + [ Test ] + public void Test_Links() + { + List<(String Name, String Before, String After)> files = new List<(String, String, String)> + { + ("Index", "this is Job anjobother Room aroom- [Job Room](https://www.job-room.ch/) url test blub" + , "this is [[Job]] an[[Job|job]]other [[Room]] a[[Room|room]]- [Job Room](https://www.job-room.ch/) url test blub") + , ("Room", "a", "a") + , ("Job", "c", "c") + }; + + files.ForEach(f => PrepareFile(GetFilePathForName(f.Name), f.Before)); + + new PluginCreateReferences().Execute(new[] { VaultDirectory } diff --git a/ObsidianTools/plugins/PluginCreateReferences.cs b/ObsidianTools/plugins/PluginCreateReferences.cs index dbee75b..c91d602 100644 --- a/ObsidianTools/plugins/PluginCreateReferences.cs +++ b/ObsidianTools/plugins/PluginCreateReferences.cs @@ -61,6 +61,8 @@ private static void ProcessFile(MarkdownFile f, String word, String keyword) private static Boolean StringIsPartOfLink(String content) { + // not replace [[a]], and [b g](www.c.de) hijklm + Boolean startedQuote = false; while (0 < content.Length) { if (content.EndsWith("[[")) @@ -73,6 +75,21 @@ private static Boolean StringIsPartOfLink(String content) return false; } + if (content.EndsWith("[")) + { + return true; + } + + if (content.EndsWith("(")) + { + startedQuote = true; + } + + if (content.EndsWith("]")) + { + return startedQuote; + } + content = content[..^1]; }