mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 13:59:52 +08:00
PegParser: update PegMarkdownHighlight for MathJax
1. Support $ in $$..$$; 2. Support raw display formula \begin{}...\end{}
This commit is contained in:
parent
439f0013c9
commit
6a470ba2f4
@ -128,6 +128,7 @@ enum pmh_extensions
|
|||||||
pmh_EXT_FRONTMATTER = (1 << 2), /**< YAML meta data */
|
pmh_EXT_FRONTMATTER = (1 << 2), /**< YAML meta data */
|
||||||
pmh_EXT_MATH = (1 << 3), /**< Math */
|
pmh_EXT_MATH = (1 << 3), /**< Math */
|
||||||
pmh_EXT_MARK = (1 << 4), /**< HTML <mark> tag content */
|
pmh_EXT_MARK = (1 << 4), /**< HTML <mark> tag content */
|
||||||
|
pmh_EXT_MATH_RAW = (1 << 5), /**< Math in format \begin and \end */
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -267,6 +267,16 @@ void PegHighlighterResult::parseFencedCodeBlocks(const PegMarkdownHighlighter *p
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline bool isDisplayFormulaRawEnd(const QString &p_text)
|
||||||
|
{
|
||||||
|
QRegExp regex("\\\\end\\{[^{}\\s\\r\\n]+\\}$");
|
||||||
|
if (p_text.indexOf(regex) > -1) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
void PegHighlighterResult::parseMathjaxBlocks(const PegMarkdownHighlighter *p_peg,
|
void PegHighlighterResult::parseMathjaxBlocks(const PegMarkdownHighlighter *p_peg,
|
||||||
const QSharedPointer<PegParseResult> &p_result)
|
const QSharedPointer<PegParseResult> &p_result)
|
||||||
{
|
{
|
||||||
@ -302,6 +312,7 @@ void PegHighlighterResult::parseMathjaxBlocks(const PegMarkdownHighlighter *p_pe
|
|||||||
VMathjaxBlock item;
|
VMathjaxBlock item;
|
||||||
bool inBlock = false;
|
bool inBlock = false;
|
||||||
QString marker("$$");
|
QString marker("$$");
|
||||||
|
QString rawMarkerStart("\\begin{");
|
||||||
for (auto it = formulaRegs.begin(); it != formulaRegs.end(); ++it) {
|
for (auto it = formulaRegs.begin(); it != formulaRegs.end(); ++it) {
|
||||||
const VElementRegion &r = *it;
|
const VElementRegion &r = *it;
|
||||||
QTextBlock block = doc->findBlock(r.m_startPos);
|
QTextBlock block = doc->findBlock(r.m_startPos);
|
||||||
@ -321,7 +332,8 @@ void PegHighlighterResult::parseMathjaxBlocks(const PegMarkdownHighlighter *p_pe
|
|||||||
QString text = block.text().mid(pib, length);
|
QString text = block.text().mid(pib, length);
|
||||||
if (inBlock) {
|
if (inBlock) {
|
||||||
item.m_text = item.m_text + "\n" + text;
|
item.m_text = item.m_text + "\n" + text;
|
||||||
if (text.endsWith(marker)) {
|
if (text.endsWith(marker)
|
||||||
|
|| (blockNum == lastBlock && isDisplayFormulaRawEnd(text))) {
|
||||||
// End of block.
|
// End of block.
|
||||||
inBlock = false;
|
inBlock = false;
|
||||||
item.m_blockNumber = blockNum;
|
item.m_blockNumber = blockNum;
|
||||||
@ -330,11 +342,13 @@ void PegHighlighterResult::parseMathjaxBlocks(const PegMarkdownHighlighter *p_pe
|
|||||||
m_mathjaxBlocks.append(item);
|
m_mathjaxBlocks.append(item);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (!text.startsWith(marker)) {
|
if (!text.startsWith(marker)
|
||||||
|
&& !text.startsWith(rawMarkerStart)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (text.size() > 2 && text.endsWith(marker)) {
|
if ((text.size() > 2 && text.endsWith(marker))
|
||||||
|
|| (blockNum == lastBlock && isDisplayFormulaRawEnd(text))) {
|
||||||
// Within one block.
|
// Within one block.
|
||||||
item.m_blockNumber = blockNum;
|
item.m_blockNumber = blockNum;
|
||||||
item.m_previewedAsBlock = true;
|
item.m_previewedAsBlock = true;
|
||||||
|
@ -37,7 +37,7 @@ void PegMarkdownHighlighter::init(const QVector<HighlightingStyle> &p_styles,
|
|||||||
m_codeBlockStyles = p_codeBlockStyles;
|
m_codeBlockStyles = p_codeBlockStyles;
|
||||||
|
|
||||||
if (p_mathjaxEnabled) {
|
if (p_mathjaxEnabled) {
|
||||||
m_parserExts |= pmh_EXT_MATH;
|
m_parserExts |= (pmh_EXT_MATH | pmh_EXT_MATH_RAW);
|
||||||
}
|
}
|
||||||
|
|
||||||
m_parseInterval = p_timerInterval;
|
m_parseInterval = p_timerInterval;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user