File indexing completed on 2025-01-19 04:46:50

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "markdowndiscount.h"
0008 #include <QDebug>
0009 extern "C" {
0010 #include <mkdio.h>
0011 }
0012 
0013 MarkdownDiscount::MarkdownDiscount() = default;
0014 
0015 MarkdownDiscount::~MarkdownDiscount() = default;
0016 
0017 QString MarkdownDiscount::toHtml() const
0018 {
0019     if (mText.isEmpty()) {
0020         return {};
0021     }
0022     const QByteArray textArray = mText.toUtf8();
0023 #if defined(MKD_NOLINKS)
0024     // on discount 2 MKD_NOLINKS is a define
0025     MMIOT *markdownHandle = mkd_string(textArray.constData(), textArray.size(), 0);
0026     mkd_flag_t flags = MKD_FENCEDCODE | MKD_GITHUBTAGS | MKD_AUTOLINK;
0027     if (!mkd_compile(markdownHandle, flags)) {
0028         qWarning() << "Failed to compile the Markdown document.";
0029         mkd_cleanup(markdownHandle);
0030         return {};
0031     }
0032 #else
0033     // on discount 3 MKD_NOLINKS is an enum value
0034     MMIOT *markdownHandle = mkd_string(textArray.constData(), textArray.size(), nullptr);
0035     mkd_flag_t *flags = mkd_flags();
0036     mkd_set_flag_bitmap(flags, MKD_FENCEDCODE | MKD_GITHUBTAGS | MKD_AUTOLINK | MKD_TOC | MKD_IDANCHOR);
0037     if (!mkd_compile(markdownHandle, flags)) {
0038         qWarning() << "Failed to compile the Markdown document.";
0039         mkd_cleanup(markdownHandle);
0040         mkd_free_flags(flags);
0041         return {};
0042     }
0043     mkd_free_flags(flags);
0044 #endif
0045 
0046     char *htmlDocument;
0047     const int size = mkd_document(markdownHandle, &htmlDocument);
0048 
0049     const QString html = QString::fromUtf8(htmlDocument, size);
0050     mkd_cleanup(markdownHandle);
0051     return html;
0052 }