fix outline navigation bug using Hoedown

Use a fixed base html and update the body using VDocument.html like
Marked does.

Signed-off-by: Le Tan <tamlokveer@gmail.com>
This commit is contained in:
Le Tan 2016-11-09 21:43:11 +08:00
parent 454072c9ba
commit 8a214831e3
5 changed files with 27 additions and 4 deletions

View File

@ -12,17 +12,25 @@
<script>hljs.initHighlightingOnLoad();</script>
</head>
<body>
<div id="placeholder"></div>
<script>
'use strict';
var placeholder = document.getElementById('placeholder');
var content;
var scrollToAnchor = function(anchor) {
document.getElementById(anchor).scrollIntoView();
};
var updateHtml = function(html) {
placeholder.innerHTML = html;
}
new QWebChannel(qt.webChannelTransport,
function(channel) {
content = channel.objects.content;
updateHtml(content.html);
content.htmlChanged.connect(updateHtml);
content.requestScrollToAnchor.connect(scrollToAnchor);
}
);
@ -35,7 +43,7 @@
return;
}
var curIdx = 0;
var biaScrollTop = scrollTop + 20;
var biaScrollTop = scrollTop + 50;
for (var i = 0; i < eles.length; ++i) {
if (biaScrollTop >= eles[i].offsetTop) {
curIdx = i;

View File

@ -166,7 +166,7 @@
return;
}
var curIdx = 0;
var biaScrollTop = scrollTop + 20;
var biaScrollTop = scrollTop + 50;
for (var i = 0; i < eles.length; ++i) {
if (biaScrollTop >= eles[i].offsetTop) {
curIdx = i;

View File

@ -52,3 +52,12 @@ void VDocument::setHeader(const QString &anchor)
m_header = anchor;
emit headerChanged(m_header);
}
void VDocument::setHtml(const QString &html)
{
if (html == m_html) {
return;
}
m_html = html;
emit htmlChanged(m_html);
}

View File

@ -9,6 +9,7 @@ class VDocument : public QObject
Q_OBJECT
Q_PROPERTY(QString text MEMBER m_text NOTIFY textChanged)
Q_PROPERTY(QString toc MEMBER m_toc NOTIFY tocChanged)
Q_PROPERTY(QString html MEMBER m_html NOTIFY htmlChanged)
public:
explicit VDocument(QObject *parent = 0);
@ -17,6 +18,7 @@ public:
QString getText();
QString getToc();
void scrollToAnchor(const QString &anchor);
void setHtml(const QString &html);
public slots:
// Will be called in the HTML side
@ -28,11 +30,13 @@ signals:
void tocChanged(const QString &toc);
void requestScrollToAnchor(const QString &anchor);
void headerChanged(const QString &anchor);
void htmlChanged(const QString &html);
private:
QString m_text;
QString m_toc;
QString m_header;
QString m_html;
};
#endif // VDOCUMENT_H

View File

@ -117,8 +117,7 @@ void VEditTab::previewByConverter()
QRegularExpression tocExp("<p>\\[TOC\\]<\\/p>", QRegularExpression::CaseInsensitiveOption);
QString toc = mdConverter.generateToc(content, vconfig.getMarkdownExtensions());
html.replace(tocExp, toc);
QString completeHtml = VNote::preTemplateHtml + html + VNote::postTemplateHtml;
webPreviewer->setHtml(completeHtml, QUrl::fromLocalFile(noteFile->basePath + QDir::separator()));
document.setHtml(html);
// Hoedown will add '\n' while Marked does not
updateTocFromHtml(toc.replace("\n", ""));
}
@ -228,6 +227,9 @@ void VEditTab::setupMarkdownPreview()
if (mdConverterType == MarkdownConverterType::Marked) {
webPreviewer->setHtml(VNote::templateHtml,
QUrl::fromLocalFile(noteFile->basePath + QDir::separator()));
} else {
webPreviewer->setHtml(VNote::preTemplateHtml + VNote::postTemplateHtml,
QUrl::fromLocalFile(noteFile->basePath + QDir::separator()));
}
addWidget(webPreviewer);