<commit_before>#include "markdown.hh"
#include "util.hh"
#include "finally.hh"

#include <sys/queue.h>
#include <lowdown.h>

namespace nix {

std::string renderMarkdownToTerminal(std::string_view markdown)
{
    struct lowdown_opts opts {
        .type = LOWDOWN_TERM,
        .maxdepth = 20,
        .cols = std::max(getWindowSize().second, (unsigned short) 80),
        .hmargin = 0,
        .vmargin = 0,
        .feat = LOWDOWN_COMMONMARK | LOWDOWN_FENCED | LOWDOWN_DEFLIST | LOWDOWN_TABLES,
        .oflags = 0,
    };

    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());
    if (!node)
        throw Error("cannot parse Markdown document");
    Finally freeNode([&]() { lowdown_node_free(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, nullptr, renderer, node);
    if (!rndr_res)
        throw Error("allocation error while rendering Markdown");

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

}<commit_msg>Respect NO_COLOR in Markdown output<commit_after>#include "markdown.hh"
#include "util.hh"
#include "finally.hh"

#include <sys/queue.h>
#include <lowdown.h>

namespace nix {

std::string renderMarkdownToTerminal(std::string_view markdown)
{
    struct lowdown_opts opts {
        .type = LOWDOWN_TERM,
        .maxdepth = 20,
        .cols = std::max(getWindowSize().second, (unsigned short) 80),
        .hmargin = 0,
        .vmargin = 0,
        .feat = LOWDOWN_COMMONMARK | LOWDOWN_FENCED | LOWDOWN_DEFLIST | LOWDOWN_TABLES,
        .oflags = 0,
    };

    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());
    if (!node)
        throw Error("cannot parse Markdown document");
    Finally freeNode([&]() { lowdown_node_free(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, nullptr, renderer, node);
    if (!rndr_res)
        throw Error("allocation error while rendering Markdown");

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

}
{'fraction_non_alphanumeric': 0.13912475506205094, 'fraction_numerical': 0.008491182233834096, 'mean_word_length': 3.4780701754385963, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5794821', 'n_tokens_mistral': 1057, 'n_tokens_neox': 1012, 'n_words': 233}