Add support for named wikilinks

This commit is contained in:
Nat 2023-01-20 15:43:53 -08:00
parent 1bd489cdc5
commit f4e04b0dfd
Signed by: nat
GPG Key ID: B53AB05285D710D6
2 changed files with 25 additions and 10 deletions

View File

@ -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

View File

@ -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,
"<a class=\"calathea-404\" href=\"#\">",
title,
linkTitle,
"</a>"
);
} 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