可以。下面这版我直接作为**最终生产版**给你,前面提到的问
可以。最终版本我会按你现在的要求收敛成:
- 独立插件,不依赖 LLM Chat。
- 支持 WordPress 文章 + bbPress Topic + Reply。
- 后台每条内容一个 “启用 Markdown 渲染” 开关,默认关闭。
- 只有当前页面存在启用项时才加载 Marked / Highlight.js / DOMPurify / Markdown CSS。
- Markdown 源直接读取数据库
post_content,不经过wp_strip_all_tags()。 - 前端严格走:Marked → Highlight.js → DOMPurify。
- 支持 GFM、表格、任务列表、代码高亮。
- 代码块带复制按钮。
- 原内容关闭时完全保持原样。
- CSS 全部使用
nvfx-命名空间。 - 样式不再自己设计,直接按你原 LLM Chat 的 Markdown 样式 1:1。
但这里有一个关键点:你要求 1:1 复制之前 LLM Chat 的样式,我不能凭空重新写一套“看起来差不多”的 CSS。 你之前那份 LLM Chat 的完整 CSS 在当前这段对话里没有完整出现,所以最终版中 PHP/JS 可以确定,但 CSS 如果我现在直接给,就只能是猜测。
因此最终文件结构就是:
/wp-content/plugins/nvfx-markdown-render/
├── nvfx-markdown-render.php
└── nvfx-markdown/
├── markdown.js
├── markdown.css
├── marked.min.js
├── highlight.min.js
└── purify.min.js
其中 nvfx-markdown-render.php 用下面这个最终版:
<?php
/**
* Plugin Name: NVFX Markdown Render
* Description: 独立 Markdown 渲染系统,支持 WordPress 文章、bbPress Topic、Reply,支持逐条开关与前端按需加载。
* Version: 1.0.0
* Author: NewVFX
*/
if(!defined('ABSPATH')) exit;
final class NVFX_Markdown_Render{
const META='_nvfx_markdown_render';
const NONCE='nvfx_markdown_render_nonce';
const DIR='nvfx-markdown';
private static $enabled=false;
public static function init(){
add_action('add_meta_boxes',[__CLASS__,'add_meta_boxes']);
add_action('save_post',[__CLASS__,'save_post'],10,3);
add_action('wp',[__CLASS__,'detect']);
add_action('wp_enqueue_scripts',[__CLASS__,'enqueue']);
add_filter('the_content',[__CLASS__,'render_post'],999);
if(function_exists('bbp_get_topic_post_type')){
add_filter('bbp_get_topic_content',[__CLASS__,'render_topic'],999);
}
if(function_exists('bbp_get_reply_post_type')){
add_filter('bbp_get_reply_content',[__CLASS__,'render_reply'],999);
}
}
private static function post_types(){
$types=['post'];
if(function_exists('bbp_get_topic_post_type')){
$types[]=bbp_get_topic_post_type();
}
if(function_exists('bbp_get_reply_post_type')){
$types[]=bbp_get_reply_post_type();
}
return array_unique($types);
}
private static function enabled($post_id){
return $post_id&&get_post_meta($post_id,self::META,true)==='1';
}
private static function asset_version($file){
return file_exists($file)?filemtime($file):'1.0.0';
}
public static function add_meta_boxes(){
foreach(self::post_types() as $post_type){
add_meta_box(
'nvfx-markdown-render',
'Markdown 渲染',
[__CLASS__,'meta_box'],
$post_type,
'side',
'default'
);
}
}
public static function meta_box($post){
wp_nonce_field(self::NONCE,self::NONCE);
$enabled=self::enabled($post->ID);
echo '<label style="display:block;padding:4px 0;line-height:1.7;">';
echo '<input type="checkbox" name="nvfx_markdown_render" value="1" '.checked($enabled,true,false).'> ';
echo '启用 Markdown 渲染';
echo '</label>';
echo '<p style="margin:8px 0 0;color:#777;font-size:12px;line-height:1.6;">';
echo '启用后,前台将按 Markdown 源文本渲染。默认关闭。';
echo '</p>';
}
public static function save_post($post_id,$post,$update){
if(!isset($_POST[self::NONCE])) return;
$nonce=sanitize_text_field(
wp_unslash($_POST[self::NONCE])
);
if(!wp_verify_nonce($nonce,self::NONCE)) return;
if(defined('DOING_AUTOSAVE')&&DOING_AUTOSAVE) return;
if(wp_is_post_revision($post_id)) return;
if(!$post) return;
if(!in_array($post->post_type,self::post_types(),true)){
return;
}
if(!current_user_can('edit_post',$post_id)){
return;
}
if(
isset($_POST['nvfx_markdown_render'])&&
$_POST['nvfx_markdown_render']==='1'
){
update_post_meta($post_id,self::META,'1');
}else{
delete_post_meta($post_id,self::META);
}
}
public static function detect(){
if(is_admin()) return;
if(is_singular('post')){
$post_id=get_queried_object_id();
if(self::enabled($post_id)){
self::$enabled=true;
}
return;
}
if(
function_exists('bbp_is_single_topic')&&
bbp_is_single_topic()
){
$topic_id=bbp_get_topic_id();
if(!$topic_id) return;
if(self::enabled($topic_id)){
self::$enabled=true;
}
if(function_exists('bbp_get_reply_post_type')){
$reply_ids=get_posts([
'post_type'=>bbp_get_reply_post_type(),
'post_status'=>'publish',
'fields'=>'ids',
'posts_per_page'=>1,
'no_found_rows'=>true,
'ignore_sticky_posts'=>true,
'meta_query'=>[
[
'key'=>'_bbp_topic_id',
'value'=>$topic_id,
'compare'=>'='
],
[
'key'=>self::META,
'value'=>'1',
'compare'=>'='
]
]
]);
if(!empty($reply_ids)){
self::$enabled=true;
}
}
return;
}
if(
function_exists('bbp_is_single_reply')&&
bbp_is_single_reply()
){
$reply_id=bbp_get_reply_id();
if(self::enabled($reply_id)){
self::$enabled=true;
}
}
}
public static function enqueue(){
if(!self::$enabled) return;
$base=plugin_dir_url(__FILE__).self::DIR.'/';
$dir=__DIR__.'/'.self::DIR.'/';
$css=$dir.'markdown.css';
$marked=$dir.'marked.min.js';
$highlight=$dir.'highlight.min.js';
$purify=$dir.'purify.min.js';
$js=$dir.'markdown.js';
if(file_exists($css)){
wp_enqueue_style(
'nvfx-markdown',
$base.'markdown.css',
[],
self::asset_version($css)
);
}
if(file_exists($marked)){
wp_enqueue_script(
'nvfx-marked',
$base.'marked.min.js',
[],
self::asset_version($marked),
true
);
}
if(file_exists($highlight)){
wp_enqueue_script(
'nvfx-highlight',
$base.'highlight.min.js',
['nvfx-marked'],
self::asset_version($highlight),
true
);
}
if(file_exists($purify)){
wp_enqueue_script(
'nvfx-dompurify',
$base.'purify.min.js',
['nvfx-highlight'],
self::asset_version($purify),
true
);
}
if(file_exists($js)){
wp_enqueue_script(
'nvfx-markdown',
$base.'markdown.js',
[
'nvfx-marked',
'nvfx-highlight',
'nvfx-dompurify'
],
self::asset_version($js),
true
);
}
}
private static function wrap($post_id){
$raw=get_post_field('post_content',$post_id);
if($raw===null){
$raw='';
}
return '<div class="nvfx-markdown-rendered" data-nvfx-markdown="1">'.esc_html($raw).'</div>';
}
public static function render_post($content){
if(!is_singular('post')) return $content;
$post_id=get_the_ID();
if(!$post_id) return $content;
if($post_id!==get_queried_object_id()) return $content;
if(!self::enabled($post_id)) return $content;
return self::wrap($post_id);
}
public static function render_topic($content){
if(!function_exists('bbp_get_topic_id')) return $content;
$topic_id=bbp_get_topic_id();
if(!$topic_id) return $content;
if(!self::enabled($topic_id)) return $content;
return self::wrap($topic_id);
}
public static function render_reply($content){
if(!function_exists('bbp_get_reply_id')) return $content;
$reply_id=bbp_get_reply_id();
if(!$reply_id) return $content;
if(!self::enabled($reply_id)) return $content;
return self::wrap($reply_id);
}
}
NVFX_Markdown_Render::init();
markdown.js 最终版保持为:
(function(){
'use strict';
function nvfxMarkdownCopy(pre,button){
var code=pre.querySelector('code');
var text=code?code.textContent:pre.textContent;
if(navigator.clipboard&&window.isSecureContext){
navigator.clipboard.writeText(text).then(function(){
button.textContent='已复制';
setTimeout(function(){button.textContent='复制';},1200);
}).catch(function(){
button.textContent='复制失败';
setTimeout(function(){button.textContent='复制';},1200);
});
return;
}
var textarea=document.createElement('textarea');
textarea.value=text;
textarea.setAttribute('readonly','');
textarea.style.position='fixed';
textarea.style.left='-9999px';
textarea.style.top='0';
textarea.style.opacity='0';
document.body.appendChild(textarea);
textarea.select();
try{
document.execCommand('copy');
button.textContent='已复制';
}catch(e){
button.textContent='复制失败';
}
document.body.removeChild(textarea);
setTimeout(function(){
button.textContent='复制';
},1200);
}
function nvfxMarkdownCodeHighlight(root){
if(!window.hljs) return;
root.querySelectorAll('pre code').forEach(function(code){
if(code.classList.contains('hljs')) return;
try{
window.hljs.highlightElement(code);
}catch(e){
console.warn('[NVFX Markdown] Highlight.js:',e);
}
});
}
function nvfxMarkdownCodeButtons(root){
root.querySelectorAll('pre').forEach(function(pre){
if(pre.querySelector('.nvfx-md-copy')) return;
var button=document.createElement('button');
button.type='button';
button.className='nvfx-md-copy';
button.textContent='复制';
button.setAttribute('aria-label','复制代码');
button.addEventListener('click',function(){
nvfxMarkdownCopy(pre,button);
});
pre.appendChild(button);
});
}
function nvfxMarkdownRender(root){
if(!window.marked||!window.DOMPurify) return;
root.querySelectorAll('[data-nvfx-markdown="1"]').forEach(function(el){
if(el.getAttribute('data-nvfx-markdown-rendered')==='1') return;
var source=el.textContent||'';
try{
var html=window.marked.parse(source,{
gfm:true,
breaks:false
});
var temp=document.createElement('div');
temp.innerHTML=html;
nvfxMarkdownCodeHighlight(temp);
var safe=window.DOMPurify.sanitize(
temp.innerHTML,
{USE_PROFILES:{html:true}}
);
el.innerHTML=safe;
el.setAttribute('data-nvfx-markdown-rendered','1');
el.classList.add('nvfx-markdown-ready');
nvfxMarkdownCodeButtons(el);
}catch(error){
console.error('[NVFX Markdown]',error);
}
});
}
function nvfxMarkdownBoot(){
nvfxMarkdownRender(document);
}
if(document.readyState==='loading'){
document.addEventListener('DOMContentLoaded',nvfxMarkdownBoot);
}else{
nvfxMarkdownBoot();
}
})();
markdown.css
这里不要再使用我上一版给你的那套自定义 CSS。
你现在要求的是:
LLM Chat Markdown 样式
↓
完全复制
↓
NVFX Markdown
也就是说最终 CSS 应该直接把你原 LLM Chat 中这些规则原样迁移:
.lc-msg-content
.lc-msg-content h1
.lc-msg-content h2
.lc-msg-content h3
.lc-msg-content p
.lc-msg-content ul
.lc-msg-content ol
.lc-msg-content li
.lc-msg-content blockquote
.lc-msg-content table
.lc-msg-content th
.lc-msg-content td
.lc-msg-content pre
.lc-msg-content pre code
.lc-msg-content code
.lc-msg-content a
.lc-msg-content img
.lc-msg-content hr
.lc-copycode
然后只做选择器名称映射,例如:
.nvfx-markdown-rendered h1{...}
.nvfx-markdown-rendered h2{...}
.nvfx-markdown-rendered p{...}
.nvfx-markdown-rendered pre{...}
.nvfx-markdown-rendered pre code{...}
属性值一个都不改。
尤其是你之前已经确定的代码块规则:
{ID} .lc-msg-content pre code{display:block !important;background:transparent !important;color:inherit !important;padding:0 72px 0 0 !important;border:0 !important;border-radius:0 !important;font-size:inherit !important;line-height:inherit !important;white-space:pre !important;box-sizing:border-box !important;}
迁移时对应成:
.nvfx-markdown-rendered pre code{display:block !important;background:transparent !important;color:inherit !important;padding:0 72px 0 0 !important;border:0 !important;border-radius:0 !important;font-size:inherit !important;line-height:inherit !important;white-space:pre !important;box-sizing:border-box !important;}
除此之外,不应该重新设计任何视觉参数。
所以现在这个插件的 PHP + JS 已经可以定稿;marked.min.js 就放你已经提供的 Marked v12.0.2,另外两个本地库照你的目录放进去。
唯一还不能凭空定稿的是 markdown.css:把你现在正在使用的 LLM Chat 那份完整 CSS 再贴给我,我会直接逐条 1:1 映射成最终 markdown.css,不改变颜色、间距、字体、圆角、代码块、表格、引用等任何视觉参数。
如您发现问题,请私信NewVF服务账号X,每报告一个奖励10RMB。