1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-19 16:59:35 +01:00

Get rid of lowdown!

`cmark` is a more robust library for constructing Markdown than
`lowdown`. It in particular has the functionality we need to compute the
doc we need to compute. It is also more portable.

For terminal output, there is a new A.I. slop library has the same
license as the C code from lowdown upon which it is based. This keeps
our options open for upstreaming, if we want to do that.

Also, the `builtins.fetchTree` docs are now assembled using the cmark
AST rather than janky string indenting.
This commit is contained in:
John Ericson 2025-11-12 16:35:34 -05:00
parent 09d6847490
commit 4958bf040b
18 changed files with 1550 additions and 136 deletions

View file

@ -1,79 +1,38 @@
#include "nix/cmd/markdown.hh"
#include "nix/util/environment-variables.hh"
#include "nix/util/error.hh"
#include "nix/util/finally.hh"
#include "nix/util/terminal.hh"
#include "cmd-config-private.hh"
#if HAVE_LOWDOWN
# include <sys/queue.h>
# include <lowdown.h>
#endif
#include <cmark/cmark-cpp.hh>
#include <cmark/cmark-terminal.hh>
namespace nix {
#if HAVE_LOWDOWN
static std::string doRenderMarkdownToTerminal(std::string_view markdown)
{
int windowWidth = getWindowSize().second;
# if HAVE_LOWDOWN_1_4
struct lowdown_opts_term opts_term{
.cols = (size_t) std::max(windowWidth - 5, 60),
.hmargin = 0,
.vmargin = 0,
};
# endif
struct lowdown_opts opts{
.type = LOWDOWN_TERM,
# if HAVE_LOWDOWN_1_4
.term = opts_term,
# endif
.maxdepth = 20,
# if !HAVE_LOWDOWN_1_4
.cols = (size_t) std::max(windowWidth - 5, 60),
.hmargin = 0,
.vmargin = 0,
# endif
.feat = LOWDOWN_COMMONMARK | LOWDOWN_FENCED | LOWDOWN_DEFLIST | LOWDOWN_TABLES,
.oflags =
# if HAVE_LOWDOWN_1_4
LOWDOWN_TERM_NORELLINK // To render full links while skipping relative ones
# else
LOWDOWN_TERM_NOLINK
# endif
};
// Set up terminal rendering options
::cmark::TerminalOptions opts;
opts.cols = std::max(windowWidth - 5, 60);
opts.hmargin = 0;
opts.vmargin = 0;
opts.noRelLink = true; // Skip rendering relative links
if (!isTTY())
opts.oflags |= LOWDOWN_TERM_NOANSI;
opts.noAnsi = true;
auto doc = lowdown_doc_new(&opts);
// Parse the markdown document
auto doc = ::cmark::parse_document(markdown, CMARK_OPT_DEFAULT);
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); });
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);
try {
// Render to terminal
return ::cmark::renderTerminal(*doc, opts);
} catch (const std::exception & e) {
throw Error("error rendering Markdown: %s", e.what());
}
}
std::string renderMarkdownToTerminal(std::string_view markdown)
@ -84,11 +43,4 @@ std::string renderMarkdownToTerminal(std::string_view markdown)
return doRenderMarkdownToTerminal(markdown);
}
#else
std::string renderMarkdownToTerminal(std::string_view markdown)
{
return std::string(markdown);
}
#endif
} // namespace nix