1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
#include "lix/libcmd/markdown.hh"
#include "lix/libutil/error.hh"
#include "lix/libutil/finally.hh"
#include "lix/libutil/terminal.hh"

#include <cstdlib>
#include <iterator>
#include <new>
#include <regex>
#include <sys/queue.h>
#include <lowdown.h>

namespace nix {

static const std::string DOCROOT = "@docroot@";
static const std::string DOCROOT_URL = "https://docs.lix.systems/manual/lix/stable";

static void processLinks(struct lowdown_node * node)
{
    if (node->type == LOWDOWN_LINK) {
        struct lowdown_buf *link = &node->rndr_link.link;
        if (link && link->size && std::string_view(link->data, link->size).starts_with(DOCROOT)) {
            // link starts with @docroot@, replace that and check the path extension too.
            static std::regex mdRewrite{"\\.md(#.*)?$"}; // NOLINT(lix-foreign-exceptions)

            auto oldLink = std::string_view(link->data, link->size).substr(DOCROOT.size());
            std::string newLink = DOCROOT_URL;
            std::regex_replace(
                std::back_inserter(newLink), oldLink.begin(), oldLink.end(), mdRewrite, ".html$1"
            );
            if (link->maxsize < newLink.size()) {
                // the existing link buffer doesn't have enough space for the new string
                char *newData;
                if (!(newData = static_cast<char *>(std::realloc(link->data, newLink.size())))) {
                    throw std::bad_alloc();
                }
                link->data = newData;
                link->maxsize = newLink.size();
            }
            newLink.copy(link->data, newLink.size());
            link->size = newLink.size();
        }
    } else {
        // recurse into children
        struct lowdown_node *child;
        TAILQ_FOREACH(child, &node->children, entries)
            processLinks(child);
    }
}

std::string renderMarkdownToTerminal(std::string_view markdown, StandardOutputStream fileno)
{
    int windowWidth = getWindowSize().second;
    size_t lowdown_cols = std::max(windowWidth - 5, 60);

    struct lowdown_opts opts{
        .type = LOWDOWN_TERM,
        .term =
            {
                .cols = lowdown_cols,
                .width = 0,
                .hmargin = 0,
                .hpadding = 4,
                .vmargin = 0,
                .centre = 0,
            },
        .maxdepth = 20,
        .feat = LOWDOWN_COMMONMARK | LOWDOWN_FENCED | LOWDOWN_DEFLIST | LOWDOWN_TABLES,
#ifdef LOWDOWN_CONSOLIDATED_OFLAGS
        .oflags = LOWDOWN_NOLINK,
#else
        .oflags = LOWDOWN_TERM_NOLINK,
#endif /* LOWDOWN_CONSOLIDATED_OFLAGS */
    };
    if (!shouldANSI(fileno)) {
        opts.oflags |= LOWDOWN_TERM_NOANSI;
    }

    auto doc = lowdown_doc_new(&opts);
    if (!doc)
        throw Error("cannot allocate Markdown document");
    Finally freeDoc([&]() { lowdown_doc_free(doc); });

    size_t maxn = 0;
    auto node = lowdown_doc_parse(doc, &maxn, markdown.data(), markdown.size(), nullptr);
    if (!node)
        throw Error("cannot parse Markdown document");
    Finally freeNode([&]() { lowdown_node_free(node); });

    processLinks(node);

    auto renderer = lowdown_term_new(&opts);
    if (!renderer)
        throw Error("cannot allocate Markdown renderer");
    Finally freeRenderer([&]() { lowdown_term_free(renderer); });

    auto buf = lowdown_buf_new(16384);
    if (!buf)
        throw Error("cannot allocate Markdown output buffer");
    Finally freeBuffer([&]() { lowdown_buf_free(buf); });

    int rndr_res = lowdown_term_rndr(buf, renderer, node);
    if (!rndr_res)
        throw Error("allocation error while rendering Markdown");

    return std::string(buf->data, buf->size);
}

}