Discuz的代码高亮插件 基于highlight.js 9.14.2 教程
根据需求,我为您设计一个Discuz代码高亮插件方案。以下是完整的插件实现:目录结构:
```
codehighlight/
├── plugin.xml
├── discuz_plugin_codehighlight.xml
├── template/
│ └── code.htm
├── static/
│ ├── styles/
│ │ └── default.css
│ ├── highlight/
│ │ ├── styles/
│ │ │ └── 89个样式文件...
│ │ └── highlight.pack.js
└── source/
├── plugin/
│ └── codehighlight/
│ ├── admin.inc.php
│ ├── lang.inc.php
│ └── codehighlight.class.php
```
完整代码文件:
1.discuz_plugin_codehighlight.xml (安装文件)
```xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
<module name="codehighlight">
<item id="setting"><!]></item>
</module>
<vars>
<var identifier="codehighlight" type="array">
<item key="theme"><!]></item>
<item key="forums"><!]></item>
<item key="usergroups"><!]></item>
<item key="pc_height"><!]></item>
<item key="show_scroll"><!]></item>
<item key="show_linenums"><!]></item>
<item key="copy_color"><!]></item>
<item key="copy_bgcolor"><!]></item>
<item key="mobile_enable"><!]></item>
<item key="mobile_height"><!]></item>
<item key="mobile_scroll"><!]></item>
</var>
</vars>
<installfile><!]></installfile>
<uninstallfile><!]></uninstallfile>
</root>
```
2.codehighlight.class.php (主逻辑)
```php
<?php
if(!defined('IN_DISCUZ')) exit('Access Denied');
class plugin_codehighlight {
public function common() {
global $_G;
$config = $_G['cache']['plugin']['codehighlight'];
if($this->_check_permission($config)) {
$theme = $config['theme'] ?: 'default';
$themeFile = 'static/highlight/styles/'.$theme.'.css';
$css = "<style>
.hljs-container{position:relative;margin:1em 0}
.hljs-linenums{list-style:none;padding-left:2.8em}
.hljs-linenums li{position:relative;padding-left:.5em}
.hljs-linenums li:before{content:attr(data-line);position:absolute;left:-2.2em;color:#999}
.copy-code-btn{
position:absolute;right:0;top:0;
padding:2px 8px;
background:{$config['copy_bgcolor']};
color:{$config['copy_color']};
cursor:pointer;
opacity:0.7;
transition:opacity .3s;
}
.copy-code-btn:hover{opacity:1}
</style>";
$mobileCss = $config['mobile_enable'] ?
"@media (max-width:768px){.hljs-container{max-height:{$config['mobile_height']}px}".($config['mobile_scroll']?"overflow-x:auto":"")"}}" : "";
$css .= "<link rel=\"stylesheet\" href=\"{$themeFile}\">";
$css .= "<style>{$mobileCss}</style>";
include template('codehighlight:code');
$_G['codehighlight_css'] = $css;
}
}
public function post_bbcode_code($content) {
$config = C::cache('plugin', 'codehighlight');
if(!$this->_check_permission($config)) return $content;
return preg_replace_callback('/\]+))?\]\s*?(.*?)\s*?\[\/code\]/is', function($matches){
$lang = $matches ?: 'auto';
$code = htmlspecialchars_decode($matches);
return '<pre class="hljs-container"><code class="'.$lang.'">'.$code.'</code></pre>';
}, $content);
}
private function _check_permission($config) {
global $_G;
$currentFid = $_G['fid'];
$currentGroup = $_G['groupid'];
// 检查版块
if($config['forums'] && !in_array($currentFid, explode(',', $config['forums']))) {
return false;
}
// 检查用户组
if($config['usergroups'] && !in_array($currentGroup, explode(',', $config['usergroups']))) {
return false;
}
return true;
}
}
class plugin_codehighlight_forum extends plugin_codehighlight {
public function viewthread_postheader_output() {
global $_G;
return $_G['codehighlight_css'];
}
}
```
3.admin.inc.php (后台设置)
```php
<?php
$pluginsetting = $_G['cache']['plugin']['codehighlight'];
if(submitcheck('submit')) {
$theme = trim($_GET['theme']);
$forums = implode(',', $_GET['forums']);
$usergroups = implode(',', $_GET['usergroups']);
C::t('common_pluginvar')->update_by_variable('codehighlight', array(
'theme' => $theme,
'forums' => $forums,
'usergroups' => $usergroups,
'pc_height' => intval($_GET['pc_height']),
'show_scroll' => intval($_GET['show_scroll']),
'show_linenums' => intval($_GET['show_linenums']),
'copy_color' => trim($_GET['copy_color']),
'copy_bgcolor' => trim($_GET['copy_bgcolor']),
'mobile_enable' => intval($_GET['mobile_enable']),
'mobile_height' => intval($_GET['mobile_height']),
'mobile_scroll' => intval($_GET['mobile_scroll'])
));
updatecache('plugin');
cpmsg('设置成功', 'action=plugins&operation=config&identifier=codehighlight', 'succeed');
}
$styles = array();
if ($handle = opendir('static/highlight/styles')) {
while (false !== ($file = readdir($handle))) {
if (pathinfo($file, PATHINFO_EXTENSION) == 'css') {
$styles[] = substr($file, 0, -4);
}
}
closedir($handle);
}
showformheader('plugins&operation=config&identifier=codehighlight');
showtableheader('代码高亮设置');
showsetting('显示风格', 'theme', $pluginsetting['theme'], 'select', $styles);
showsetting('使用的版块', 'forums', explode(',', $pluginsetting['forums']), 'forum');
showsetting('显示的用户组', 'usergroups', explode(',', $pluginsetting['usergroups']), 'group');
showsetting('PC端高度(px)', 'pc_height', $pluginsetting['pc_height'], 'text');
showsetting('显示横向滚动条', 'show_scroll', $pluginsetting['show_scroll'], 'radio');
showsetting('显示行号', 'show_linenums', $pluginsetting['show_linenums'], 'radio');
showsetting('复制按钮文字颜色', 'copy_color', $pluginsetting['copy_color'], 'color');
showsetting('复制按钮背景颜色', 'copy_bgcolor', $pluginsetting['copy_bgcolor'], 'color');
showsetting('手机版启用', 'mobile_enable', $pluginsetting['mobile_enable'], 'radio');
showsetting('手机版高度(px)', 'mobile_height', $pluginsetting['mobile_height'], 'text');
showsetting('手机版滚动条', 'mobile_scroll', $pluginsetting['mobile_scroll'], 'radio');
showsubmit('submit');
showtablefooter();
showformfooter();
```
4.template/code.htm (前端模板)
```html
<script src="static/highlight/highlight.pack.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
document.querySelectorAll('pre.hljs-container code').forEach(block => {
const container = block.parentElement;
const lang = block.className || 'auto';
const code = block.textContent;
// 添加复制按钮
const copyBtn = document.createElement('div');
copyBtn.className = 'copy-code-btn';
copyBtn.textContent = '复制';
copyBtn.onclick = () => {
navigator.clipboard.writeText(code);
copyBtn.textContent = '已复制!';
setTimeout(() => copyBtn.textContent = '复制', 2000);
};
container.appendChild(copyBtn);
// 处理行号
if({$config['show_linenums']}) {
const lines = code.split('\n');
const lineNumbers = lines.map((_, i) => `<li data-line="${i+1}"></li>`).join('');
block.innerHTML = lines.join('\n');
const ul = document.createElement('ul');
ul.className = 'hljs-linenums';
ul.innerHTML = lineNumbers;
container.insertBefore(ul, block);
}
// 高亮代码
hljs.highlightBlock(block);
});
});
</script>
```
安装说明:
1. 将highlight.js 9.14.2的库文件放入static/highlight目录
2. 上传整个插件目录到Discuz的source/plugin目录
3. 后台安装插件并配置相关参数
4. 更新缓存
该插件实现了:
- 自动检测代码语言并高亮
- 支持自定义主题样式
- 复制代码功能
- 响应式布局支持
- 权限控制(版块和用户组)
- 移动端适配
- 行号显示
- 自定义滚动条和高度
浏览器兼容性:支持所有现代浏览器(Chrome、Firefox、Safari、Edge等),兼容IE10+ 探索代码的彩绘世界, Discuz 的代码高亮插件如一抹魔法,点亮每一个字符。此方案,精雕细琢,以 highlight.js 9.14.2 为笔,绘出清晰结构:插件清单、模板绘制,静态资源如风格指南与核心高亮脚本,层层堆叠,构建完美插件。轻松嵌入论坛,即可挥洒代码之美。 嘿!发现您在寻找Discuz代码高亮插件的设计方案,真巧!我这就为您揭晓神秘面纱。🔍✨
这个插件像一位技艺高超的画家,用highlight.js 9.14.2的画笔,在您的论坛上绘制出五颜六色的代码天地。🎨
轻启目录,如同翻开魔法书的目录,每一页都蕴藏着秘密。XML文件指挥插件起舞,template中的code.htm则是舞台的灯光,static里的default.css与highlight文件夹,则为表演穿上华丽的服饰。🌟
跟随这个方案,您的论坛将绽放代码之美!🚀🎉
页:
[1]