halo-theme-hao/templates/modules/libs/code-highlight.html

72 lines
2.7 KiB
HTML
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.

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<th:block th:fragment="import">
<!--<link rel="stylesheet"-->
<!-- th:href="@{/assets/libs/highlight.js/styles/{theme}(theme=${theme.config.content.code_highlight_style})}"/>-->
<!--<script th:src="@{/assets/libs/highlight.js/highlight.min.js}"></script>-->
<script src="https://unpkg.com/shiki"></script>
</th:block>
<th:block th:fragment="script">
<!--<script>-->
<!-- (async function () {-->
<!-- const extraLanguages = "[[${theme.config.content.code_highlight_extra_languages}]]".split(",").filter((x) => x);-->
<!-- for (let i = 0; i < extraLanguages.length; i++) {-->
<!-- const lang = extraLanguages[i];-->
<!-- if (lang) {-->
<!-- await loadScript("[[${#theme.assets('/libs/highlight.js/languages')}]]/" + lang + ".min.js");-->
<!-- }-->
<!-- }-->
<!-- console.log("Extra languages: ", extraLanguages);-->
<!-- document.querySelectorAll("pre code").forEach((el) => {-->
<!-- hljs.highlightElement(el);-->
<!-- });-->
<!-- console.log("Loaded languages: ", hljs.listLanguages());-->
<!-- })();-->
<!-- function loadScript(url) {-->
<!-- return new Promise(function (resolve, reject) {-->
<!-- const script = document.createElement("script");-->
<!-- script.type = "text/javascript";-->
<!-- script.src = url;-->
<!-- script.onload = resolve;-->
<!-- script.onerror = reject;-->
<!-- document.head.appendChild(script);-->
<!-- });-->
<!-- }-->
<!--</script>-->
<script>
$("article pre>code").each(function () {
console.log(this.innerHTML)
// 去除空格以及多余标点
var codeId = $(this).text().replace(/[\s|\~|`|\!|\@|\#|\$|\%|\^|\&|\*|\(|\)|\_|\+|\=|\||\|\[|\]|\{|\}|\;|\:|\"|\'|\,|\<|\.|\>|\/|\?|\|\|\。]/g, '');
codeId = codeId.toLowerCase();
if (idArr[codeId]) {
// id已经存在
$(this).attr('id', codeId + '-' + idArr[codeId]);
idArr[codeId]++;
} else {
// id未存在
idArr[codeId] = 1;
$(this).attr('id', codeId);
}
shiki.getHighlighter({
theme: 'nord'
}).then(highlighter => {
const code = highlighter.codeToHtml(`console.log('shiki');`, {lang: 'js'})
document.getElementById('codeId').innerHTML = code
})
})
</script>
</th:block>
</html>