mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 13:59:52 +08:00
$$: no need to add blank line before $$ start
This commit is contained in:
parent
8be34ade30
commit
2908148c00
@ -1 +1 @@
|
|||||||
Subproject commit b3af91647d64f91eb031b4c0a3ce5d1e656fae08
|
Subproject commit a6a9a31cf64b4a159d360f0ef6ed6cf296f8eca8
|
@ -33,7 +33,7 @@ v1.4.0
|
|||||||
Vitaly Puzrin
|
Vitaly Puzrin
|
||||||
|
|
||||||
# [markdown-it-texmath](https://github.com/vnotex/markdown-it-texmath)
|
# [markdown-it-texmath](https://github.com/vnotex/markdown-it-texmath)
|
||||||
83e3fd3cd0799593d825298466053793a5461e42
|
b08ddd342f26ddcdc3afa2218f1b55410f44029b
|
||||||
Stefan Goessner
|
Stefan Goessner
|
||||||
Le Tan
|
Le Tan
|
||||||
|
|
||||||
|
@ -1,17 +1,26 @@
|
|||||||
/*---------------------------------------------------------------------------------------------
|
/*---------------------------------------------------------------------------------------------
|
||||||
* Copyright (c) Stefan Goessner - 2017-20. All rights reserved.
|
* Copyright (c) Stefan Goessner - 2017-21. All rights reserved.
|
||||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||||
*--------------------------------------------------------------------------------------------*/
|
*--------------------------------------------------------------------------------------------*/
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
function texmath(md, options) {
|
function escapeHTML(text) {
|
||||||
texmath.escapeHtml = md.utils.escapeHtml;
|
return text
|
||||||
const delimitersList = options && options.delimitersList || ['dollars'];
|
.replace(/&/g, "&")
|
||||||
const katexOptions = options && options.katexOptions || { throwOnError: false };
|
.replace(/</g, "<")
|
||||||
katexOptions.macros = options && options.macros || katexOptions.macros; // ensure backwards compatibility
|
.replace(/>/g, ">")
|
||||||
|
.replace(/"/g, """)
|
||||||
|
.replace(/'/g, "'");
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
function texmath(md, options) {
|
||||||
if (!texmath.katex) { // else ... depricated `use` method was used ...
|
const delimitersList = options && options.delimitersList || ['dollars'];
|
||||||
|
const outerSpace = options && options.outerSpace || false; // inline rules, effectively `dollars` require surrounding spaces, i.e ` $\psi$ `, to be accepted as inline formulas. This is primarily a guard against misinterpreting single `$`'s in normal markdown text (relevant for inline math only. Default: `false`, for backwards compatibility).
|
||||||
|
const katexOptions = options && options.katexOptions || {};
|
||||||
|
katexOptions.throwOnError = katexOptions.throwOnError || false;
|
||||||
|
katexOptions.macros = katexOptions.macros || options && options.macros; // ensure backwards compatibility
|
||||||
|
|
||||||
|
if (!texmath.katex) { // else ... deprecated `use` method was used ...
|
||||||
if (options && typeof options.engine === 'object') {
|
if (options && typeof options.engine === 'object') {
|
||||||
texmath.katex = options.engine;
|
texmath.katex = options.engine;
|
||||||
}
|
}
|
||||||
@ -20,53 +29,56 @@ function texmath(md, options) {
|
|||||||
else // artifical error object.
|
else // artifical error object.
|
||||||
texmath.katex = { renderToString() { return 'No math renderer found.' } };
|
texmath.katex = { renderToString() { return 'No math renderer found.' } };
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
delimitersList.forEach(function(delimiters) {
|
delimitersList.forEach(function(delimiters) {
|
||||||
if (delimiters in texmath.rules) {
|
if (delimiters in texmath.rules) {
|
||||||
for (const rule of texmath.rules[delimiters].inline) {
|
for (const rule of texmath.rules[delimiters].inline) {
|
||||||
|
if (!!outerSpace && 'outerSpace' in rule) rule.outerSpace = true;
|
||||||
md.inline.ruler.before('escape', rule.name, texmath.inline(rule)); // ! important
|
md.inline.ruler.before('escape', rule.name, texmath.inline(rule)); // ! important
|
||||||
md.renderer.rules[rule.name] = (tokens, idx) => rule.tmpl.replace(/\$1/,texmath.render(tokens[idx].content,!!rule.displayMode,katexOptions));
|
md.renderer.rules[rule.name] = (tokens, idx) => rule.tmpl.replace(/\$1/,texmath.render(tokens[idx].content,!!rule.displayMode,katexOptions));
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const rule of texmath.rules[delimiters].block) {
|
for (const rule of texmath.rules[delimiters].block) {
|
||||||
md.block.ruler.before('fence', rule.name, texmath.block(rule)); // ! important for ```math delimiters
|
md.block.ruler.before('fence', rule.name, texmath.block(rule)); // ! important for ```math delimiters
|
||||||
md.renderer.rules[rule.name] = (tokens, idx) => rule.tmpl.replace(/\$2/,tokens[idx].info) // equation number .. ?
|
md.renderer.rules[rule.name] = (tokens, idx) => rule.tmpl.replace(/\$2/,escapeHTML(tokens[idx].info)) // equation number .. ?
|
||||||
.replace(/\$1/,texmath.render(tokens[idx].content,true,katexOptions));
|
.replace(/\$1/,texmath.render(tokens[idx].content,true,katexOptions));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
texmath.applyRule = function(rule, pos, str) {
|
|
||||||
const pre = str.startsWith(rule.tag, rule.rex.lastIndex = pos) && (!rule.pre || rule.pre(str, pos)); // valid pre-condition ...
|
|
||||||
const match = pre && rule.rex.exec(str);
|
|
||||||
const res = !!match && pos < rule.rex.lastIndex && (!rule.post || rule.post(str, rule.rex.lastIndex - 1));
|
|
||||||
return { ret: res, match: match };
|
|
||||||
}
|
|
||||||
|
|
||||||
// texmath.inline = (rule) => dollar; // just for debugging/testing ..
|
// texmath.inline = (rule) => dollar; // just for debugging/testing ..
|
||||||
|
|
||||||
texmath.inline = (rule) =>
|
texmath.inline = (rule) =>
|
||||||
function(state, silent) {
|
function(state, silent) {
|
||||||
const res = texmath.applyRule(rule, state.pos, state.src);
|
const pos = state.pos;
|
||||||
if (res.ret) {
|
const str = state.src;
|
||||||
|
const pre = str.startsWith(rule.tag, rule.rex.lastIndex = pos) && (!rule.pre || rule.pre(str, rule.outerSpace, pos)); // valid pre-condition ...
|
||||||
|
const match = pre && rule.rex.exec(str);
|
||||||
|
const res = !!match && pos < rule.rex.lastIndex && (!rule.post || rule.post(str, rule.outerSpace, rule.rex.lastIndex - 1));
|
||||||
|
|
||||||
|
if (res) {
|
||||||
if (!silent) {
|
if (!silent) {
|
||||||
const token = state.push(rule.name, 'math', 0);
|
const token = state.push(rule.name, 'math', 0);
|
||||||
token.content = res.match[1];
|
token.content = match[1];
|
||||||
token.markup = rule.tag;
|
token.markup = rule.tag;
|
||||||
}
|
}
|
||||||
state.pos = rule.rex.lastIndex;
|
state.pos = rule.rex.lastIndex;
|
||||||
}
|
}
|
||||||
return res.ret;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
texmath.block = (rule) =>
|
texmath.block = (rule) =>
|
||||||
function block(state, begLine, endLine, silent) {
|
function block(state, begLine, endLine, silent) {
|
||||||
const pos = state.bMarks[begLine] + state.tShift[begLine];
|
const pos = state.bMarks[begLine] + state.tShift[begLine];
|
||||||
const str = state.src;
|
const str = state.src;
|
||||||
const res = texmath.applyRule(rule, pos, str);
|
const pre = str.startsWith(rule.tag, rule.rex.lastIndex = pos) && (!rule.pre || rule.pre(str, false, pos)); // valid pre-condition ....
|
||||||
if (res.ret && !silent) { // match and valid post-condition ...
|
const match = pre && rule.rex.exec(str);
|
||||||
|
const res = !!match
|
||||||
|
&& pos < rule.rex.lastIndex
|
||||||
|
&& (!rule.post || rule.post(str, false, rule.rex.lastIndex - 1));
|
||||||
|
|
||||||
|
if (res && !silent) { // match and valid post-condition ...
|
||||||
const endpos = rule.rex.lastIndex - 1;
|
const endpos = rule.rex.lastIndex - 1;
|
||||||
let curline;
|
let curline;
|
||||||
|
|
||||||
@ -82,24 +94,23 @@ texmath.block = (rule) =>
|
|||||||
state.parentType = 'math';
|
state.parentType = 'math';
|
||||||
|
|
||||||
if (parentType === 'blockquote') // remove all leading '>' inside multiline formula
|
if (parentType === 'blockquote') // remove all leading '>' inside multiline formula
|
||||||
res.match[1] = res.match[1].replace(/(\n*?^(?:\s*>)+)/gm,'');
|
match[1] = match[1].replace(/(\n*?^(?:\s*>)+)/gm,'');
|
||||||
// begin token
|
// begin token
|
||||||
let token = state.push(rule.name, 'math', 1); // 'math_block'
|
let token = state.push(rule.name, 'math', 1); // 'math_block'
|
||||||
token.block = true;
|
token.block = true;
|
||||||
token.markup = rule.tag;
|
token.tag = rule.tag;
|
||||||
token.content = res.match[1];
|
token.markup = '';
|
||||||
token.info = res.match[res.match.length-1]; // eq.no
|
token.content = match[1];
|
||||||
|
token.info = match[match.length-1]; // eq.no
|
||||||
token.map = [ begLine, curline ];
|
token.map = [ begLine, curline ];
|
||||||
// end token
|
// token.hidden = true;
|
||||||
token = state.push(rule.name+'_end', 'math', -1);
|
// end token ... superfluous ...
|
||||||
token.block = true;
|
|
||||||
token.markup = rule.tag;
|
|
||||||
|
|
||||||
state.parentType = parentType;
|
state.parentType = parentType;
|
||||||
state.lineMax = lineMax;
|
state.lineMax = lineMax;
|
||||||
state.line = curline+1;
|
state.line = curline+1;
|
||||||
}
|
}
|
||||||
return res.ret;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
texmath.render = function(tex,displayMode,options) {
|
texmath.render = function(tex,displayMode,options) {
|
||||||
@ -114,9 +125,9 @@ texmath.render = function(tex,displayMode,options) {
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
if (displayMode) {
|
if (displayMode) {
|
||||||
res = '$$$$' + texmath.escapeHtml(tex) + '$$$$';
|
res = '$$$$' + escapeHTML(tex) + '$$$$';
|
||||||
} else {
|
} else {
|
||||||
res = '$$' + texmath.escapeHtml(tex) + '$$';
|
res = '$$' + escapeHTML(tex) + '$$';
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@ -173,14 +184,19 @@ function dollar(state, silent) {
|
|||||||
texmath.inlineRuleNames = ['math_inline','math_inline_double'];
|
texmath.inlineRuleNames = ['math_inline','math_inline_double'];
|
||||||
texmath.blockRuleNames = ['math_block','math_block_eqno'];
|
texmath.blockRuleNames = ['math_block','math_block_eqno'];
|
||||||
|
|
||||||
texmath.$_pre = (str,beg) => {
|
texmath.$_pre = (str,outerSpace,beg) => {
|
||||||
const prv = beg > 0 ? str[beg-1].charCodeAt(0) : false;
|
const prv = beg > 0 ? str[beg-1].charCodeAt(0) : false;
|
||||||
return !prv || prv !== 0x5c // no backslash,
|
return outerSpace ? !prv || prv === 0x20 // space (avoiding regex's for performance reasons)
|
||||||
|
: !prv || prv !== 0x5c // no backslash,
|
||||||
&& (prv < 0x30 || prv > 0x39); // no decimal digit .. before opening '$'
|
&& (prv < 0x30 || prv > 0x39); // no decimal digit .. before opening '$'
|
||||||
}
|
}
|
||||||
texmath.$_post = (str,end) => {
|
texmath.$_post = (str,outerSpace,end) => {
|
||||||
const nxt = str[end+1] && str[end+1].charCodeAt(0);
|
const nxt = str[end+1] && str[end+1].charCodeAt(0);
|
||||||
return !nxt || nxt < 0x30 || nxt > 0x39; // no decimal digit .. after closing '$'
|
return outerSpace ? !nxt || nxt === 0x20 // space (avoiding regex's for performance reasons)
|
||||||
|
|| nxt === 0x2e // '.'
|
||||||
|
|| nxt === 0x2c // ','
|
||||||
|
|| nxt === 0x3b // ';'
|
||||||
|
: !nxt || nxt < 0x30 || nxt > 0x39; // no decimal digit .. after closing '$'
|
||||||
}
|
}
|
||||||
|
|
||||||
texmath.rules = {
|
texmath.rules = {
|
||||||
@ -194,17 +210,38 @@ texmath.rules = {
|
|||||||
],
|
],
|
||||||
block: [
|
block: [
|
||||||
{ name: 'math_brackets_block_eqno',
|
{ name: 'math_brackets_block_eqno',
|
||||||
rex: /\\\[(((?!\\\]|\\\[)[\s\S])+?)\\\]\s*?\(([^)$\r\n]+?)\)\s*$/gmy,
|
rex: /\\\[(((?!\\\]|\\\[)[\s\S])+?)\\\]\s*?\(([^)$\r\n]+?)\)/gmy,
|
||||||
tmpl: '<section class="eqno"><eqn class="tex-to-render">$1</eqn><span>($2)</span></section>',
|
tmpl: '<section class="eqno"><eqn class="tex-to-render">$1</eqn><span>($2)</span></section>',
|
||||||
tag: '\\['
|
tag: '\\['
|
||||||
},
|
},
|
||||||
{ name: 'math_brackets_block',
|
{ name: 'math_brackets_block',
|
||||||
rex: /\\\[([\s\S]+?)\\\]\s*$/gmy,
|
rex: /\\\[([\s\S]+?)\\\]/gmy,
|
||||||
tmpl: '<section><eqn class="tex-to-render">$1</eqn></section>',
|
tmpl: '<section><eqn class="tex-to-render">$1</eqn></section>',
|
||||||
tag: '\\['
|
tag: '\\['
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
doxygen: {
|
||||||
|
inline: [
|
||||||
|
{ name: 'math_doxygen_inline',
|
||||||
|
rex: /\\f\$(.+?)\\f\$/gy,
|
||||||
|
tmpl: '<eq>$1</eq>',
|
||||||
|
tag: '\\f$'
|
||||||
|
}
|
||||||
|
],
|
||||||
|
block: [
|
||||||
|
{ name: 'math_doxygen_block_eqno',
|
||||||
|
rex: /\\f\[([^]+?)\\f\]\s*?\(([^)\s]+?)\)/gmy,
|
||||||
|
tmpl: '<section class="eqno"><eqn>$1</eqn><span>($2)</span></section>',
|
||||||
|
tag: '\\f['
|
||||||
|
},
|
||||||
|
{ name: 'math_doxygen_block',
|
||||||
|
rex: /\\f\[([^]+?)\\f\]/gmy,
|
||||||
|
tmpl: '<section><eqn>$1</eqn></section>',
|
||||||
|
tag: '\\f['
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
gitlab: {
|
gitlab: {
|
||||||
inline: [
|
inline: [
|
||||||
{ name: 'math_gitlab_inline',
|
{ name: 'math_gitlab_inline',
|
||||||
@ -237,18 +274,19 @@ texmath.rules = {
|
|||||||
rex: /\$((?:\S?)|(?:\S.*?\S))\$/gy,
|
rex: /\$((?:\S?)|(?:\S.*?\S))\$/gy,
|
||||||
tmpl: '<eq class="tex-to-render">$1</eq>',
|
tmpl: '<eq class="tex-to-render">$1</eq>',
|
||||||
tag: '$',
|
tag: '$',
|
||||||
|
spaceEnclosed: false,
|
||||||
pre: texmath.$_pre,
|
pre: texmath.$_pre,
|
||||||
post: texmath.$_post
|
post: texmath.$_post
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
block: [
|
block: [
|
||||||
{ name: 'math_julia_block_eqno',
|
{ name: 'math_julia_block_eqno',
|
||||||
rex: /`{3}math\s+?([^`]+?)\s+?`{3}\s*?\(([^)$\r\n]+?)\)\s*$/gmy,
|
rex: /`{3}math\s+?([^`]+?)\s+?`{3}\s*?\(([^)$\r\n]+?)\)/gmy,
|
||||||
tmpl: '<section class="eqno"><eqn class="tex-to-render">$1</eqn><span>($2)</span></section>',
|
tmpl: '<section class="eqno"><eqn class="tex-to-render">$1</eqn><span>($2)</span></section>',
|
||||||
tag: '```math'
|
tag: '```math'
|
||||||
},
|
},
|
||||||
{ name: 'math_julia_block',
|
{ name: 'math_julia_block',
|
||||||
rex: /`{3}math\s+?([^`]+?)\s+?`{3}\s*$/gmy,
|
rex: /`{3}math\s+?([^`]+?)\s+?`{3}/gmy,
|
||||||
tmpl: '<section><eqn class="tex-to-render">$1</eqn></section>',
|
tmpl: '<section><eqn class="tex-to-render">$1</eqn></section>',
|
||||||
tag: '```math'
|
tag: '```math'
|
||||||
}
|
}
|
||||||
@ -264,12 +302,12 @@ texmath.rules = {
|
|||||||
],
|
],
|
||||||
block: [
|
block: [
|
||||||
{ name: 'math_kramdown_block_eqno',
|
{ name: 'math_kramdown_block_eqno',
|
||||||
rex: /\${2}([^$]+?)\${2}\s*?\(([^)\s]+?)\)\s*$/gmy,
|
rex: /\${2}([^$]+?)\${2}\s*?\(([^)\s]+?)\)/gmy,
|
||||||
tmpl: '<section class="eqno"><eqn class="tex-to-render">$1</eqn><span>($2)</span></section>',
|
tmpl: '<section class="eqno"><eqn class="tex-to-render">$1</eqn><span>($2)</span></section>',
|
||||||
tag: '$$'
|
tag: '$$'
|
||||||
},
|
},
|
||||||
{ name: 'math_kramdown_block',
|
{ name: 'math_kramdown_block',
|
||||||
rex: /\${2}([^$]+?)\${2}\s*$/gmy,
|
rex: /\${2}([^$]+?)\${2}/gmy,
|
||||||
tmpl: '<section><eqn class="tex-to-render">$1</eqn></section>',
|
tmpl: '<section><eqn class="tex-to-render">$1</eqn></section>',
|
||||||
tag: '$$'
|
tag: '$$'
|
||||||
}
|
}
|
||||||
@ -278,7 +316,7 @@ texmath.rules = {
|
|||||||
dollars: {
|
dollars: {
|
||||||
inline: [
|
inline: [
|
||||||
{ name: 'math_dollars_inline_double',
|
{ name: 'math_dollars_inline_double',
|
||||||
rex: /\${2}((?:\S)|(?:\S.*?\S))\${2}/gy,
|
rex: /\${2}([^$]*?[^\\])\${2}/gy,
|
||||||
tmpl: '<section><eqn class="tex-to-render">$1</eqn></section>',
|
tmpl: '<section><eqn class="tex-to-render">$1</eqn></section>',
|
||||||
tag: '$$',
|
tag: '$$',
|
||||||
displayMode: true,
|
displayMode: true,
|
||||||
@ -286,9 +324,10 @@ texmath.rules = {
|
|||||||
post: texmath.$_post
|
post: texmath.$_post
|
||||||
},
|
},
|
||||||
{ name: 'math_dollars_inline',
|
{ name: 'math_dollars_inline',
|
||||||
rex: /\$((?:\S)|(?:\S.*?\S))\$/gy,
|
rex: /\$((?:[^\s\\])|(?:\S.*?[^\s\\]))\$/gy,
|
||||||
tmpl: '<eq class="tex-to-render">$1</eq>',
|
tmpl: '<eq class="tex-to-render">$1</eq>',
|
||||||
tag: '$',
|
tag: '$',
|
||||||
|
outerSpace: false,
|
||||||
pre: texmath.$_pre,
|
pre: texmath.$_pre,
|
||||||
post: texmath.$_post
|
post: texmath.$_post
|
||||||
}
|
}
|
||||||
@ -311,7 +350,7 @@ texmath.rules = {
|
|||||||
block: [
|
block: [
|
||||||
{
|
{
|
||||||
name: 'math_raw_block',
|
name: 'math_raw_block',
|
||||||
rex: /(\\begin\s*\{([^{}\s\r\n]+)\}(?:[^\\]|\\(?!end\s*\{\2\}))*\\end\s*\{\2\})\s*$/gmy,
|
rex: /(\\begin\s*\{([^{}\s\r\n]+)\}(?:[^\\]|\\(?!end\s*\{\2\}))*\\end\s*\{\2\})/gmy,
|
||||||
tmpl: '<section><eqn class="tex-to-render">$1</eqn></section>',
|
tmpl: '<section><eqn class="tex-to-render">$1</eqn></section>',
|
||||||
tag: '\\begin'
|
tag: '\\begin'
|
||||||
}
|
}
|
||||||
|
@ -1990,6 +1990,8 @@ void NotebookNodeExplorer::setExploreMode(int p_mode)
|
|||||||
case ExploreMode::Combined:
|
case ExploreMode::Combined:
|
||||||
setFocusProxy(m_masterExplorer);
|
setFocusProxy(m_masterExplorer);
|
||||||
|
|
||||||
|
WidgetUtils::distributeWidgetsOfSplitter(m_splitter);
|
||||||
|
|
||||||
Q_ASSERT(m_slaveExplorer);
|
Q_ASSERT(m_slaveExplorer);
|
||||||
m_slaveExplorer->clear();
|
m_slaveExplorer->clear();
|
||||||
m_slaveExplorer->hide();
|
m_slaveExplorer->hide();
|
||||||
@ -2009,6 +2011,7 @@ void NotebookNodeExplorer::setExploreMode(int p_mode)
|
|||||||
|
|
||||||
m_slaveExplorer->show();
|
m_slaveExplorer->show();
|
||||||
m_splitter->setOrientation(m_exploreMode == ExploreMode::SeparateSingle ? Qt::Vertical : Qt::Horizontal);
|
m_splitter->setOrientation(m_exploreMode == ExploreMode::SeparateSingle ? Qt::Vertical : Qt::Horizontal);
|
||||||
|
WidgetUtils::distributeWidgetsOfSplitter(m_splitter);
|
||||||
|
|
||||||
connect(m_masterExplorer, &QTreeWidget::currentItemChanged,
|
connect(m_masterExplorer, &QTreeWidget::currentItemChanged,
|
||||||
this, &NotebookNodeExplorer::updateSlaveExplorer);
|
this, &NotebookNodeExplorer::updateSlaveExplorer);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user