Files
hugo-theme-chirpy/layouts/partials/refactor-content.html
2025-04-28 21:55:12 +08:00

63 lines
3.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.

{{ $content := .Content }}
<!-- Change checkbox icons -->
{{ if findRE `<input.*type="checkbox"` $content }}
<!-- first replace the li with task-list-item class -->
{{ $content = replaceRE `<li>(\s*)<input` `<li class="task-list-item">$1<input` $content }}
{{ $content = replaceRE `<li class="([^"]*)"(\s*)<input` `<li class="task-list-item $1"$2<input` $content }}
<!-- then add task-list class to the parent ul of the li with task-list-item class -->
{{ $content = replaceRE `<ul>(\s*)<li class="task-list-item` `<ul class="task-list">$1<li class="task-list-item` $content }}
{{ $content = replaceRE `<ul class="([^"]*)"(\s*)<li class="task-list-item` `<ul class="task-list $1"$2<li class="task-list-item` $content }}
<!-- replace all checked checkbox format -->
{{ $content = replaceRE `<input.*?checked.*?type="checkbox".*?>` `<i class="fas fa-check-circle fa-fw checked"></i>` $content }}
{{ $content = replaceRE `<input.*?type="checkbox".*?checked.*?>` `<i class="fas fa-check-circle fa-fw checked"></i>` $content }}
<!-- replace all unchecked checkbox format -->
{{ $content = replaceRE `<input.*?type="checkbox".*?>` `<i class="far fa-circle fa-fw"></i>` $content }}
{{ end }}
<!-- Create heading anchors -->
{{ $headingLevels := slice "2" "3" "4" "5" }}
{{ $headingContent := $content }}
{{ range $level := $headingLevels }}
{{ $markStart := printf `<h%s id="` $level }}
{{ $markEnd := printf `</h%s>` $level }}
{{ $pattern := printf `%s([^"]+)">(.*?)%s` $markStart $markEnd }}
{{ $replacement := printf `%s$1"><span class="me-2">$2</span><a href="#$1" class="anchor text-muted"><i class="fas fa-hashtag"></i></a>%s` $markStart $markEnd }}
{{ $headingContent = replaceRE $pattern $replacement $headingContent }}
{{ end }}
{{ $content = $headingContent | safeHTML }}
<!-- Insert code header after highlight div and extract language from code class or file attribute -->
{{ if findRE `<div class="highlight"` $content }}
<!-- Add a special marker at the beginning of every <div class="highlight"> -->
{{ $content = replaceRE `<div class="highlight"` `<div class="CHIRPY-HIGHLIGHT-MARKER highlight"` $content }}
<!-- Process highlight divs with file attribute -->
{{ $content = replaceRE `<div class="CHIRPY-HIGHLIGHT-MARKER highlight"([^>]*?)file="([^"]*)"([^>]*?)>` `<div class="highlight"$1file="$2"$3><div class="code-header"> <span data-label-text="$2"><i class="far fa-file-code fa-fw"></i></span> <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div>` $content }}
<!-- Process remaining highlight divs that still have the marker (no file attribute) -->
{{ $content = replaceRE `<div class="CHIRPY-HIGHLIGHT-MARKER highlight"([^>]*?)>([\s\S]*?)<code class="language-([^"]*)"` `<div class="highlight"$1><div class="code-header"> <span data-label-text-language="$3"><i class="fas fa-code fa-fw small"></i></span> <button aria-label="copy" data-title-succeed="Copied!"><i class="far fa-clipboard"></i></button></div>$2<code class="language-$3"` $content }}
<!-- Process language aliases in data-label-text -->
{{ $pattern := `<span data-label-text-language="([^"]*)"` }}
<!-- 查找所有语言标签逐个处理 -->
{{ range findRE $pattern $content -1 }}
{{ $langRaw := replaceRE $pattern "$1" . }}
{{ $langNormalized := partial "language-alias.html" (dict "language" $langRaw) }}
{{ if ne $langNormalized "" }}
{{ $replacement := printf `<span data-label-text="%s"` $langNormalized }}
{{ $original := printf `<span data-label-text-language="%s"` $langRaw }}
{{ $content = replace $content $original $replacement }}
{{ end }}
{{ end }}
{{ end }}
{{ $content | safeHTML }}