diff --git a/README.md b/README.md index 13740c4..6be8a85 100644 --- a/README.md +++ b/README.md @@ -11,9 +11,9 @@ zero-terminated strings. There will likely be memoryleaks and comments that over explain every line of code. ## Features -- [x] Rendering wikilinks -- [x] Ability to keep track of incoming links -- [ ] Named wikilinks (i.e. `[[link title|actual page]]`) +- Rendering wikilinks +- Ability to keep track of incoming links +- Named wikilinks (i.e. `[[link title|actual page]]`) ## installation This tool requires [cmark](https://github.com/commonmark/cmark) to be diff --git a/calathea.c b/calathea.c index a8e0cf4..8cb05d7 100644 --- a/calathea.c +++ b/calathea.c @@ -350,13 +350,24 @@ int main(int argc, char *argv[]) { int linkLength = nextLinkEnd - nextLinkStart; - // Determine the exact title of the link - char title[linkLength - 3]; + char *nextVerticalBar = strchr(nextLinkStart, '|'); - strncpy(title, nextLinkStart + 2, (linkLength - 2)*sizeof(char)); - title[linkLength - 2] = 0; + char *linkTitle = calloc(linkLength - 3, sizeof(char)); + char *linkPageTitle = NULL; - struct Page *linkedPage = map_get(pageMap, mapSize, title); + if (nextVerticalBar != NULL && nextVerticalBar < nextLinkEnd) { + // The link is in the from [[link title|page title]] + strncpy(linkTitle, nextLinkStart + 2, (nextVerticalBar - 1) - (nextLinkStart + 1)); + linkPageTitle = calloc((nextLinkEnd - 2) - nextVerticalBar, sizeof(char)); + strncpy(linkPageTitle, nextVerticalBar + 1, (nextLinkEnd - 1) - nextVerticalBar); + puts(linkPageTitle); + } else { + // The link is of the form [[page title]] + linkPageTitle = linkTitle; + strncpy(linkTitle, nextLinkStart + 2, (linkLength - 2)*sizeof(char)); + } + + struct Page *linkedPage = map_get(pageMap, mapSize, linkPageTitle); char *compiledLink = NULL; @@ -364,12 +375,12 @@ int main(int argc, char *argv[]) { // i.e. the page does not exist compiledLink = concat_strings(3, "", - title, + linkTitle, "" ); } else { page_list_insert(linkedPage->incoming, currentPage); - compiledLink = concat_strings(5, "[", title, "](", linkedPage->permalink, ")"); + compiledLink = concat_strings(5, "[", linkTitle, "](", linkedPage->permalink, ")"); } char *newContent = substitute_string( @@ -378,6 +389,10 @@ int main(int argc, char *argv[]) { currentPage->content = newContent; free(compiledLink); + if (linkTitle != linkPageTitle) { + free(linkPageTitle); + } + free(linkTitle); // Move to the next chunk of the file // NOTE: This is suboptimal, because we search for "[[" from the