forked from typecho-fans/plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plugin.php
executable file
·148 lines (141 loc) · 4.91 KB
/
Plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
/**
* 自动渲染 LaTeX 公式
*
* @package AutoLaTeX
* @author bLue
* @version 0.1.0
* @link https://dreamer.blue
*/
class AutoLaTeX_Plugin implements Typecho_Plugin_Interface {
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate() {
Typecho_Plugin::factory('Widget_Archive')->header = array(__CLASS__, 'header');
Typecho_Plugin::factory('Widget_Archive')->footer = array(__CLASS__, 'footer');
Typecho_Plugin::factory('admin/write-post.php')->content = array(__CLASS__, 'header');
Typecho_Plugin::factory('admin/write-post.php')->bottom = array(__CLASS__, 'footer');
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate() {}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form) {
$renderingList = array(
'KaTeX' => 'KaTeX',
'MathJax' => 'MathJax',
);
$name = new Typecho_Widget_Helper_Form_Element_Select('rendering', $renderingList, 'KaTeX', _t('选择 LaTeX 渲染方式'));
$form->addInput($name->addRule('enum', _t('请选择 LaTeX 渲染方式'), $renderingList));
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form) {}
/**
* 插件实现方法
*
* @access public
* @return void
*/
public static function render() {}
/**
* 添加额外输出到 Header
*
* @access public
* @return void
*/
public static function header() {
$pluginDir = Helper::options()->pluginUrl . '/AutoLaTeX';
$rendering = Helper::options()->plugin('AutoLaTeX')->rendering;
switch($rendering) {
case 'MathJax':
break;
case 'KaTeX':
echo <<<HTML
<link href="{$pluginDir}/KaTeX/katex.min.css" rel="stylesheet">
HTML;
break;
}
}
/**
* 添加额外输出到 Footer
*
* @access public
* @return void
*/
public static function footer() {
$pluginDir = Helper::options()->pluginUrl . '/AutoLaTeX';
$rendering = Helper::options()->plugin('AutoLaTeX')->rendering;
switch($rendering) {
case 'MathJax':
echo <<<HTML
<script src="{$pluginDir}/MathJax/MathJax.js?config=TeX-AMS-MML_HTMLorMML,local/local"></script>
<script>
function triggerRenderingLaTeX(element) {
MathJax.Hub.Queue(["Typeset", MathJax.Hub, element]);
}
</script>
HTML;
break;
case 'KaTeX':
echo <<<HTML
<script src="{$pluginDir}/KaTeX/katex.min.js"></script>
<script src="{$pluginDir}/KaTeX/auto-render.min.js"></script>
<script>
function triggerRenderingLaTeX(element) {
renderMathInElement(
element,
{
delimiters: [
{left: "$$", right: "$$", display: true},
{left: "\\\\[", right: "\\\\]", display: true},
{left: "$", right: "$", display: false},
{left: "\\\\(", right: "\\\\)", display: false}
]
}
);
}
document.addEventListener("DOMContentLoaded", function() {
triggerRenderingLaTeX(document.body);
});
</script>
HTML;
break;
}
echo <<<HTML
<script>
document.addEventListener("DOMContentLoaded", function() {
var wmdPreviewLink = document.querySelector("a[href='#wmd-preview']");
var wmdPreviewContainer = document.querySelector("#wmd-preview");
if(wmdPreviewLink && wmdPreviewContainer) {
wmdPreviewLink.onclick = function() {
triggerRenderingLaTeX(wmdPreviewContainer);
};
}
});
</script>
HTML;
}
}