Files
wordpress-kratos-yang/functions.php
T
laoyang da0d9ae4a1 代码高亮整套自研:10 浅 + 10 深配色、语言标签、评论区贴代码 (#11)
不再依赖第三方高亮插件,全部由主题自己实现。

主题配色(20 套)
- 浅色 10:github / vs / atom-one-light / idea / xcode /
  stackoverflow-light / intellij-light / googlecode / magula / color-brewer
- 深色 10:github-dark / atom-one-dark / monokai / github-dark-dimmed /
  a11y-dark / stackoverflow-dark / night-owl / srcery /
  panda-syntax-dark / tomorrow-night-blue
- 深色卡片带 data-kind="dark",顶栏跟着换深色底
- 只加载当前选中的一套(20 套全加载会让后出现的规则压前面的,
  切换配色容易"看起来没变")

代码语言标签(用户要求:防止复制错语言)
- 作者标了 language-xxx → 实线边框、正体
- 自动识别猜出来的 → 虚线边框、斜体
- 认不出来 → 显示 "—",仍保持虚线斜体(不冒充作者标注)

行号
- 默认关;行号槽在 <pre> 外面,复制天然不带行号
- 改掉原主题的 word-break:break-all(任意字符断行,代码像乱码)
- 归零 hljs 主题自带的 pre code.hljs{padding:1em},
  否则代码被压低 1em、与行号槽错位 15.5px

复制
- 默认开;安全上下文判断 + execCommand 退路(HTTP 下 clipboard 是 undefined)
- 取 textContent 而非 innerHTML
- 默认 opacity:0、hover 显示、触屏覆盖回 1
  (旧写法只在 @media(hover:none) 里写 1,等于空操作)

自动识别
- 做成选项,默认开
- 只对没标语言的块生效;站内实测长块准确率 22%,不能覆盖作者标注

评论区贴代码
- 评论工具条加"代码"按钮 + 29 种语言面板
- kses 放行 pre 并给 pre/code 放开 class
  (WP 默认 pre 整条剥掉、code 属性全清,此前根本贴不了代码)
- 放行 pre 之后 wpautop/wptexturize/make_clickable 自动让路,
  代码里的连续空行、引号、URL 都不会被改写
- comments.js 用占位符保护代码块,避免 walk() 的 /\n{3,}/ 压缩

其它
- 资源版本号改为读主题头,不再写死(写死会导致以后升级后
  浏览器一直用旧缓存,改了 CSS/JS 完全不生效)
- 删除 footer.php 里旧的 Prism 兜底(两套高亮器同页会互相打架)

验收
- 用线上 284 个真实代码块跑产品代码:284/284 全部变卡片,内容丢字 0、双转义 0
- 47 个 text 标记全部映射到 plaintext(hljs 无 text 语言,不映射会静默不上色)
- 80 个裸 <pre>(无 code 子元素)全部覆盖
- 核心纯函数 65/65;核心反向 10/10 变红
- 浏览器端到端 32/32;行号几何逐行对齐偏差 0.00px,几何反向 4/4 变红
- 端到端反向 7/7 变红;语言标签反向 2/2 变红
- 全量 PHP 语法 95 文件 0 错误

发布:走 theme-update 通道(zip + readme + update.json),
未覆盖服务器主题目录;线上主题目录仍是 3.2.41,待后台升级。
2026-10-26 09:00:17 +08:00

94 lines
4.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* 模板函数
* @author Yongquan Yang <yangyq.net>
* @license MIT License
* @version 2020.03.17
*/
define('THEME_VERSION', wp_get_theme()->get('Version'));
// 主题配置
require get_template_directory() . '/inc/options-framework/autoload.php';
// 自定义代码(CSS / JS,存选项表而非主题目录)
require get_template_directory() . '/inc/theme-custom-code.php';
// 更新配置
require get_template_directory() . '/inc/update-checker/autoload.php';
// 核心配置
require get_template_directory() . '/inc/theme-core.php';
// 图片类选项的默认值(Logo/favicon/404/分享图… 空值时落到包内默认图)。
// 必须排在 theme-core.php 之后:它要用 ASSET_PATH 与 kratos_option。
require get_template_directory() . '/inc/theme-option-defaults.php';
// 站点配置
require get_template_directory() . '/inc/theme-setting.php';
// 纪念日自动哀悼(按日期判断,需在 theme-setting 之后,
// 因为它用到 kratos_option)
require get_template_directory() . '/inc/theme-mourning.php';
// 哀悼说明页(固定路径 /memory,由主题判定路由,不写数据库)
require get_template_directory() . '/inc/theme-mourning-page.php';
// 文章配置
require get_template_directory() . '/inc/theme-article.php';
// 代码高亮:highlight.js 自托管,接管正文与评论区的所有代码块。
// 必须排在 theme-core.php 之后:它要用 kratos_option 读三个开关。
// 必须**早于 theme-comments.php**:评论区渲染要调 yang_hl_render_block()。
// 挂在 the_content 优先级 9 —— 早于 wpautop(10),
// 因为裸 <pre>(站内实测 80 个,占 28.2%)一旦先被 wpautop 包进 <p>,
// 行首缩进就会被 white-space:normal 折叠掉,代码完全没法读。
require get_template_directory() . '/inc/theme-highlight.php';
// 评论区(统一渲染 + AJAX + 评论通知)。
// 必须在 theme-article.php 之后:后者不再定义评论函数,
// 但评论模板 comments.php 里引用的 comment_callbacks 由本文件提供。
// 也必须早于 theme-mail.php(评论通知要调 yang_mail_render)。
require get_template_directory() . '/inc/theme-comments.php';
// 小工具配置
require get_template_directory() . '/inc/theme-widgets.php';
// 文章增强
require get_template_directory() . '/inc/theme-shortcode.php';
// 文章目录([TOC] 短代码)
require get_template_directory() . '/inc/theme-toc.php';
// 添加导航目录
require get_template_directory() . '/inc/theme-navwalker.php';
// 对象存储配置
require get_template_directory() . '/inc/theme-dogecloud.php';
// CDN 加速(地址改写:CDN 地址 + 白名单 + 黑名单)。
// 必须排在 theme-core.php 之后:它要用 kratos_option 与 wp_parse_url。
// 挂在 template_redirect 上开输出缓冲,所以本文件晚加载不影响生效。
require get_template_directory() . '/inc/theme-cdn.php';
// 自动头像:为没有头像的访客按邮箱生成一张(纯 PHP,零外网请求)。
// 必须排在 theme-core.php 之后:它要用 kratos_option。
// 它挂在 get_avatar 过滤器上(优先级 20,晚于 theme-core.php 里
// 那个改 Gravatar 域名的过滤器),所以要保证 theme-core.php 先加载。
require get_template_directory() . '/inc/theme-avatar.php';
// 图片灯箱:正文图片点击在当前页放大、多图轮播、键盘切换。
// 必须排在 theme-core.php 之后:它要用 ASSET_PATH 与 THEME_VERSION。
// 必须排在 theme-article.php 之后:它挂在 the_content 优先级 20,
// 要去掉 imgnofollow()(优先级 10)给图片链接加的 target="_blank"。
require get_template_directory() . '/inc/theme-lightbox.php';
// 邮件模板:必须先于 theme-mail.php 加载,
// 因为后者在发信时会调用 yang_mail_render()
require get_template_directory() . '/inc/theme-mail-template.php';
// 邮件治理:统一接管发信通道、去重、关掉重复通知
require get_template_directory() . '/inc/theme-mail.php';
// SMTP 配置(连通性相关,实际接管在 theme-mail.php)
require get_template_directory() . '/inc/theme-smtp.php';