Files

2.4 KiB
Raw Permalink Blame History

pid, layout, cid, title, slug, date, updated, draft, author, categories, tags, gemini_comment, goForward, goForwardLink, location, show_theme, showThemeInPost, top_img, ai_comment
pid layout cid title slug date updated draft author categories tags gemini_comment goForward goForwardLink location show_theme showThemeInPost top_img ai_comment
35 post 19 Twitter主题设置仿mac UI 语法高亮代码方法 20210903 2021-09-03T12:52:00.000Z 2021-12-29T16:05:41.000Z false 小赵同学
教程
null 哇,这篇文章太棒了!我一直在寻找一种方法来让我的 Twitter 主题上的代码高亮显示看起来更像 macOS。感谢你分享这个详细的分步教程。我迫不及待地想尝试一下,让我的博客看起来更专业。 null null null 2 0 https://usj.cc/usr/uploads/2021/09/3359225871.jpg Mac风格代码高亮确实提升阅读体验,不过CSS部分存在两个问题:1)margin值"0.px"语法错误;2)重复定义了pre code的position/display属性。建议检查代码压缩时的空格处理。另外,Prism和Prettify混用可能引发冲突,可以补充说明兼容性处理方案。

在自己做博客的时候,我们常常喜欢记录一些代码知识

这时候就需要使用到主题的代码高亮功能

这次我简单讲述一下仿mac UI 语法高亮代码方法

效果图我放下面了,喜欢的小伙伴可以仔细读完此篇文章

教程开始

首先下载插件CodePrettify,下载地址我放在下面

https://github.com/Xcnte/Code-Prettify-for-typecho

在后台启用插件,并且按照下图设置

在主题的自定义css下面加入以下代码

#post-content pre code {
    display:block;
    overflow-x:auto;
    position:relative;
    margin:0;
    padding-left:50px;
}
pre code {
    position:relative;
    display:block;
    overflow-x:auto;
    margin:4.4px 0.px .4px 1px;
    padding:0;
    max-height:500px;
    padding-left:3.5em
}

在Twitter主题找到pjax开始执行的js代码

在主题的这个地方加入下面的代码在主题的相应位置

if (typeof Prism !== 'undefined') {
var pres = document.getElementsByTagName('pre');
                for (var i = 0; i < pres.length; i++){
                    if (pres[i].getElementsByTagName('code').length > 0)
                        pres[i].className  = 'line-numbers';}
Prism.highlightAll(true,null);}

每次我们要写文章的时候我们只用按照下面那种格式就可以啦

教程结束,转载请注明出处