-
Notifications
You must be signed in to change notification settings - Fork 285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
代码折叠如何开启? #76
Comments
尝试下 Hugo 的短代码? 比如这个:https://github.com/jiridj/hugo-collapsible-code(可能还需要自己修改下,把 CSS 和 JS 放到 MemE 提供的自定义文件中引入下) 在线效果见 https://jiridj.be/posts/collapsible-code-block/,短代码的食用方法可参考 https://github.com/martignoni/hugo-notice。 或者你也可以自己写一个…… 参考: |
哦哦,好的,多谢 |
MemE 提供的自定义文件中引入 CSS 和 JS 这个在哪?我在static中引入了hugo-collapsible-code的css,和js,但是代码没有折叠? |
需要自己新建:
|
行,稍等,我看看。 |
<div class="highlight-wrapper">
{{ if len .Params | eq 2 }}
{{ highlight (trim .Inner "\n\r") (.Get 0) (.Get 1) }}
{{ else }}
{{ highlight (trim .Inner "\n\r") (.Get 0) "" }}
{{ end }}
</div>
.highlight-wrapper {
position: relative;
margin-top: -1rem;
margin-bottom: 2rem;
}
.highlight-link {
position: absolute;
bottom: 0.5em;
right: 0.5em;
}
// Collapsible Hugo code blocks
// by Jiri De Jagere, @JiriDJ
const height = 300;
makeCollapsible();
function makeCollapsible() {
const divs = document.querySelectorAll('.highlight-wrapper');
divs.forEach((e) => {
if (e.offsetHeight > height) {
e.style.maxHeight = height + 'px';
e.style.overflow = 'hidden';
const linkWrapper = document.createElement('div');
const link = document.createElement('a');
link.href = '';
link.textContent = 'more';
link.addEventListener('click', codeToggle);
linkWrapper.className = 'highlight-link';
linkWrapper.appendChild(link);
e.appendChild(linkWrapper);
}
});
}
function codeToggle(e) {
e.preventDefault();
const link = e.target;
const linkWrapper = link.parentElement.parentElement;
if (link.innerHTML === 'more') {
link.innerHTML = 'less';
linkWrapper.style.maxHeight = '';
linkWrapper.style.overflow = 'none';
link.parentElement.style.right = '0';
} else {
link.innerHTML = 'more';
linkWrapper.style.maxHeight = height + 'px';
linkWrapper.style.overflow = 'hidden';
link.parentElement.style.right = '0.5em';
scrollToTargetAdjusted(linkWrapper);
}
}
function scrollToTargetAdjusted(e) {
const header = document.querySelector('header');
const headerHeight = window.getComputedStyle(header, null).getPropertyValue('height');
const headerOffset = Number(headerHeight.replace('px', ''));
const bodyRect = document.body.getBoundingClientRect().top;
const elementRect = e.getBoundingClientRect().top;
const elementPosition = elementRect - bodyRect;
const offsetPosition = elementPosition - headerOffset;
window.scrollTo({
top: offsetPosition,
behavior: 'smooth'
});
} |
……又看了看,发现可以完全不用短代码,直接 JS 就能解决……
.highlight {
position: relative;
}
.highlight-link {
position: absolute;
bottom: 0.5em;
right: 0.5em;
}
// Collapsible Hugo code blocks
// by Jiri De Jagere, @JiriDJ
const height = 300;
makeCollapsible();
function makeCollapsible() {
const divs = document.querySelectorAll('.highlight');
divs.forEach((e) => {
if (e.offsetHeight > height) {
e.style.maxHeight = height + 'px';
e.style.overflow = 'hidden';
const linkWrapper = document.createElement('div');
const link = document.createElement('a');
link.href = '';
link.textContent = 'more';
link.addEventListener('click', codeToggle);
linkWrapper.className = 'highlight-link';
linkWrapper.appendChild(link);
e.appendChild(linkWrapper);
}
});
}
function codeToggle(e) {
e.preventDefault();
const link = e.target;
const linkWrapper = link.parentElement.parentElement;
if (link.innerHTML === 'more') {
link.innerHTML = 'less';
linkWrapper.style.maxHeight = '';
linkWrapper.style.overflow = 'none';
link.parentElement.style.right = '0';
} else {
link.innerHTML = 'more';
linkWrapper.style.maxHeight = height + 'px';
linkWrapper.style.overflow = 'hidden';
link.parentElement.style.right = '0.5em';
scrollToTargetAdjusted(linkWrapper);
}
}
function scrollToTargetAdjusted(e) {
const header = document.querySelector('header');
const headerHeight = window.getComputedStyle(header, null).getPropertyValue('height');
const headerOffset = Number(headerHeight.replace('px', ''));
const bodyRect = document.body.getBoundingClientRect().top;
const elementRect = e.getBoundingClientRect().top;
const elementPosition = elementRect - bodyRect;
const offsetPosition = elementPosition - headerOffset;
window.scrollTo({
top: offsetPosition,
behavior: 'smooth'
});
} 这样就能自动折叠所有高度大于 |
等我理一理,然后加入主题中去 :) |
滚动的话看上下文有点费劲,显示面积太小了 |
可以配置高度 |
配置的高度是固定的,但代码块长度不是固定的,个人感觉折叠式的稍好点,当然如果可以折叠+滚动就更完美了 🤣 |
网页显示的代码是否可以默认折叠?
The text was updated successfully, but these errors were encountered: