Make incoming link list display none if there's nothing

This commit is contained in:
Nat 2023-01-20 18:48:32 -08:00
parent 8dd7876ebc
commit 7f78f1db3c
Signed by: nat
GPG Key ID: B53AB05285D710D6
1 changed files with 26 additions and 16 deletions

View File

@ -478,28 +478,38 @@ int main(int argc, char *argv[]) {
char *incomingTagStart = strstr(renderedPage, "{{incoming}}");
if (incomingTagStart != NULL) {
// Build the incoming links list
int incomingListSize = 37; // <ul class="calathea-incoming">\n</ul>\n
for (int i = 0; i < currentPage->incoming->length; i++) {
// ` <li><a href=\"[permalink]\">[title]</a></li>\n`
struct Page *page = currentPage->incoming->pages[i];
incomingListSize += 27 + strlen(page->title) + strlen(page->permalink);
// <ul class="calathea-incoming">\n</ul>\n
int incomingListSize = 37;
if (currentPage->incoming->length == 0) {
// ` <li>none</li>\n`
incomingListSize += 16;
} else {
for (int i = 0; i < currentPage->incoming->length; i++) {
// ` <li><a href=\"[permalink]\">[title]</a></li>\n`
struct Page *page = currentPage->incoming->pages[i];
incomingListSize += 27 + strlen(page->title) + strlen(page->permalink);
}
}
char *incomingLinksList = malloc((incomingListSize + 1) * sizeof(char));
memset(incomingLinksList, 0, (incomingListSize + 1) * sizeof(char));
strcpy(incomingLinksList, "<ul class=\"calathea-incoming\">\n");
for (int i = 0; i < currentPage->incoming->length; i++) {
struct Page *page = currentPage->incoming->pages[i];
char *link = concat_strings(5,
" <li><a href=\"",
page->permalink,
"\">",
page->title,
"</a></li>\n"
);
strcat(incomingLinksList, link);
free(link);
if (currentPage->incoming->length == 0) {
strcat(incomingLinksList, " <li>none</li>\n");
} else {
for (int i = 0; i < currentPage->incoming->length; i++) {
struct Page *page = currentPage->incoming->pages[i];
char *link = concat_strings(5,
" <li><a href=\"",
page->permalink,
"\">",
page->title,
"</a></li>\n"
);
strcat(incomingLinksList, link);
free(link);
}
}
strcat(incomingLinksList, "</ul>\n");