惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - Franky
N
Netflix TechBlog - Medium
Google Online Security Blog
Google Online Security Blog
月光博客
月光博客
量子位
酷 壳 – CoolShell
酷 壳 – CoolShell
V
V2EX
腾讯CDC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
M
MIT News - Artificial intelligence
Vercel News
Vercel News
The GitHub Blog
The GitHub Blog
Hugging Face - Blog
Hugging Face - Blog
博客园 - 【当耐特】
Apple Machine Learning Research
Apple Machine Learning Research
aimingoo的专栏
aimingoo的专栏
博客园 - 三生石上(FineUI控件)
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
MongoDB | Blog
MongoDB | Blog
H
Help Net Security
The Cloudflare Blog
Blog — PlanetScale
Blog — PlanetScale
F
Full Disclosure
G
Google Developers Blog
罗磊的独立博客
Jina AI
Jina AI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Y
Y Combinator Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
J
Java Code Geeks
A
About on SuperTechFans
IT之家
IT之家
大猫的无限游戏
大猫的无限游戏
S
SegmentFault 最新的问题
有赞技术团队
有赞技术团队
GbyAI
GbyAI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
The Register - Security
The Register - Security
U
Unit 42
D
Docker
Martin Fowler
Martin Fowler
L
LINUX DO - 热门话题
NISL@THU
NISL@THU
阮一峰的网络日志
阮一峰的网络日志
C
Cybersecurity and Infrastructure Security Agency CISA
博客园_首页
Google DeepMind News
Google DeepMind News

空白Koobai

唠叨 - 2026-05-10 10:45 唠叨 - 2026-05-08 23:34 唠叨 - 2026-05-08 15:55 唠叨 - 2026-05-07 14:38 唠叨 - 2026-05-06 11:47 唠叨 - 2026-04-30 14:40 唠叨 - 2026-04-30 12:51 唠叨 - 2026-04-22 12:46 唠叨 - 2026-04-18 09:35 唠叨 - 2026-04-16 12:24 唠叨 - 2026-04-12 10:47 唠叨 - 2026-04-06 12:25 唠叨 - 2026-04-03 19:57 唠叨 - 2026-04-02 22:38 唠叨 - 2026-04-01 23:23 唠叨 - 2026-03-31 14:08 唠叨 - 2026-03-28 16:43 唠叨 - 2026-03-28 09:39 唠叨 - 2026-03-27 11:10 唠叨 - 2026-03-25 11:48 唠叨 - 2026-03-24 13:12 唠叨 - 2026-03-21 16:55 唠叨 - 2026-03-20 11:48 罢免小区业委会之后续 反角度之博客用户体验不能做的 N 件事 唠叨 - 2026-03-16 13:10 唠叨 - 2026-03-13 12:46 唠叨 - 2026-03-11 19:25 唠叨 - 2026-03-07 20:21 唠叨 - 2026-03-04 13:45 唠叨 - 2026-02-25 23:14 唠叨 - 2026-02-25 18:16 唠叨 - 2026-02-23 16:11 唠叨 - 2026-02-21 16:21 唠叨 - 2026-02-05 09:19 唠叨 - 2026-02-04 20:18 唠叨 - 2026-01-29 13:03 唠叨 - 2026-01-27 15:40 Hugo 通过 Github 自动推送到宝塔 (2026 年版备忘) 唠叨 - 2026-01-24 15:55 唠叨 - 2026-01-24 15:38 2025 关键词 唠叨 - 2025-06-22 22:07 唠叨 - 2025-06-10 17:31 唠叨 - 2025-05-14 22:04 唠叨 - 2025-04-16 22:06 唠叨 - 2025-04-09 10:28 唠叨 - 2025-04-07 22:09 唠叨 - 2025-03-22 11:30 唠叨 - 2025-01-29 23:14 唠叨 - 2025-01-21 19:30 唠叨 - 2025-01-10 19:39 唠叨 - 2025-01-09 13:57 罢免小区业委会 唠叨 - 2024-12-20 09:48 唠叨 - 2024-12-19 14:46 唠叨 - 2024-12-19 09:37 唠叨 - 2024-12-18 22:04 唠叨 - 2024-12-17 19:01 唠叨 - 2024-12-11 10:32 唠叨 - 2024-12-07 15:47 唠叨 - 2024-11-27 18:15 唠叨 - 2024-11-16 12:45 唠叨 - 2024-11-13 16:14 唠叨 - 2024-09-29 11:51 唠叨 - 2024-09-26 20:24 唠叨 - 2024-09-20 18:13 从 iOS 更换到 Android 会有不适吗? 唠叨 - 2024-09-17 11:21 唠叨 - 2024-09-14 20:10 唠叨 - 2024-09-04 20:51 唠叨 - 2024-09-02 20:17 唠叨 - 2024-08-08 14:19 唠叨 - 2024-08-07 16:45 唠叨 - 2024-07-27 23:21 唠叨 - 2024-07-09 23:37 唠叨 - 2024-06-15 10:43 唠叨 - 2024-06-01 23:03 唠叨 - 2024-05-31 10:06 五一假期之登高望远 以后如果小米做汽车了,也可以买小米汽车 唠叨 - 2024-03-25 14:05 唠叨 - 2024-03-23 15:26 唠叨 - 2024-03-20 21:06 自行车给自己带来了什么情绪价值爽点 唠叨 - 2024-02-28 15:44 唠叨 - 2024-02-21 21:00 唠叨 - 2024-02-18 19:38 唠叨 - 2024-02-17 16:24 这个春节有什么不一样 唠叨 - 2024-02-16 11:35 唠叨 - 2024-02-15 14:56 唠叨 - 2024-02-14 18:30 唠叨 - 2024-02-03 20:23 HUGO 外链跳转到中间页 唠叨 - 2024-01-31 20:20 折腾博客更像是自己的精神鸦片 唠叨 - 2024-01-29 16:21 唠叨 - 2024-01-27 10:54 唠叨 - 2024-01-24 16:16
HUGO 折腾随记之热力图 / 段落导航
2024-02-24 · via 空白Koobai

在闲逛各博客的时候,发现有些功能蛮有意思的,于是趁现在还没忙碌起来,跟着抄了下作业,同时通过 AI 的帮忙,优化了下以适应自己页面的风格。

博文热力图

热力图
热力图

最开始看到好像是因为 木木老师发了个memos,当时想着自己平常也不怎么更新博文,所以意义不大。这两天又在 椒盐豆豉中看到完整的教程,心想这次页面布局改成很窄的单栏之后,似乎很契合小而美的特性。一顿操作,跟原版的区别在于:

  1. 增加了支持暗黑模式。
  2. 删除了字数统计,及根据字数多少显示色块的深浅。
  3. 简化星期及月份的显示。
  4. 一周的开始,从周日改成周一。
  5. 在 Echarts 官网重新定制了 JS 功能,不然原版的有 1M 多。
<div id="heatmap" style="max-width: 700px;height: 110px;margin-bottom: 40px;"></div>
<script src="https://img.koobai.com/echarts.min.js"></script>
<script type="text/javascript">
  var chartDom = document.getElementById('heatmap');
  var myChart = echarts.init(chartDom);
  window.onresize = function() {
      myChart.resize();
  };
  var option;
  var dataMap = new Map();
  {{ range ((where .Site.RegularPages "Type" "posts")) }}
    var key = {{ .Date.Format "2006-01-02" }};
    var value = dataMap.get(key);
    var link = {{ .RelPermalink}};
    var title = {{ .Title }};
    
    // multiple posts in same day
    if (value == null) {
      dataMap.set(key, [{link, title}]);
    } else {
      value.push({link, title});
    }
  {{- end -}}

  var data = [];
  for (const [key, value] of dataMap.entries()) {
    data.push([key, value.length]);
  }

  var startDate = new Date();
  var year_Mill = startDate.setFullYear((startDate.getFullYear() - 1));
  var startDate = +new Date(year_Mill);
  var endDate = +new Date();

  startDate = echarts.format.formatTime('yyyy-MM-dd', startDate);
  endDate = echarts.format.formatTime('yyyy-MM-dd', endDate);
  
  // 检测浏览器主题模式并选择颜色方案
  var prefersDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;

  // 定义明亮模式下的颜色方案
  var lightTheme = {
      backgroundColor: '#FFFFFF',
      fangkuaicolor:'#F4F4F4',
      gaoliangcolor: ['#ffd0b6'],
      riqiColor: '#999',
      textbrcolor: '#FFF',
      xiankuangcolor:'rgba(0, 0, 0, 0.0)',
  };

  // 定义暗黑模式下的颜色方案
  var darkTheme = {
      backgroundColor: '#1A1718',
      fangkuaicolor:'#282325',
      gaoliangcolor: ['#b25f2f'],
      riqiColor: '#666',
      textbrcolor: '#332D2F',
      xiankuangcolor:'rgba(0, 0, 0, 0.0)',
  };

  // 根据浏览器主题模式选择当前主题
  var currentTheme = prefersDarkMode ? darkTheme : lightTheme;

  option = {
    tooltip: {
    hideDelay: 1000,
    enterable: true,
    backgroundColor: currentTheme.textbrcolor,
    borderWidth: 0, // 边框宽度为0
    formatter: function (p) {
        const date = p.data[0];
        const posts = dataMap.get(date);
        var content = `<span style="font-size: 0.75rem;font-family: var(--font-family-code);">${date}</span>`;
        for (const [i, post] of posts.entries()) {
            content += "<br>";          
            var link = post.link;
            var title = post.title;
            content += `<a href="${link}" target="_blank">${title}</a>` + '<br>';
        }
        return content;
        }
    },
    visualMap: {
        show: false,
        inRange: {   
          color: currentTheme.gaoliangcolor
        },
    },
    calendar: {
        left: 20,
        top:20,
        bottom:0,
        right: 0,
        cellSize: ['auto', 13],
        range: [startDate, endDate],
        itemStyle: {
            color: currentTheme.fangkuaicolor,
            borderWidth: 3.5,
            borderColor: currentTheme.backgroundColor, 
        },
        yearLabel: { show: false },
        monthLabel: { 
        nameMap: ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'],
        textStyle: {
            color: currentTheme.riqiColor,
        }
    },
        dayLabel: {
            firstDay: 1,
            nameMap: ['日', '一', '', '三', '', '五', ''],
            textStyle: {
                color: currentTheme.riqiColor
            }
        },
        splitLine: {
            lineStyle: {
                color: currentTheme.xiankuangcolor,
            }
        }
    },
    series: {
        type: 'heatmap',
        coordinateSystem: 'calendar',
        data: data,
    }
  };
  
  myChart.setOption(option);
  myChart.on('click', function(params) {
    if (params.componentType === 'series') {
      // open the first post on the day
      const post = dataMap.get(params.data[0])[0];
      const link = window.location.origin + post.link;
      window.open(link, '_blank').focus();
    }
});
</script>

详情页段落目录导航

段落导航
段落导航

这个功能早在去年重新折腾博客的时候就想增加,但当时因为不大会,搞得都不完美,所以也就不了了之。这几天在 atpX中,看到文章目录滚动高亮教程,于是又试着折腾了下。经过跟 AI 的配合,完美显示,不过过程还是碰到问题:

1. 显示位置

如果直接使用 position: fixed; 特性,目录位置只会根据浏览器窗口的大小来改变。这就会导致,浏览器窗口很大,主内容跟目录就会分得很开;浏览器窗口小,主内容跟目录有可能就会重叠。尝试了各种解决方法之后,决定在最上层的父元素中使用display: flex;。然后在目录的上一级再加入一个父元素div,完美解决。

<div class="single-nrong">
<div class="single-zw">主内容</div>
<div>
    <div class="paragraph-dh">文章段落目录导航</div>
</div>
</div>
<style>
.single-nrong {display: flex;}
.single-zw {width: 100%;}
.paragraph-dh {position: fixed;}
</style>

2. 无目录导航

 文章页如果没有目录导航,也会显示内容标题,这就看着很奇怪了。解决方法:  

  {{ $toc := .TableOfContents }}
  <!--判断<li>标签中有没有内容,如果有内容就显示,无内容就隐藏-->
  {{ if findRE "<li>" $toc 1 }} 
    <div class="paragraph-dh">
      <div class="sidebar-biaot">文章段落导航</div>
      {{ $toc }}
    </div>
  {{ end }}

3. 完整方案

特性:1. 默认隐藏,滑动到一定距离显示。2. 滑动到页面上方 1/4 处高亮显示当前目录。3. 点击目录导航平滑滚动到对应的标题。4. 无目录导航的文章页不显示。

  1. 新建 js 文件,如 single.js。
document.addEventListener("DOMContentLoaded", () => {
    const postTOC = document.querySelector('.paragraph-dh');

    const headingObserver = new IntersectionObserver(headings => {
        headings.forEach(({ target, isIntersecting }) => {
            const link = postTOC.querySelector(`a[href="#${target.id}"]`);
            if (isIntersecting && link) {
                postTOC.querySelectorAll('a').forEach(a => a.classList.remove('active'));
                link.classList.add('active');
            }
        });
    }, { rootMargin: '0px 0px -75%' });

    document.querySelectorAll('.content h2[id], .content h3[id]').forEach(heading => headingObserver.observe(heading));

    window.addEventListener('scroll', () => postTOC.style.opacity = (window.pageYOffset > 400) ? 1 : 0);

    postTOC.addEventListener('click', (e) => {
        e.preventDefault();
        const targetElement = document.getElementById(e.target.getAttribute('href').substring(1));
        if (targetElement) targetElement.scrollIntoView({ behavior: 'smooth' });
    });
});
  1. 在 single.html 文件合适的位置中添加:
<div class="single-nrong">
<div class="single-zw">
   文章内容
</div>
<div>
  {{ $toc := .TableOfContents }}
  {{ if findRE "<li>" $toc 1 }}
    <div class="paragraph-dh">
      <div class="sidebar-biaot">文章段落导航</div>
      {{ $toc }}
    </div>
  {{ end }}
</div>
</div>
<script src="/js/single.js"></script>
  1. 在 css 文件中添加:
.single-nrong{
  display: flex;
}
.single-zw{
  width: 100%;
}
.paragraph-dh {
   display: none;
  @include breakpoint('large') {
    display: block;
    position: fixed;
    opacity: 0;
    top: 70px;
    margin-left: 40px;
    transition: opacity 0.5s ease; 
    border-left: 4px solid #f0f0f0;
    padding: 0 15px;
    max-width: 300px;
  }
}
.paragraph-dh a{
  line-height: 1.4;
  color: #666;
}
.paragraph-dh ul{
  gap: 15px;
  display: grid;
}
.paragraph-dh ul ul {
  padding-inline-start: 1rem;
  margin-top: 10px;
}
.paragraph-dh a.active {
  color: var(--text-highlight-color);
  transition: all .25s ease-in-out;
}

结语

现在折腾博客是完全离不开各类 AI 协助了,让一个不懂技术的人也能瞎折腾。这几次折腾的时候发现 coze 非常好用,基本上一次就能给出正确答案。看来 ChatGPT 4.0 就是牛逼。