support MathJax

Work well with Hoedown and Markdown-it.
This commit is contained in:
Le Tan 2017-03-23 20:16:35 +08:00
parent b9f8144f29
commit a9b34955ee
12 changed files with 68 additions and 1 deletions

View File

@ -23,5 +23,9 @@ var updateHtml = function(html) {
} }
} }
} }
if (VEnableMathjax && (typeof MathJax != 'undefined')) {
MathJax.Hub.Queue(["Typeset", MathJax.Hub, placeholder]);
}
} }

View File

@ -169,5 +169,8 @@ var updateText = function(text) {
placeholder.innerHTML = html; placeholder.innerHTML = html;
handleToc(needToc); handleToc(needToc);
renderMermaid('lang-mermaid'); renderMermaid('lang-mermaid');
if (VEnableMathjax) {
MathJax.Hub.Queue(["Typeset", MathJax.Hub, placeholder]);
}
} }

View File

@ -24,6 +24,10 @@ if (typeof VEnableMermaid == 'undefined') {
}); });
} }
if (typeof VEnableMathjax == 'undefined') {
VEnableMathjax = false;
}
var scrollToAnchor = function(anchor) { var scrollToAnchor = function(anchor) {
var anc = document.getElementById(anchor); var anc = document.getElementById(anchor);
if (anc != null) { if (anc != null) {

View File

@ -122,5 +122,8 @@ var updateText = function(text) {
placeholder.innerHTML = html; placeholder.innerHTML = html;
handleToc(needToc); handleToc(needToc);
renderMermaid('lang-mermaid'); renderMermaid('lang-mermaid');
if (VEnableMathjax) {
MathJax.Hub.Queue(["Typeset", MathJax.Hub, placeholder]);
}
}; };

View File

@ -14,6 +14,7 @@ editor_font_size=12
; 0 - Hoedown, 1 - Marked, 2 - Markdown-it ; 0 - Hoedown, 1 - Marked, 2 - Markdown-it
markdown_converter=2 markdown_converter=2
enable_mermaid=true enable_mermaid=true
enable_mathjax=true
[session] [session]
tools_dock_checked=true tools_dock_checked=true

View File

@ -49,7 +49,7 @@ void VConfigManager::initialize()
markdownExtensions = hoedown_extensions(HOEDOWN_EXT_TABLES | HOEDOWN_EXT_FENCED_CODE | markdownExtensions = hoedown_extensions(HOEDOWN_EXT_TABLES | HOEDOWN_EXT_FENCED_CODE |
HOEDOWN_EXT_HIGHLIGHT | HOEDOWN_EXT_AUTOLINK | HOEDOWN_EXT_HIGHLIGHT | HOEDOWN_EXT_AUTOLINK |
HOEDOWN_EXT_QUOTE | HOEDOWN_EXT_MATH); HOEDOWN_EXT_QUOTE | HOEDOWN_EXT_MATH | HOEDOWN_EXT_MATH_EXPLICIT);
mdConverterType = (MarkdownConverterType)getConfigFromSettings("global", "markdown_converter").toInt(); mdConverterType = (MarkdownConverterType)getConfigFromSettings("global", "markdown_converter").toInt();
tabStopWidth = getConfigFromSettings("global", "tab_stop_width").toInt(); tabStopWidth = getConfigFromSettings("global", "tab_stop_width").toInt();
@ -85,6 +85,8 @@ void VConfigManager::initialize()
m_language = getConfigFromSettings("global", "language").toString(); m_language = getConfigFromSettings("global", "language").toString();
m_enableMermaid = getConfigFromSettings("global", "enable_mermaid").toBool(); m_enableMermaid = getConfigFromSettings("global", "enable_mermaid").toBool();
m_enableMathjax = getConfigFromSettings("global", "enable_mathjax").toBool();
} }
void VConfigManager::readPredefinedColorsFromSettings() void VConfigManager::readPredefinedColorsFromSettings()

View File

@ -120,6 +120,9 @@ public:
inline bool getEnableMermaid() const; inline bool getEnableMermaid() const;
inline void setEnableMermaid(bool p_enabled); inline void setEnableMermaid(bool p_enabled);
inline bool getEnableMathjax() const;
inline void setEnableMathjax(bool p_enabled);
private: private:
void updateMarkdownEditStyle(); void updateMarkdownEditStyle();
QVariant getConfigFromSettings(const QString &section, const QString &key); QVariant getConfigFromSettings(const QString &section, const QString &key);
@ -181,6 +184,9 @@ private:
// Enable Mermaid. // Enable Mermaid.
bool m_enableMermaid; bool m_enableMermaid;
// Enable Mathjax.
bool m_enableMathjax;
// The name of the config file in each directory // The name of the config file in each directory
static const QString dirConfigFileName; static const QString dirConfigFileName;
// The name of the default configuration file // The name of the default configuration file
@ -512,4 +518,18 @@ inline void VConfigManager::setEnableMermaid(bool p_enabled)
setConfigToSettings("global", "enable_mermaid", m_enableMermaid); setConfigToSettings("global", "enable_mermaid", m_enableMermaid);
} }
inline bool VConfigManager::getEnableMathjax() const
{
return m_enableMathjax;
}
inline void VConfigManager::setEnableMathjax(bool p_enabled)
{
if (m_enableMathjax == p_enabled) {
return;
}
m_enableMathjax = p_enabled;
setConfigToSettings("global", "enable_mathjax", m_enableMathjax);
}
#endif // VCONFIGMANAGER_H #endif // VCONFIGMANAGER_H

View File

@ -304,6 +304,17 @@ void VEditTab::setupMarkdownPreview()
"<script>var VEnableMermaid = true;</script>\n"; "<script>var VEnableMermaid = true;</script>\n";
} }
if (vconfig.getEnableMathjax()) {
extraFile += "<script type=\"text/x-mathjax-config\">"
"MathJax.Hub.Config({\n"
" tex2jax: {inlineMath: [['$','$'], ['\\\\(','\\\\)']]},\n"
" showProcessingMessages: false,\n"
" messageStyle: \"none\"});\n"
"</script>\n"
"<script type=\"text/javascript\" async src=\"" + VNote::c_mathjaxJsFile + "\"></script>\n" +
"<script>var VEnableMathjax = true;</script>\n";
}
QString htmlTemplate = VNote::s_markdownTemplate; QString htmlTemplate = VNote::s_markdownTemplate;
htmlTemplate.replace(jsHolder, jsFile); htmlTemplate.replace(jsHolder, jsFile);
if (!extraFile.isEmpty()) { if (!extraFile.isEmpty()) {

View File

@ -330,6 +330,15 @@ void VMainWindow::initMarkdownMenu()
markdownMenu->addAction(mermaidAct); markdownMenu->addAction(mermaidAct);
mermaidAct->setChecked(vconfig.getEnableMermaid()); mermaidAct->setChecked(vconfig.getEnableMermaid());
QAction *mathjaxAct = new QAction(tr("Math&jax"), this);
mathjaxAct->setStatusTip(tr("Enable Mathjax for math support in Markdown"));
mathjaxAct->setCheckable(true);
connect(mathjaxAct, &QAction::triggered,
this, &VMainWindow::enableMathjax);
markdownMenu->addAction(mathjaxAct);
mathjaxAct->setChecked(vconfig.getEnableMathjax());
} }
void VMainWindow::initViewMenu() void VMainWindow::initViewMenu()
@ -607,6 +616,11 @@ void VMainWindow::enableMermaid(bool p_checked)
vconfig.setEnableMermaid(p_checked); vconfig.setEnableMermaid(p_checked);
} }
void VMainWindow::enableMathjax(bool p_checked)
{
vconfig.setEnableMathjax(p_checked);
}
void VMainWindow::changeHighlightCursorLine(bool p_checked) void VMainWindow::changeHighlightCursorLine(bool p_checked)
{ {
vconfig.setHighlightCursorLine(p_checked); vconfig.setHighlightCursorLine(p_checked);

View File

@ -62,6 +62,7 @@ private slots:
void handleFindDialogTextChanged(const QString &p_text, uint p_options); void handleFindDialogTextChanged(const QString &p_text, uint p_options);
void openFindDialog(); void openFindDialog();
void enableMermaid(bool p_checked); void enableMermaid(bool p_checked);
void enableMathjax(bool p_checked);
protected: protected:
void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE; void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE;

View File

@ -22,6 +22,7 @@ const QString VNote::c_mermaidApiJsFile = ":/utils/mermaid/mermaidAPI.min.js";
const QString VNote::c_mermaidCssFile = ":/utils/mermaid/mermaid.css"; const QString VNote::c_mermaidCssFile = ":/utils/mermaid/mermaid.css";
const QString VNote::c_mermaidDarkCssFile = ":/utils/mermaid/mermaid.dark.css"; const QString VNote::c_mermaidDarkCssFile = ":/utils/mermaid/mermaid.dark.css";
const QString VNote::c_mermaidForestCssFile = ":/utils/mermaid/mermaid.forest.css"; const QString VNote::c_mermaidForestCssFile = ":/utils/mermaid/mermaid.forest.css";
const QString VNote::c_mathjaxJsFile = "https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_CHTML";
VNote::VNote(QObject *parent) VNote::VNote(QObject *parent)
: QObject(parent), m_mainWindow(dynamic_cast<VMainWindow *>(parent)) : QObject(parent), m_mainWindow(dynamic_cast<VMainWindow *>(parent))

View File

@ -46,6 +46,9 @@ public:
static const QString c_mermaidDarkCssFile; static const QString c_mermaidDarkCssFile;
static const QString c_mermaidForestCssFile; static const QString c_mermaidForestCssFile;
// Mathjax
static const QString c_mathjaxJsFile;
inline const QVector<QPair<QString, QString> > &getPalette() const; inline const QVector<QPair<QString, QString> > &getPalette() const;
void initPalette(QPalette palette); void initPalette(QPalette palette);
QString getColorFromPalette(const QString &p_name) const; QString getColorFromPalette(const QString &p_name) const;