1 /*
2    SPDX-FileCopyrightText: 2020-2021 Laurent Montel <montel@kde.org>
3 
4    SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "markdowndiscount.h"
8 #include <QDebug>
9 extern "C" {
10 #include <mkdio.h>
11 }
12 
MarkdownDiscount()13 MarkdownDiscount::MarkdownDiscount()
14 {
15 }
16 
~MarkdownDiscount()17 MarkdownDiscount::~MarkdownDiscount()
18 {
19 }
20 
toHtml() const21 QString MarkdownDiscount::toHtml() const
22 {
23     if (mText.isEmpty()) {
24         return {};
25     }
26     const QByteArray textArray = mText.toUtf8();
27     MMIOT *markdownHandle = mkd_string(textArray.constData(), textArray.count(), 0);
28     mkd_flag_t flags = MKD_FENCEDCODE | MKD_GITHUBTAGS | MKD_AUTOLINK;
29     if (!mkd_compile(markdownHandle, flags)) {
30         qWarning() << "Failed to compile the Markdown document.";
31         mkd_cleanup(markdownHandle);
32         return {};
33     }
34 
35     char *htmlDocument;
36     const int size = mkd_document(markdownHandle, &htmlDocument);
37 
38     const QString html = QString::fromUtf8(htmlDocument, size);
39     mkd_cleanup(markdownHandle);
40     return html;
41 }
42