@@ -3,26 +3,31 @@ debug = require("./config").debug;
33function process_katex_formula ( input ) {
44 processed_input = input
55 processed_input = processed_input
6- . replaceAll ( "\\{" , "\\lbrace " )
7- . replaceAll ( "\\}" , "\\rbrace " ) //没这个会报错
8- . replaceAll ( "\\|" , "\\Vert " ) // 防止斜杠被干掉
9- . replaceAll ( "<" , "< " ) // 防止转义成 html 标签
6+ . replaceAll ( / < / g, '<' )
7+ . replaceAll ( / > / g, '>' ) ;
108 return processed_input
119}
1210module . exports = function ( data ) {
1311 console . log ( "Katex patch loaded. Made by BlitherBoom812." ) ;
1412 console . log ( data . content )
15- data . content = data . content . replace ( / \$ ( [ ^ \n $ ] + ) \$ / g, ( match , p1 ) => {
16- console . log ( "matched: " , match )
17- return_value = process_katex_formula ( p1 )
18- . replaceAll ( / _ / g, "\\_" ) // 防止被解析成 markdown 的下划线
19- . replaceAll ( / \* / g, "\\\*" ) // 防止被解析成 markdown 的下划线
20- . replaceAll ( "\\\\" , "\\\\\\\\" ) // 防止被解析成 markdown 的转义符
21- console . log ( "revised: " , return_value )
22- return `$${ return_value } $`
13+ data . content = data . content . replace ( / (?< ! \$ ) \$ ( [ ^ \n $ ] + ) \$ (? ! \$ ) / g, ( match , p1 ) => {
14+ console . log ( "matched single line formula: " , match )
15+ return_value = `{% raw %} $${ process_katex_formula ( p1 ) } $ {% endraw %}`
16+ console . log ( "revised single line formula: " , return_value )
17+ return return_value
2318 } ) ;
24- data . content = data . content . replace ( / \$ \$ ( [ \s \S ] * ?) \$ \$ / g, ( match , p1 ) => {
25- return `<div>${ process_katex_formula ( match ) } </div>`
19+ data . content = data . content . replace ( / \$ \$ ( [ ^ $ ] + ?) \$ \$ / g, ( match , p1 ) => {
20+ console . log ( "matched multiline formula: " , match )
21+ return_value = `{% raw %}\n$$${ process_katex_formula ( p1 ) } $$\n{% endraw %}`
22+ max_str_length = 800
23+ if ( return_value . length > max_str_length ) {
24+ console . log ( "revised (preview): " ,
25+ `${ return_value . slice ( 0 , max_str_length / 2 ) } ... ${ return_value . slice ( - max_str_length / 2 ) } `
26+ ) ;
27+ } else {
28+ console . log ( "revised multiline formula: " , return_value ) ;
29+ }
30+ return return_value
2631 } ) ;
2732 return data ;
2833} ;
0 commit comments