-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathfunctions.php
1555 lines (1425 loc) · 45.3 KB
/
functions.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
}
// 文章主题样式map
global $markdownThemeMap;
$markdownThemeMap = array(
'juejin' => _t('掘金'),
'github' => _t('github'),
'smartblue' => _t('smartblue'),
'cyanosis' => _t('cyanosis'),
'channing-cyan' => _t('channing-cyan'),
'fancy' => _t('fancy'),
'hydrogen' => _t('hydrogen'),
'v-green' => _t('v-green'),
'vue-pro' => _t('vue-pro'),
'healer-readable' => _t('healer-readable'),
'mk-cute' => _t('mk-cute'),
'geek-black' => _t('geek-black'),
'qklhk-chocolate' => _t('qklhk-chocolate'),
'orange' => _t('orange'),
'scrolls-light' => _t('scrolls-light'),
'simplicity-green' => _t('simplicity-green'),
'arknights' => _t('arknights'),
'vuepress' => _t('vuepress'),
'nico' => _t('nico'),
'devui-blue' => _t('devui-blue'),
'serene-rose' => _t('serene-rose'),
'z-blue' => _t('z-blue'),
'minimalism' => _t('minimalism'),
'yu' => _t('yu'),
'keepnice' => _t('keepnice'),
);
// 默认文章主题对应的代码高亮map
global $defaultMarkdownThemeHighlightMap;
$defaultMarkdownThemeHighlightMap = array(
'juejin' => 'juejin',
'github' => 'github',
'smartblue' => 'juejin',
'cyanosis' => 'atom-one-dark',
'channing-cyan' => 'juejin',
'fancy' => 'juejin',
'hydrogen' => 'juejin',
'v-green' => 'juejin',
'vue-pro' => 'monokai',
'healer-readable' => 'srcery',
'mk-cute' => 'juejin',
'geek-black' => 'monokai',
'qklhk-chocolate' => 'juejin',
'orange' => 'atom-one-light',
'scrolls-light' => 'juejin',
'simplicity-green' => 'juejin',
'arknights' => 'atom-one-light',
'vuepress' => 'base16/tomorrow-night',
'nico' => 'atelier-sulphurpool-light',
'devui-blue' => 'juejin',
'serene-rose' => 'atom-one-dark',
'z-blue' => 'androidstudio',
'minimalism' => 'atom-one-dark',
'yu' => 'atom-one-dark',
'keepnice' => 'github',
);
// 代码高亮主题map
global $markdownHighlightMap;
$markdownHighlightMap = array(
'' => _t('无'),
'juejin' => _t('掘金'),
'github' => _t('github'),
'github-gist' => _t('github-gist'),
'atom-one-dark' => _t('atom-one-dark'),
'atom-one-light' => _t('atom-one-light'),
'monokai' => _t('monokai'),
'monokai-sublime' => _t('monokai-sublime'),
'srcery' => _t('srcery'),
'tomorrow-night-blue' => _t('tomorrow-night-blue'),
'tomorrow-night-eighties' => _t('tomorrow-night-eighties'),
'tomorrow-night' => _t('tomorrow-night'),
'tomorrow' => _t('tomorrow'),
'atelier-sulphurpool-light' => _t('atelier-sulphurpool-light'),
'androidstudio' => _t('androidstudio'),
'a11y-dark' => _t('a11y-dark'),
'a11y-light' => _t('a11y-light'),
'zenburn' => _t('zenburn'),
);
/**
* @description: 主题可视化配置
* @param {*} $form
* @Date: 2023-03-21 23:48:01
* @Author: mulingyuer
*/
function themeConfig($form)
{
global $markdownThemeMap;
// head标签底部插入代码
$headInsertCode = new \Typecho\Widget\Helper\Form\Element\Textarea(
'headInsertCode',
null,
null,
_t('head标签底部插入代码'),
_t('放入自定义样式link或者脚本script')
);
$form->addInput($headInsertCode);
// body标签底部插入代码
$bodyInsertCode = new \Typecho\Widget\Helper\Form\Element\Textarea(
'bodyInsertCode',
null,
null,
_t('body标签底部插入代码'),
_t('放入站点统计代码或者自定义脚本')
);
$form->addInput($bodyInsertCode);
// 备案信息
$filing = new \Typecho\Widget\Helper\Form\Element\Textarea(
'filing',
null,
null,
_t('备案信息'),
_t('例子:<div class="footer-item"><a href="备案跳转的链接" target="_blank" rel="noopener nofollow">备案号</a></div>')
);
$form->addInput($filing);
// 文章置顶
$stickyCidList = new \Typecho\Widget\Helper\Form\Element\Text(
'stickyCidList',
null,
'',
_t('置顶文章cid列表'),
_t('请用英文逗号 , 分隔文章cid')
);
$form->addInput($stickyCidList);
// 文章置顶标题高亮tag
$stickyCidTag = new \Typecho\Widget\Helper\Form\Element\Text(
'stickyCidTag',
null,
'',
_t('置顶文章标题前面加的tag'),
_t('请使用html标签,默认:<span class="article-card-sticky-tag">置顶</span>')
);
$form->addInput($stickyCidTag);
// 首页右侧推荐文章cid列表
$homeRecommendedArticleCidList = new \Typecho\Widget\Helper\Form\Element\Text(
'homeRecommendedArticleCidList',
null,
'',
_t('首页右侧推荐文章cid列表'),
_t('请用英文逗号 , 分隔文章cid,最大3篇,务必配置好文章自定义缩略图字段!!!')
);
$form->addInput($homeRecommendedArticleCidList);
// 首页右侧推荐文章tag
$homeRecommendedArticleTag = new \Typecho\Widget\Helper\Form\Element\Text(
'homeRecommendedArticleTag',
null,
'推荐',
_t('首页右侧推荐文章tag文字'),
_t('推荐2个文字')
);
$form->addInput($homeRecommendedArticleTag);
// 文章详情页右侧推荐文章cid
$articleRecommendedArticleCid = new \Typecho\Widget\Helper\Form\Element\Text(
'articleRecommendedArticleCid',
null,
'',
_t('文章详情页右侧推荐文章cid'),
_t('只能填写一个文章cid,务必配置好文章自定义缩略图字段!!!')
);
$form->addInput($articleRecommendedArticleCid);
// 文章详情页右侧推荐文章tag
$articleRecommendedArticleTag = new \Typecho\Widget\Helper\Form\Element\Text(
'articleRecommendedArticleTag',
null,
'推荐',
_t('文章详情页右侧推荐文章tag文字'),
_t('推荐2个文字')
);
$form->addInput($articleRecommendedArticleTag);
$defaultMarkdownTheme = new \Typecho\Widget\Helper\Form\Element\Select(
'defaultMarkdownTheme',
$markdownThemeMap,
'juejin', _t('默认文章和独立页主题'), _t('默认使用掘金主题,非默认选项时优先级大于文章和独立页的默认值')
);
$form->addInput($defaultMarkdownTheme);
// 文章翻页类型
$paginationType = new \Typecho\Widget\Helper\Form\Element\Select(
'paginationType',
array(
'infinite' => _t('无限滚动'),
'button' => _t('按钮翻页'),
),
'infinite', _t('文章翻页类型'), _t('默认使用无限滚动')
);
$form->addInput($paginationType);
// 404页面类型
$errorType = new \Typecho\Widget\Helper\Form\Element\Select(
'errorType',
array(
'chrome' => _t('谷歌浏览器小恐龙'),
'juejin' => _t('掘金404'),
),
'juejin', _t('404页面类型'), _t('默认使用掘金404')
);
$form->addInput($errorType);
// 底部联系地址
$address = new \Typecho\Widget\Helper\Form\Element\Text(
'address',
null,
'东之国中远离人里的边境之地',
_t('底部联系地址'),
_t('默认幻想乡')
);
$form->addInput($address);
// 自定义文章版权声明
$customCopyright = new \Typecho\Widget\Helper\Form\Element\Textarea(
'customCopyright',
null,
'',
_t('自定义文章版权声明'),
_t('默认空')
);
$form->addInput($customCopyright);
// DocSearch
$isOpenDocSearch = new \Typecho\Widget\Helper\Form\Element\Radio(
'isOpenDocSearch',
array(
'off' => _t('关闭'),
'on' => _t('开启'),
),
'off', _t('是否开启DocSearch'), _t('默认关闭')
);
$form->addInput($isOpenDocSearch);
$docSearchAppId = new \Typecho\Widget\Helper\Form\Element\Text(
'docSearchAppId',
null,
'',
_t('DocSearch AppId'),
_t('默认为空')
);
$form->addInput($docSearchAppId);
$docSearchApiKey = new \Typecho\Widget\Helper\Form\Element\Text(
'docSearchApiKey',
null,
'',
_t('DocSearch ApiKey'),
_t('默认为空')
);
$form->addInput($docSearchApiKey);
$docSearchIndexName = new \Typecho\Widget\Helper\Form\Element\Text(
'docSearchIndexName',
null,
'',
_t('DocSearch IndexName'),
_t('默认为空')
);
$form->addInput($docSearchIndexName);
}
/**
* @description: 获取当前页面标题
* @param {*} $that 当前页面对象
* @Date: 2023-03-14 20:50:07
* @Author: mulingyuer
*/
function blogTitle($that)
{
$before = $that->archiveTitle(array(
'category' => _t('分类 %s 下的文章'),
'search' => _t('包含关键字 %s 的文章'),
'tag' => _t('标签 %s 下的文章'),
'author' => _t('%s 发布的文章'),
), '', ' - ');
$title = Helper::options()->title();
return $before . $title;
}
/**
* @description: 固定的一些其他页面
* @param {*} $that 当前页面对象
* @Date: 2023-03-22 00:40:07
* @Author: mulingyuer
*/
function isOtherPage($that)
{
$isIndex = $that->is('index');
$isCategory = $that->is('category');
$isArchive = $that->is('archive');
return $isIndex || $isCategory || $isArchive;
}
/**
* @description: 获取当前页面描述
* @param {*} $that 当前页面对象
* @param {*} $max 最大字符数,推荐在25-160之间
* @Date: 2023-05-14 23:22:44
* @Author: mulingyuer
*/
function blogDescription($that, $max = 160)
{
$desc = '';
if (isOtherPage($that)) {
$desc = Helper::options()->description();
} else {
$desc = $that->excerpt($max);
}
return $desc;
}
/**
* @description: 获取当前页面关键词
* @param {*} $that 当前页面对象
* @Date: 2023-03-14 21:02:23
* @Author: mulingyuer
*/
function blogKeywords($that)
{
$keywords = '';
if (isOtherPage($that)) {
$keywords = Helper::options()->keywords();
} else {
$keywords = $that->category(',', false) + $that->tags(',', false);
}
return $keywords;
}
/**
* @description: seo链接
* @param {*} $that 当前页面对象
* @Date: 2023-03-14 21:20:53
* @Author: mulingyuer
*/
function seoUrl($that)
{
// 旧版
// $url = "";
// if ($that->is('index')) {
// $url = Helper::options()->siteUrl();
// } else {
// $url = $that->permalink();
// }
// return $url;
echo $that->request->getRequestUrl();
}
/**
* @description: seo图片
* @param {*} $that 当前页面对象
* @Date: 2023-03-14 21:30:30
* @Author: mulingyuer
*/
function seoImage($that)
{
$image = '';
if (isOtherPage($that)) {
$image = Helper::options()->themeUrl . '/static/images/favicon/android-chrome-512x512.png';
} else {
$image = articleThumbnail($that);
}
//保底图片
if (!$image) {
$image = Helper::options()->themeUrl . '/static/images/seo_img_null.jpg';
}
return $image;
}
/**
* @description: 父级菜单是否高亮
* @param {*} $activeSlug 选中的菜单slug,也就是名称
* @param {*} $category 父级分类信息
* @param {*} $children 子级分类信息
* @Date: 2023-03-18 22:24:19
* @Author: mulingyuer
*/
function isParentActive($activeSlug, $category, $children)
{
$flag = false;
foreach ($children as $mid) {
$child = $category->getCategory($mid);
if ($child['slug'] === $activeSlug) {
$flag = true;
break;
}
}
return $flag;
}
/** 二级分类:全部是否高亮 */
function secondaryAllActive($that, $category, $children)
{
$flag = true;
foreach ($children as $mid) {
$child = $category->getCategory($mid);
if ($that->is('category', $child['slug'])) {
$flag = false;
break;
}
}
return $flag;
}
/**
* @description: 文章发布时间
* @param {*} $time 原文章发布时间
* @Date: 2023-03-19 16:58:25
* @Author: mulingyuer
*/
function timeFormatting($time)
{
if ($time == 'no') {return;}
$chunks = array(
array(31536000, '年'),
array(2592000, '月'),
array(604800, '周'),
array(86400, '天'),
array(3600, '小时'),
array(60, '分钟'),
array(1, '秒'),
);
$newer_date = time();
$since = abs($newer_date - $time);
for ($i = 0, $j = count($chunks); $i < $j; $i++) {
$seconds = $chunks[$i][0];
$name = $chunks[$i][1];
if (($count = floor($since / $seconds)) != 0) {
break;
}
}
$output = $count . $name . '前';
echo $output;
}
/**
* @description: 获取文章缩略图
* @param {*} $that
* @Date: 2023-03-19 17:03:31
* @Author: mulingyuer
*/
function articleThumbnail($that)
{
$attach = $that->attachments(1)->attachment;
$pattern1 = '/\<img.*?src\=\"(.*?)\"[^>]*>/i';
$pattern2 = '/\!\[.*?\]\((.*?)\)/i';
//如果有自定义缩略图
if ($that->fields->titleImg) {
return $that->fields->titleImg;
} elseif ($that->fields->thumb) {
return $that->fields->thumb;
} elseif (preg_match_all($pattern1, $that->content, $thumbUrl) && strlen($thumbUrl[1][0]) > 7) {
return $thumbUrl[1][0];
} elseif (preg_match_all($pattern2, $that->content, $thumbUrl) && strlen($thumbUrl[1][0]) > 7) {
return $thumbUrl[1][0];
} elseif ($attach && $attach->isImage) {
return $attach->url;
} else {
return '';
}
}
/**
* @description: 文章浏览量
* @param {*} $that 当前页面对象
* @param {*} $format0
* @param {*} $format1
* @param {*} $formats
* @param {*} $return
* @param {*} $field
* @Date: 2023-03-19 17:07:55
* @Author: mulingyuer
*/
function articleViews($that, $format0 = '%d', $format1 = '%d', $formats = '%d', $return = false, $field = 'views')
{
$fields = unserialize($that->fields);
if (array_key_exists($field, $fields)) {
$fieldValue = (!empty($fields[$field])) ? intval($fields[$field]) : 0;
} else {
$fieldValue = 0;
}
if ($fieldValue == 0) {
$fieldValue = sprintf($format0, $fieldValue);
} elseif ($fieldValue == 1) {
$fieldValue = sprintf($format1, $fieldValue);
} else {
$fieldValue = sprintf($formats, $fieldValue);
}
if ($return) {
return $fieldValue;
} else {
return $fieldValue;
}
}
/**
* @description: 文章点赞数
* @param {*} $that 当前页面对象
* @Date: 2023-03-24 23:19:56
* @Author: mulingyuer
*/
function getLikeCount($that)
{
$linkCount = $that->fields->likes;
if (empty($linkCount)) {
return 0;
}
return $linkCount;
}
/**
* Post Action AJAX接口 点赞接口
*
* @param Widget_Archive $widget
* @return void
* @date 2020-05-04
*/
function promo($widget)
{
$user = $widget->widget('Widget_User');
$db = Typecho_Db::get();
$fields = unserialize($widget->fields);
$allowOperates = array('get', 'set', 'inc', 'dec'); // 这里可以扩展操作,建议屏蔽get/set
$allowFields = array('likes'); // 这里可以扩展修改字段
// 获取操作
$operate = $widget->request->get('operate');
$field = $widget->request->get('field');
$value = $widget->request->filter('int')->get('value');
$value = $value === null ? 100 : $value; // 100 起步
$result = array('cid' => $widget->cid);
if ($operate === 'get') {
$result['operate'] = 'get';
if (array_key_exists($field, $fields)) {
$result[$field] = $fields[$field];
} else {
$result[$field] = -1;
}
$widget->response->throwJson(array('status' => 1, 'msg' => _t('已获取参数'), 'result' => json_encode($result)));
} elseif ($operate === 'set') {
$result['operate'] = 'set';
if ($value > 0) {
$widget->setField($field, 'str', $value, $widget->cid);
} else {
$db->query($db->delete('table.fields')
->where('cid = ? AND name = ?', $widget->cid, $field));
}
$widget->response->throwJson(array('status' => 1, 'msg' => _t('已完成操作'), 'result' => json_encode($result)));
} elseif ($operate === 'inc') {
$result['operate'] = 'inc';
$value = intval($fields[$field]) + 1;
$widget->setField($field, 'str', $value, $widget->cid);
$result[$field] = $value;
$widget->response->throwJson(array('status' => 1, 'msg' => _t('已完成操作'), 'result' => json_encode($result)));
} elseif ($operate === 'dec') {
$result['operate'] = 'dec';
$value = intval($fields[$field]) - 1;
$result[$field] = $value;
if ($value > 0) {
$widget->setField($field, 'str', $value, $widget->cid);
} else {
$db->query($db->delete('table.fields')
->where('cid = ? AND name = ?', $widget->cid, $field));
}
$widget->response->throwJson(array('status' => 1, 'msg' => _t('已完成操作'), 'result' => json_encode($result)));
}
}
/**
* @description: 是否是ajax请求
* @Date: 2023-03-21 00:22:09
* @Author: mulingyuer
*/
function isAjax()
{
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
return true;
}
return false;
}
/**
* @description: 获取所有独立页,包括隐藏的
* @Date: 2023-03-21 19:43:52
* @Author: mulingyuer
*/
function getAllPages()
{
$db = Typecho_Db::get();
if (class_exists('\Typecho\Widget')) {
$widget = \Widget\Contents\Page\Rows::alloc();
foreach (array(
'stack' => array(),
'row' => array(),
'length' => 0,
) as $name => $val) {
try {
$reflect = new ReflectionClass($widget);
$property = $reflect->getProperty($name);
$property->setAccessible(true);
$property->setValue($widget, $val);
} catch (ReflectionException $e) {
}
}
} else {
$widget = new Widget_Contents_Page_List(Typecho_Request::getInstance(), Typecho_Widget_Helper_Empty::getInstance(), null);
}
$db->fetchAll($db->select()
->from('table.contents')
->where('table.contents.type = ?', 'page')
->where('table.contents.status = ? or table.contents.status = ?', 'publish', 'hidden')
->where('table.contents.created < ?', Helper::options()->time), array($widget, 'push'));
return $widget;
}
/**
* @description: 获取指定隐藏分页地址
* @param {*} $page 分页对象
* @param {*} $name 分页名称
* @Date: 2023-03-21 19:32:45
* @Author: mulingyuer
*/
function getHidePage($page, $name)
{
$link = [
"title" => "",
"href" => "",
];
getAllPages()->to($page);
while ($page->next()) {
if ($page->slug === $name) {
$link["title"] = $page->title;
$link["href"] = $page->permalink;
break;
}
}
return $link;
}
/**
* @description: 自定义关键字
* @Date: 2023-03-22 20:28:04
* @Author: mulingyuer
*/
if ($_SERVER['SCRIPT_NAME'] == __TYPECHO_ADMIN_DIR__ . 'write-post.php' || $_SERVER['SCRIPT_NAME'] == __TYPECHO_ADMIN_DIR__ . 'write-page.php') {
function themeFields($layout)
{
global $markdownThemeMap;
global $markdownHighlightMap;
//文章独享关键字
if ($_SERVER['SCRIPT_NAME'] == __TYPECHO_ADMIN_DIR__ . 'write-post.php') {
//自定义文章缩略图
$thumb = new Typecho_Widget_Helper_Form_Element_Text('thumb', null, null, _t('自定义缩略图'), _t('输入缩略图地址(仅文章有效)<style>.wmd-button-row {height:auto;}</style>'));
$layout->addItem($thumb);
//文章内容标题图
$titleImg = new Typecho_Widget_Helper_Form_Element_Text('titleImg', null, null, _t('自定义文章内容标题图'), _t('输入文章内容标题图地址(仅文章有效)<style>.wmd-button-row {height:auto;}</style>'));
$layout->addItem($titleImg);
}
// 文章主题
$markdownTheme = new Typecho_Widget_Helper_Form_Element_Select('markdownTheme', $markdownThemeMap, 'juejin', _t('文章主题'), _t('默认使用掘金主题'));
$layout->addItem($markdownTheme);
// 代码高亮
$highlightTheme = new Typecho_Widget_Helper_Form_Element_Select('highlightTheme', $markdownHighlightMap, null, _t('文章代码块主题'), _t('文章主题自带配套的代码高亮,如果你有定制需求,可以自行选择代码高亮主题,否则默认选择无即可。'));
$layout->addItem($highlightTheme);
}
}
/**
* @description: 获取文章title图片
* @param {*} $that
* @Date: 2023-03-22 20:28:15
* @Author: mulingyuer
*/
function getArticleTitleImg($that)
{
return $that->fields->titleImg;
}
/**
* @description: 获取文章主题
* @param {*} $that
* @Date: 2023-03-23 00:44:46
* @Author: mulingyuer
*/
function getArticleTheme($that)
{
$defaultTheme = Helper::options()->defaultMarkdownTheme;
$fieldsTheme = $that->fields->markdownTheme;
if (empty($fieldsTheme)) {
$fieldsTheme = 'juejin';
}
if ($defaultTheme !== 'juejin' && $fieldsTheme === 'juejin') {
$theme = $defaultTheme;
} else {
$theme = $fieldsTheme;
}
return $theme;
}
/**
* @description: 生成markdown主题样式link元素
* @param {*} $that
* @Date: 2023-12-22 23:13:27
* @Author: mulingyuer
*/
function getMarkdownTheme($that)
{
global $defaultMarkdownThemeHighlightMap;
$themeUrl = Helper::options()->themeUrl;
// 文章主题
$articleTheme = $that->fields->markdownTheme;
$defaultTheme = Helper::options()->defaultMarkdownTheme;
if (empty($articleTheme)) {
$articleTheme = 'juejin';
}
if ($defaultTheme !== 'juejin' && $articleTheme === 'juejin') {
$articleTheme = $defaultTheme;
}
// 代码高亮主题
$highlightTheme = $that->fields->highlightTheme;
if (empty($highlightTheme)) {
// 如果文章主题对应的代码高亮主题不存在
// 则使用默认代码高亮主题
if (array_key_exists($articleTheme, $defaultMarkdownThemeHighlightMap)) {
$highlightTheme = $defaultMarkdownThemeHighlightMap[$articleTheme];
} else {
$highlightTheme = 'juejin';
}
}
// 文章主题链接
$articleHref = $themeUrl . '/static/css/markdown/' . $articleTheme . '.css';
$articleLink = '<link href="' . $articleHref . '" rel="stylesheet">';
// 代码高亮主题链接
$highlightHref = $themeUrl . '/static/css/highlight/' . $highlightTheme . '.css';
$highlightLink = '<link href="' . $highlightHref . '" rel="stylesheet">';
echo $articleLink . $highlightLink;
}
/**
* @description: 获取用户组
* @Date: 2023-03-23 05:10:27
* @Author: mulingyuer
*/
function getGroup($uid = 0)
{
$db = Typecho_Db::get();
$prow = $db->fetchRow($db->select('group')->from('table.users')->where('uid = ?', $uid));
$group = $prow['group'];
if (empty($group)) {$group = 'visitor';}
return $group;
}
/**
* @description: 中文转义用户组
* @param {*} $uid 用户id
* @Date: 2023-03-23 05:09:12
* @Author: mulingyuer
*/
function chineseUserGroup($uid = null)
{
$userGroup = getGroup($uid);
$zhUserGroup;
switch ($userGroup) {
case 'administrator':
$zhUserGroup = '博主';
break;
case 'editor':
$zhUserGroup = '编辑';
break;
case 'contributor':
$zhUserGroup = '贡献者';
break;
case 'subscriber':
$zhUserGroup = '粉丝';
break;
default:
$zhUserGroup = '访客';
}
if (empty($zhUserGroup)) {
$zhUserGroup = '未知用户';
}
return $zhUserGroup;
}
/**
* @description: 给文章内容标题添加锚点
* @param {*} $content 文章内容
* @Date: 2023-03-23 20:43:02
* @Author: mulingyuer
*/
function addAnchorPoint($content)
{
global $catalog;
global $catalog_count;
$catalog = array();
$catalog_count = 0;
$content = preg_replace_callback('/<h([1-6])(.*?)>(.*?)<\/h\1>/i', function ($content) {
global $catalog;
global $catalog_count;
$catalog_count++;
$catalog[] = array('text' => trim(strip_tags($content[3])), 'depth' => $content[1], 'count' => $catalog_count);
return '<h' . $content[1] . $content[2] . ' id="heading-' . $catalog_count . '">' . $content[3] . '</h' . $content[1] . '>';
}, $content);
return $content;
}
/**
* @description: 获取文章目录树
* @Date: 2023-03-23 20:36:47
* @Author: mulingyuer
*/
function getDirectoryTree()
{
global $catalog;
$index = '';
if ($catalog) {
$index = '<ul class="directory-tree-list">' . "\n";
$prev_depth = '';
$to_depth = 0;
foreach ($catalog as $catalog_item) {
$catalog_depth = $catalog_item['depth'];
if ($prev_depth) {
if ($catalog_depth == $prev_depth) {
$index .= '' . "\n";
} elseif ($catalog_depth > $prev_depth) {
$to_depth++;
$index .= '<ul class="directory-tree-sub-list">' . "\n";
} else {
$to_depth2 = ($to_depth > ($prev_depth - $catalog_depth)) ? ($prev_depth - $catalog_depth) : $to_depth;
if ($to_depth2) {
for ($i = 0; $i < $to_depth2; $i++) {
$index .= '' . "\n" . '</ul>' . "\n";
$to_depth--;
}
}
$index .= '';
}
}
$index .= '<li class="directory-tree-list-item"><div class="directory-tree-list-item-container"><a class="directory-tree-list-item-link" href="#heading-' . $catalog_item['count'] . '" data-scroll="#heading-' . $catalog_item['count'] . '" title="' . $catalog_item['text'] . '"><i class="jj-icon jj-icon-send directory-tree-list-item-icon"></i>' . $catalog_item['text'] . '</a></div>';
$prev_depth = $catalog_item['depth'];
}
for ($i = 0; $i <= $to_depth; $i++) {
$index .= '' . "\n" . '</li></ul>' . "\n";
}
// $index = '<div id="toc-container">'."\n".'<div id="toc">'."\n".'<strong>文章目录</strong>'."\n".$index.'</div>'."\n".'</div>'."\n";
}
if (!$index) {
echo '<ul class="directory-tree-list"><div class="directory-tree-list-empty">暂无目录</div></ul>';
} else {
echo $index;
}
}
/**
* @description: 获取目录树
* @param {*} $maxDirectory 最大层级
* @Date: 2023-06-03 22:30:49
* @Author: mulingyuer
*/
function getJJDirectoryTree($maxDirectory = 3)
{
global $catalog;
$treeList = generateTreeList(array_replace_recursive(array(), $catalog));
echo generateTreeTemplate($treeList, $maxDirectory);
}
/**
* @description: 将扁平化目录树数组转成结构化目录树数组
* @param {*} $list 目录树数组
* @param {*} $depth 最大层级
* @Date: 2023-06-03 15:42:21
* @Author: mulingyuer
*/
function generateTreeList($list, $depth = 6)
{
if (count($list) <= 0 || $depth <= 1) {
return $list;
}
for ($i = count($list) - 1; $i >= 0; $i--) {
$item = $list[$i];
if ($item['depth'] == $depth) {
$parentIndex = $i - 1;
while ($parentIndex >= 0) {
$parent = &$list[$parentIndex];
if ($parent['depth'] < $depth) {
break;
}
$parentIndex--;
}
if ($parentIndex < 0) {
break;
}
if (!isset($parent['children'])) {
$parent['children'] = array();
}
array_unshift($parent['children'], $item);
array_splice($list, $i, 1);
}
}
$list = array_values($list);
return generateTreeList($list, $depth - 1);
}
/**
* @description: 删除目录树数组指定层级children
* @param {*} $list 目录树数组
* @param {*} $depth 最大层级
* @param {*} $currentDepth 当前层级
* @Date: 2023-06-03 15:49:03
* @Author: mulingyuer
*/
function removeChildren($list, $depth, $currentDepth = 0)
{
foreach ($list as &$item) {
if (isset($item['children']) && count($item['children']) > 0) {
if ($currentDepth < $depth - 1) {
$item['children'] = removeChildren($item['children'], $depth, $currentDepth + 1);
} else {
unset($item['children']);
}
}
}
return $list;
}
/**
* @description: 生成目录树html
* @param {*} $arr 目录树数组
* @param {*} $depth 最大层级
* @param {*} $currentDepth 当前层级
* @param {*} $isChildren 是否是子级
* @Date: 2023-06-03 16:48:54
* @Author: mulingyuer
*/
function generateTreeTemplate($arr, $depth, $currentDepth = 1, $isChildren = false)
{
if (count($arr) <= 0) {
return '<div class="directory-tree-list-empty">暂无目录</div>';
}
if ($currentDepth > $depth) {
return '';
}
$output = !$isChildren ? '<ul class="directory-tree-list">' : '';
foreach ($arr as $item) {
$output .= '<li class="directory-tree-list-item depth-' . $currentDepth . '"><div class="directory-tree-list-item-link-wrapper"><a class="directory-tree-list-item-link" href="#heading-' . $item['count'] . '" title="' . $item['text'] . '">' . $item['text'] . '</a></div>';
if (!empty($item['children']) && $currentDepth < $depth) {
$output .= '<ul class="directory-tree-sub-list">';
$output .= generateTreeTemplate($item['children'], $depth, $currentDepth + 1, true);
$output .= '</ul>';
}
$output .= '</li>';
}
$output .= !$isChildren ? '</ul>' : '';
return $output;
}
/**
* 增加浏览次数
* 使用方法: 在<code>themeInit</code>函数中添加代码
* <pre>if($archive->is('single') || $archive->is('page')){ viewsCounter($archive);}</pre>
*
* @param Widget_Archive $widget
* @return boolean
*/