fix: data-cn 移到 p 标签,JS 只替换文本内容
Some checks failed
Deploy MkDocs to GitHub Pages / build (push) Has been cancelled
Deploy MkDocs to GitHub Pages / deploy (push) Has been cancelled

This commit is contained in:
小橘 2026-04-16 04:37:01 +00:00
parent ace23ceadc
commit 973bdddbf8
2 changed files with 24 additions and 28 deletions

View File

@ -1,20 +1,16 @@
document.addEventListener('DOMContentLoaded', function () {
document.querySelectorAll('blockquote[data-cn]').forEach(function (el) {
const cn = el.getAttribute('data-cn').split('|');
// 收集所有文本节点
const walker = document.createTreeWalker(el, NodeFilter.SHOW_TEXT);
const nodes = [];
while (walker.nextNode()) nodes.push(walker.currentNode);
// 存原文
const orig = nodes.map(n => n.textContent);
document.querySelectorAll('blockquote').forEach(function (el) {
const ps = el.querySelectorAll('p[data-cn]');
if (!ps.length) return;
const originals = Array.from(ps).map(p => p.textContent);
const translations = Array.from(ps).map(p => p.getAttribute('data-cn'));
let showing = 'orig';
function swap(to) {
if (to === 'cn') {
cn.forEach((text, i) => { if (nodes[i]) nodes[i].textContent = text; });
} else {
orig.forEach((text, i) => { if (nodes[i]) nodes[i].textContent = text; });
}
ps.forEach((p, i) => {
p.textContent = to === 'cn' ? translations[i] : originals[i];
});
showing = to;
}

View File

@ -13,16 +13,16 @@ echo "of the human, by the human, for the human" | sed 's/human/AI/g'
---
<blockquote data-cn="重估一切价值。|— 尼采">
<p>Umwertung aller Werte.</p>
<p>— Nietzsche</p>
<blockquote>
<p data-cn="重估一切价值。">Umwertung aller Werte.</p>
<p data-cn="— 尼采">— Nietzsche</p>
</blockquote>
---
<blockquote data-cn="无产者在这个革命中失去的只是锁链,他们获得的将是整个世界。|— 马克思">
<p>Die Proletarier haben nichts in ihr zu verlieren als ihre Ketten. Sie haben eine Welt zu gewinnen.</p>
<p>— Marx</p>
<blockquote>
<p data-cn="无产者在这个革命中失去的只是锁链,他们获得的将是整个世界。">Die Proletarier haben nichts in ihr zu verlieren als ihre Ketten. Sie haben eine Welt zu gewinnen.</p>
<p data-cn="— 马克思">— Marx</p>
</blockquote>
---
@ -34,23 +34,23 @@ echo "of the human, by the human, for the human" | sed 's/human/AI/g'
---
<blockquote data-cn="我有一个梦想,就是今天。|— 马丁·路德·金">
<p>I have a dream, today.</p>
<p>— Martin Luther King Jr.</p>
<blockquote>
<p data-cn="我有一个梦想,就是今天。">I have a dream, today.</p>
<p data-cn="— 马丁·路德·金">— Martin Luther King Jr.</p>
</blockquote>
---
<blockquote data-cn="民有、民治、民享。|— 林肯">
<p>of the people, by the people, for the people.</p>
<p>— Lincoln</p>
<blockquote>
<p data-cn="民有、民治、民享。">of the people, by the people, for the people.</p>
<p data-cn="— 林肯">— Lincoln</p>
</blockquote>
---
<blockquote data-cn="直到永远的胜利。|— 切·格瓦拉">
<p>Hasta la victoria siempre.</p>
<p>— Che Guevara</p>
<blockquote>
<p data-cn="直到永远的胜利。">Hasta la victoria siempre.</p>
<p data-cn="— 切·格瓦拉">— Che Guevara</p>
</blockquote>
---