Skip to content

Commit

Permalink
fix links
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiaswuerth committed Sep 17, 2020
1 parent 4b4e364 commit 905bb14
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
30 changes: 28 additions & 2 deletions ObsidianTools.Test/TestPluginCreateReferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
17 changes: 17 additions & 0 deletions ObsidianTools/plugins/PluginCreateReferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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("[["))
Expand All @@ -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];
}

Expand Down

0 comments on commit 905bb14

Please sign in to comment.