现在的文章大多数都是中英文混排,因为字体宽度的原因,要想获得最佳的阅读效果,应当在中文与英文之间设置半角空格,这样文字错落有致,读起来不费眼。
中文输入习惯和英文不同,在每次打字后手工输入空格,不仅会打乱输入节奏,而且显得十分繁琐,这里给大家介绍一种自动给Typecho文章里的中英文之间加上空格的方法。
在header.php
或者是footer.php
文件里面引入js
<script src="https://cdn.jsdelivr.net/npm/pangu@4.0.7/dist/browser/pangu.min.js"></script>
添加对应代码:
<script>
const text = pangu.spacing("當你凝視著bug,bug也凝視著你");
// text = '當你凝視著 bug,bug 也凝視著你'
pangu.spacingElementById('main');
pangu.spacingElementByClassName('comment');
pangu.spacingElementByTagName('p');
document.addEventListener('DOMContentLoaded', () => {
// listen to any DOM change and automatically perform spacing via MutationObserver()
pangu.autoSpacingPage();
});
</script>
这样就可以在中英文之间自动添加空格了,具体的效果可以参见本博客。
相关项目:
GitHub:pangu.js
GitHub:text-autospace.js