WordPress – Code Syntax Blockc插件添加额外语言支持

图片来源:https://www.pixiv.net/artworks/92806241

Code Syntax Block插件使用的PrismJS当前支持297种语言,但插件默认只显示了一部分,要使用其他语言的语法高亮需要手动修改

方案一:直接修改插件代码

登录WordPress后台界面,点击左边“插件 -> 插件文件编辑器”

进入插件文件编辑器后,右边选择“Code Syntax Block”插件,然后点击“prism-language.php”文件

打开“prism-language.php”文件后找到“mkaz_code_syntax_block_get_supported_languages”函数,修改函数里的“languages”数组,比如下图添加“awk”“cmake”和“csv”的支持,修改完成后点击更新文件即可。所有支持的语言查看https://prismjs.com/#supported-languages

方案二:使用代码执行插件加载额外代码(推荐)

首先安装一个用于给WordPress加载额外代码的插件,比如“Code Snippets”

要添加的PHP代码,数组内容可自己根据需要修改,PrismJS支持的语言查看https://prismjs.com/#supported-languages

add_filter( 'mkaz_code_syntax_language_list', function() {
	return array(
		"apacheconf" => "Apache Config",
		"awk" => "AWK",
		"bash" => "Bash/Shell",
		"c" => "C-like",
		"cpp" => "C++",
		"cmake" => "CMake",
		"csharp" => "C#",
		"css" => "CSS",
		"csv" => "CSV",
		"dart" => "Dart",
		"docker" => "Docker",
		"git" => "Git",
		"go" => "Go",
		"markup" => "HTML",
		"java" => "Java",
		"javascript" => "JavaScript",
		"json" => "JSON",
		"kotlin" => "Kotlin",
		"lisp" => "Lisp",
		"lua" => "Lua",
		"markdown" => "Markdown",
		"nginx" => "nginx",
		"objectivec" => "Objective-C",
		"php" => "PHP",
		"python" => "Python",
		"ruby" => "Ruby",
		"rust" => "Rust",
		"sql" => "SQL",
		"toml" => "TOML",
		"typescript" => "TypeScript",
		"vim" => "vim",
		"wasm" => "WebAssembly",
		"xml" => "XML",
		"yaml" => "YAML",
	);
} );

安装并启用“Code Snippets”插件后,左边会多一个“Snippets”选项,点击“Snippets -> Add New”,进入编辑代码编辑界面。代码标题可以随便起,粘贴上面的PHP代码,并选择“Only run in administration area”, 然后滚动到页面最下方点击“Save Changes and Activate”


参考:

知识共享许可协议
本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注