diff --git a/DYNAMICS.md b/DYNAMICS.md new file mode 100644 index 0000000..43eec4d --- /dev/null +++ b/DYNAMICS.md @@ -0,0 +1,221 @@ +# 动态(Moments) + +给 Fluid 主题加的一个「动态」页,用来发短评、生活记录和图片,不适合整理成长文的碎片内容都放在这里。 + +- 页面地址: +- 和文章一样:一个动态 = 一个 Markdown 文件 + Front-matter,按日期倒序排列 +- 支持完整的 Markdown 语法、主题的标签插件(如 `{% note %}`、`{% gi %}`)以及图片 +- 支持分页、置顶、标签、暗色模式、移动端自适应 +- 每条动态还会自动生成一个可分享的详情页(便于单独分享) + +![动态页效果预览](docs/dynamics-preview.jpg) + +--- + +## 一、新建动态 + +### 方式 1:命令行(推荐) + +```bash +# 标题默认作为文件名;也可以用 --slug 指定目录名 +hexo dynamics "今天的晚霞" +hexo dynamics "今天的晚霞" --slug 2026-11-14-sunset +``` + +会在 `source/dynamics//index.md` 生成一条动态,`` 默认取标题(中文标题会保留中文目录名)。 +生成的文件长这样: + +```markdown +--- +title: "今天的晚霞" +date: 2026-11-14 18:30:00 +tags: +--- + +``` + +标题会以 YAML 字符串写入,因此标题里带 `:`、`#` 等字符也不会破坏 Front-matter。 +编辑正文后执行 `hexo generate` 即可。 + +### 方式 2:手动创建文件 + +在 `source/dynamics/` 目录下新建 Markdown 文件,两种存放方式都支持: + +```text +source/dynamics/ +├── 2026-11-14-sunset.md # 方式 A:单文件 +└── 2026-11-12-reading/ # 方式 B:文件夹 + 配图(推荐放图片时使用) + ├── index.md + └── photo.jpg +``` + +两种方式的 Front-matter 完全一致: + +```yaml +--- +title: 今天的晚霞 # 可选,不写则详情页以日期作为标题(列表页不显示标题) +date: 2026-11-14 18:30:00 +tags: # 可选,仅作为动态的标签展示(不会进入 /tags/ 页面) + - 生活 + - 摄影 +top: true # 可选,置顶 +--- +``` + +> 注意:**不要**创建 `source/dynamics/index.md`,`/dynamics/` 列表页由主题自动生成,创建该文件会冲突。 + +--- + +## 二、与文章逻辑的一致性 + +| | 文章(Post) | 动态(Page) | +| --- | --- | --- | +| 文件格式 | Markdown + Front-matter | 相同 | +| 时间排序 | 按 `date` 倒序 | 相同 | +| 图片/附件 | 站内绝对路径或文章同名资源目录 | 站内绝对路径或动态同目录相对路径 | +| 正文渲染 | hexo-renderer-marked + 主题标签插件 | 相同 | +| 出现位置 | 首页 / 归档 / 标签 / 分类 / RSS | 只出现在 `/dynamics/` 列表和各自的详情页 | +| 索引 | 搜索、RSS、sitemap | 搜索、sitemap(不含 RSS) | + +动态存放在 `source/dynamics/` 而不是 `source/_posts/`,所以不会混进首页、归档、标签和 RSS; +但存储方式、Front-matter 写法、Markdown 渲染、图片资源处理都和文章保持一致。 + +--- + +## 三、图片与附件 + +### 站内绝对路径(和现有文章一样) + +```markdown +![晚霞](/img/dynamics/2026/sunset.webp) +``` + +把图片放在 `source/img/...` 下即可。 + +### 动态目录内的相对路径 + +图片和 `index.md` 放在同一目录,直接用相对路径引用: + +```text +source/dynamics/2026-11-12-reading/ +├── index.md +└── photo.jpg +``` + +```markdown +![书桌](photo.jpg) +``` + +主题在渲染前会把相对路径补全为 `/dynamics/2026-11-12-reading/photo.jpg`, +详情页和列表页都能正常显示,附件链接(如 `[PDF](file.pdf)`)同理。 +代码块(``` 或 ~~~ 围栏、4 空格缩进、`{% codeblock %}`)、行内代码和脚注语法不会被路径补全影响。 + +### 多图排版 + +可以使用 Fluid 自带的 `groupimage` 标签: + +```markdown +{% gi 2 %} +![](photo-1.jpg) +![](photo-2.jpg) +{% endgi %} +``` + +--- + +## 四、配置 + +配置位于 `_config.fluid.yml`(覆盖配置)中的 `dynamics` 段,主题自带默认值(`themes/fluid/_config.yml`): + +```yaml +dynamics: + enable: true # 是否启用动态列表页 + path: /dynamics/ # 列表页路径(详情页 URL 始终跟随文件所在目录,不受此项影响) + per_page: 10 # 每页条数,0 表示不分页 + description: '' # 页面顶部说明,支持 HTML + + banner_img: /img/index.jpg + banner_img_height: 50 + banner_mask_alpha: 0.3 + + note_banner_img: '' # 单条动态详情页的头图,留空用 banner_img + note_banner_img_height: 30 + + title: '动态' + subtitle: '一些短评和生活碎片' + avatar: '' # 留空使用 about.avatar + author: '' # 留空使用 about.name + + date_format: 'YYYY-MM-DD HH:mm' +``` + +导航菜单在 `_config.fluid.yml` 的 `navbar.menu` 中添加(已默认添加): + +```yaml +- { key: "dynamics", link: "/dynamics/", icon: "iconfont icon-clock-fill" } +``` + +关闭动态页:把 `dynamics.enable` 设为 `false`,列表页不再生成;动态文件本身仍可通过详情页访问,详情页的「返回动态」入口会自动隐藏。 + +--- + +## 五、实现文件 + +本功能是对本地 Fluid 主题的增强,涉及以下文件: + +新增: + +- `themes/fluid/layout/dynamics.ejs`:动态列表页布局 +- `themes/fluid/layout/dynamics-note.ejs`:单条动态详情页布局 +- `themes/fluid/layout/_partials/dynamics/note-card.ejs`:动态卡片 +- `themes/fluid/scripts/generators/dynamics.js`:列表页分页 generator + 动态详情页布局套用 +- `themes/fluid/scripts/filters/dynamics.js`:动态正文相对资源路径补全 +- `themes/fluid/scripts/helpers/dynamics.js`:列表页正文资源路径处理 +- `themes/fluid/scripts/console/dynamics.js`:`hexo dynamics` 命令 +- `themes/fluid/source/css/_pages/_dynamics/dynamics.styl`:动态页样式 +- `themes/fluid/source/js/dynamics.js`:相对时间显示 + +修改: + +- `themes/fluid/source/css/_pages/pages.styl`(引入动态页样式) +- `themes/fluid/languages/*.yml`(菜单、空状态、相对时间等文案) +- `themes/fluid/source/xml/local-search.xml`(顺带修复了页面搜索结果的链接) +- `themes/fluid/_config.yml`(动态页默认配置) +- `_config.fluid.yml`(动态页配置 + 导航菜单) + +示例内容: + +- `source/dynamics/hello-dynamics/index.md` +- `source/dynamics/first-cocoa-of-winter/index.md` +- `source/dynamics/2026-11-09-rainy-day.md` + +> 以后升级 Fluid 主题时,请保留上述新增/修改的文件;或者把 `themes/fluid` 中的这些文件合并到新主题里。 + +--- + +## 六、常见问题 + +**改了配置/样式后页面没变化?** + +```bash +hexo clean && hexo generate +``` + +**动态没出现在列表里?** + +- 确认文件在 `source/dynamics/` 目录下(不要在 `_posts/`) +- 确认 `dynamics.enable` 不是 `false` +- 确认没有创建 `source/dynamics/index.md` + +**图片显示 404?** + +- 用 `/img/...` 时确认文件在 `source/img/` 下 +- 用相对路径时确认图片与 `index.md` 放在同一目录,并且路径大小写一致(Linux 服务器区分大小写) + +**想调整列表每页数量?** + +修改 `dynamics.per_page`,然后 `hexo clean && hexo generate`。 + +**想改标题/副标题/头像?** + +修改 `dynamics.title`、`dynamics.subtitle`、`dynamics.avatar`、`dynamics.author`,留空时分别使用语言包、`about.avatar`、`about.name`。 diff --git a/README.md b/README.md index 7fd810e..bb31b45 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ # blog -mei的网络日志 \ No newline at end of file +mei的网络日志 + +- 动态页(Moments):,使用说明见 [DYNAMICS.md](DYNAMICS.md) \ No newline at end of file diff --git a/_config.fluid.yml b/_config.fluid.yml index d2ac63a..1805331 100644 --- a/_config.fluid.yml +++ b/_config.fluid.yml @@ -368,6 +368,7 @@ navbar: # Navigation bar menu. `key` is used to associate languages/*.yml. If there is no association, the value of `key` itself will be displayed; if `icon` is a css class, it can be omitted; adding `name` can force the display of the specified name menu: - { key: 'home', link: "/", icon: "iconfont icon-home-fill" } + - { key: "dynamics", link: "/dynamics/", icon: "iconfont icon-clock-fill" } - { key: '页面', icon: 'iconfont icon-program', @@ -984,7 +985,7 @@ about: banner_img: /img/index.jpg banner_img_height: 60 banner_mask_alpha: 0.3 - avatar: https://api.mmeiblog.cn/favicon.ico + avatar: /favicon.ico name: "mei" intro: "重度妄想症患者" # 更多图标可从 https://hexo.fluid-dev.com/docs/icon/ 查找,`class` 代表图标的 css class,添加 `qrcode` 后,图标不再是链接而是悬浮二维码 @@ -1139,6 +1140,55 @@ links: type: utterances +#--------------------------- +# 动态页 +# Moments Page +# +# 在 source/dynamics/ 目录下新建 Markdown 文件即可发布动态,文件格式与文章一致 +# (Front-matter + 正文),支持图片、代码等 Markdown 语法,也支持图片资源目录。 +# 也可以运行 `hexo dynamics "标题"` 快速新建一条动态。 +#--------------------------- +dynamics: + enable: true + + # 动态列表页路径 + # Path of the moments page + path: /dynamics/ + + # 每页显示的动态数量,设为 0 表示不分页 + # Number of moments per page, 0 means no pagination + per_page: 10 + + # 页面顶部说明文字,支持 HTML + # Intro text at the top of the page, HTML is supported + description: '' + + # 头图配置,留空则使用 page 页面的头图 + # Banner config, empty means using the banner of page + banner_img: /img/index.jpg + banner_img_height: 50 + banner_mask_alpha: 0.3 + + # 单条动态详情页的头图,留空则使用上面的 banner_img + # Banner of the single moment page, empty means using banner_img above + note_banner_img: '' + note_banner_img_height: 30 + + # 标题/副标题,为空则使用语言包中的默认值 + # Title and subtitle, empty means using the default value in language files + title: '动态' + subtitle: '一些短评和生活碎片' + + # 头像与作者名,为空则使用关于页的配置 + # Avatar and author name, empty means using the config of about page + avatar: '' + author: '' + + # 时间显示格式,参考 http://momentjs.cn/docs/#/displaying/format/ + # Date format, see http://momentjs.com/docs/#/displaying/format/ + date_format: 'YYYY-MM-DD HH:mm' + + #--------------------------- # 以下是配置 JS CSS 等静态资源的 URL 前缀,可以自定义成 CDN 地址, # 如果需要修改,最好使用与默认配置相同的版本,以避免潜在的问题, diff --git a/docs/dynamics-preview.jpg b/docs/dynamics-preview.jpg new file mode 100644 index 0000000..e9ef548 Binary files /dev/null and b/docs/dynamics-preview.jpg differ diff --git a/source/_drafts/the-future.md b/source/_drafts/the-future.md index 4d7e9bf..3e37574 100644 --- a/source/_drafts/the-future.md +++ b/source/_drafts/the-future.md @@ -7,7 +7,7 @@ categories: - 编程 index_img: /img/2024/ banner_img: /img/2024/ -permalink: /articles/2026/the-future.md +permalink: /articles/2026/the-future.html date: 2026-10-08 10:48:59 --- diff --git a/source/_drafts/where-human-from.md b/source/_drafts/where-human-from.md new file mode 100644 index 0000000..24d0d5f --- /dev/null +++ b/source/_drafts/where-human-from.md @@ -0,0 +1,14 @@ +--- +title: 智人从何而来 +tags: +- 科普 +- 智人 +- 人类 +- 历史 +categories: +- 金额普 +index_img: /img/2024/ +banner_img: /img/2024/ +permalink: /articles/2026/where-human-from.html +date: 2026-10-08 10:48:59 +--- \ No newline at end of file diff --git a/source/about/index.md b/source/about/index.md index 97f3eca..4efdad6 100644 --- a/source/about/index.md +++ b/source/about/index.md @@ -28,7 +28,7 @@ Hi! 这里是我的网络日志. * 我所描绘的一切或许并非真实,然而我的感受绝无虚假。 —— 《妄想症Paranoia》 * 大多数人到死都没有向尘世之外瞥一眼。 —— 刘慈欣《三体》 -* 想要逃避现实,最好的方法就是深深融入现实。 —— 刘慈欣《三体》 +* 想要逃避现实,最好的方法就是深深介入现实之中。 —— 刘慈欣《三体》 * 我们还有多久时间?没有人真正知道。如果智人的历史确实即将谢幕,我们这些最后一代的智人,或许该花点时间回答最后一个问题:我们究竟想要变成什么? —— 尤瓦尔·赫拉利《人类简史》 * 我们只是让某些局部变得更有秩序,把混乱转移到另一些领域。 —— 阮一峰《未来世界的幸存者》 * 有人说你可以不懂摄影,但一定要记录生活,你拍的不好没有关系,没有记录下来才是最可惜的一点,因为很多事情过后,已经不能凭空想象起来了 —— Tim diff --git a/source/dynamics/2026/ai-for-human.md b/source/dynamics/2026/ai-for-human.md new file mode 100644 index 0000000..a7822aa --- /dev/null +++ b/source/dynamics/2026/ai-for-human.md @@ -0,0 +1,10 @@ +--- +title: AI 似乎在让一切变得更糟 +date: 2026-11-08 22:51:23 +tags: + - AI + - 思考 +--- + +技术是为了服务人,我们都希望 AI 能让这一切变得更美好,而不是更糟。 +但就目前来看,AI 正在占据我们的生活并让他变得更糟。 diff --git a/themes/fluid/_config.yml b/themes/fluid/_config.yml index c8819e7..f7e999e 100644 --- a/themes/fluid/_config.yml +++ b/themes/fluid/_config.yml @@ -342,6 +342,7 @@ navbar: # Navigation bar menu. `key` is used to associate languages/*.yml. If there is no association, the value of `key` itself will be displayed; if `icon` is a css class, it can be omitted; adding `name` can force the display of the specified name menu: - { key: "home", link: "/", icon: "iconfont icon-home-fill" } + - { key: "dynamics", link: "/dynamics/", icon: "iconfont icon-clock-fill" } - { key: '页面', icon: 'iconfont icon-books', @@ -1075,6 +1076,31 @@ links: type: disqus +#--------------------------- +# 动态页 +# Moments Page +# +# 在 source/dynamics/ 目录下新建 Markdown 文件即可发布动态,文件格式与文章一致 +# (Front-matter + 正文),支持图片、代码等 Markdown 语法,也支持图片资源目录。 +# 也可以运行 `hexo dynamics "标题"` 快速新建一条动态。 +#--------------------------- +dynamics: + enable: true + path: /dynamics/ + per_page: 10 + description: '' + banner_img: /img/index.jpg + banner_img_height: 50 + banner_mask_alpha: 0.3 + note_banner_img: '' + note_banner_img_height: 30 + title: '' + subtitle: '' + avatar: '' + author: '' + date_format: 'YYYY-MM-DD HH:mm' + + #--------------------------- # 以下是配置 JS CSS 等静态资源的 URL 前缀,可以自定义成 CDN 地址, # 如果需要修改,最好使用与默认配置相同的版本,以避免潜在的问题, diff --git a/themes/fluid/languages/de.yml b/themes/fluid/languages/de.yml index 753801b..a1d7514 100644 --- a/themes/fluid/languages/de.yml +++ b/themes/fluid/languages/de.yml @@ -66,4 +66,19 @@ search: title: 'Suchen' keyword: 'Stichwort' +dynamics: + menu: 'Momente' + title: 'Momente' + total: 'Insgesamt %d Momente' + current_page: 'Seite %d' + empty: 'Noch keine Momente' + back: 'Zurück zu den Momenten' + pin: 'Angepinnt' + permalink: 'Permalink' + nav_label: 'Momente-Navigation' + relative_just_now: 'Gerade eben' + relative_minutes: 'vor {n} Minuten' + relative_hours: 'vor {n} Stunden' + relative_days: 'vor {n} Tagen' + noscript_warning: 'Blog funktioniert am besten mit aktiviertem JavaScript' diff --git a/themes/fluid/languages/en.yml b/themes/fluid/languages/en.yml index 20775b5..ab20767 100644 --- a/themes/fluid/languages/en.yml +++ b/themes/fluid/languages/en.yml @@ -66,4 +66,19 @@ search: title: 'Search' keyword: 'Keyword' +dynamics: + menu: 'Moments' + title: 'Moments' + total: 'Total %d moments' + current_page: 'Page %d' + empty: 'No moments yet' + back: 'Back to moments' + pin: 'Pinned' + permalink: 'Permalink' + nav_label: 'Moments navigation' + relative_just_now: 'Just now' + relative_minutes: '{n} minutes ago' + relative_hours: '{n} hours ago' + relative_days: '{n} days ago' + noscript_warning: 'Blog works best with JavaScript enabled' diff --git a/themes/fluid/languages/eo.yml b/themes/fluid/languages/eo.yml index 716fca6..16128d7 100644 --- a/themes/fluid/languages/eo.yml +++ b/themes/fluid/languages/eo.yml @@ -66,4 +66,19 @@ search: title: 'Serĉi' keyword: 'ŝlosivorto' +dynamics: + menu: 'Momentoj' + title: 'Momentoj' + total: 'Entute %d momentoj' + current_page: 'Paĝo %d' + empty: 'Ankoraŭ ne estas momentoj' + back: 'Reen al momentoj' + pin: 'Fiksita' + permalink: 'Konstanta ligilo' + nav_label: 'Momentoj-navigado' + relative_just_now: 'Ĝuste nun' + relative_minutes: 'antaŭ {n} minutoj' + relative_hours: 'antaŭ {n} horoj' + relative_days: 'antaŭ {n} tagoj' + noscript_warning: 'Blogo funkcias plej bone kun JavaScript ebligita' diff --git a/themes/fluid/languages/es.yml b/themes/fluid/languages/es.yml index 9aa4da9..668fa15 100644 --- a/themes/fluid/languages/es.yml +++ b/themes/fluid/languages/es.yml @@ -66,4 +66,19 @@ search: title: 'Buscar' keyword: 'Palabra clave' +dynamics: + menu: 'Momentos' + title: 'Momentos' + total: 'Total %d momentos' + current_page: 'Página %d' + empty: 'Aún no hay momentos' + back: 'Volver a los momentos' + pin: 'Fijado' + permalink: 'Enlace' + nav_label: 'Navegación de momentos' + relative_just_now: 'Justo ahora' + relative_minutes: 'hace {n} minutos' + relative_hours: 'hace {n} horas' + relative_days: 'hace {n} días' + noscript_warning: 'El blog funciona mejor con JavaScript habilitado' diff --git a/themes/fluid/languages/ja.yml b/themes/fluid/languages/ja.yml index 4584ce8..c691a2e 100644 --- a/themes/fluid/languages/ja.yml +++ b/themes/fluid/languages/ja.yml @@ -66,4 +66,19 @@ search: title: '検索' keyword: 'キーワード' +dynamics: + menu: 'つぶやき' + title: 'つぶやき' + total: '全 %d 件' + current_page: 'ページ %d' + empty: 'まだつぶやきがありません' + back: 'つぶやきに戻る' + pin: '固定' + permalink: 'リンク' + nav_label: 'つぶやきナビゲーション' + relative_just_now: 'たった今' + relative_minutes: '{n} 分前' + relative_hours: '{n} 時間前' + relative_days: '{n} 日前' + noscript_warning: 'ブログは JavaScript を有効にすると最適に機能します' diff --git a/themes/fluid/languages/ru.yml b/themes/fluid/languages/ru.yml index 11837ba..3c707fc 100644 --- a/themes/fluid/languages/ru.yml +++ b/themes/fluid/languages/ru.yml @@ -66,4 +66,19 @@ search: title: 'Поиск' keyword: 'Ключевое слово' +dynamics: + menu: 'Моменты' + title: 'Моменты' + total: 'Всего %d моментов' + current_page: 'Страница %d' + empty: 'Пока нет моментов' + back: 'Назад к моментам' + pin: 'Закреплено' + permalink: 'Ссылка' + nav_label: 'Навигация по моментам' + relative_just_now: 'Только что' + relative_minutes: '{n} мин назад' + relative_hours: '{n} ч назад' + relative_days: '{n} дн назад' + noscript_warning: 'Блог лучше всего работает с включенным JavaScript' diff --git a/themes/fluid/languages/zh-CN.yml b/themes/fluid/languages/zh-CN.yml index 1638021..2a592d8 100644 --- a/themes/fluid/languages/zh-CN.yml +++ b/themes/fluid/languages/zh-CN.yml @@ -66,4 +66,19 @@ search: title: '搜索' keyword: '关键词' +dynamics: + menu: '动态' + title: '动态' + total: '共 %d 条动态' + current_page: '第 %d 页' + empty: '还没有动态,去记录一下生活吧' + back: '返回动态' + pin: '置顶' + permalink: '动态链接' + nav_label: '动态导航' + relative_just_now: '刚刚' + relative_minutes: '{n} 分钟前' + relative_hours: '{n} 小时前' + relative_days: '{n} 天前' + noscript_warning: '博客在允许 JavaScript 运行的环境下浏览效果更佳' diff --git a/themes/fluid/languages/zh-HK.yml b/themes/fluid/languages/zh-HK.yml index a682f6d..16fab42 100644 --- a/themes/fluid/languages/zh-HK.yml +++ b/themes/fluid/languages/zh-HK.yml @@ -66,4 +66,19 @@ search: title: '搜尋' keyword: '關鍵字' +dynamics: + menu: '動態' + title: '動態' + total: '共 %d 條動態' + current_page: '第 %d 頁' + empty: '還沒有動態,去記錄一下生活吧' + back: '返回動態' + pin: '置頂' + permalink: '動態連結' + nav_label: '動態導覽' + relative_just_now: '剛剛' + relative_minutes: '{n} 分鐘前' + relative_hours: '{n} 小時前' + relative_days: '{n} 天前' + noscript_warning: '博客在允許 JavaScript 運行的環境下瀏覽效果更佳' diff --git a/themes/fluid/languages/zh-TW.yml b/themes/fluid/languages/zh-TW.yml index db8dd41..1a7cd91 100644 --- a/themes/fluid/languages/zh-TW.yml +++ b/themes/fluid/languages/zh-TW.yml @@ -66,4 +66,19 @@ search: title: '搜尋' keyword: '關鍵字' +dynamics: + menu: '動態' + title: '動態' + total: '共 %d 條動態' + current_page: '第 %d 頁' + empty: '還沒有動態,去記錄一下生活吧' + back: '返回動態' + pin: '置頂' + permalink: '動態連結' + nav_label: '動態導覽' + relative_just_now: '剛剛' + relative_minutes: '{n} 分鐘前' + relative_hours: '{n} 小時前' + relative_days: '{n} 天前' + noscript_warning: '博客在允許 JavaScript 運行的環境下瀏覽效果更佳' diff --git a/themes/fluid/layout/_partials/dynamics/note-card.ejs b/themes/fluid/layout/_partials/dynamics/note-card.ejs new file mode 100644 index 0000000..1206645 --- /dev/null +++ b/themes/fluid/layout/_partials/dynamics/note-card.ejs @@ -0,0 +1,71 @@ +<% +var note = note || page +var noteUrl = url_for(note.path) +var noteTitle = note.title || '' +var noteTags = Array.isArray(note.tags) ? note.tags : (note.tags ? [note.tags] : []) +var noteIsDetail = typeof isDetail === 'boolean' ? isDetail : false +var noteAuthor = String(author || theme.about.name || config.author || '') +var noteDateFormat = dateFormat || (theme.dynamics && theme.dynamics.date_format) || 'YYYY-MM-DD HH:mm' +%> + +
+
+ + + <% if (avatar) { %> + <%= noteAuthor %> + <% } %> + +
+ +
+
+ <%= noteAuthor %> + + <% if (note.top === true) { %> + + + <%- __('dynamics.pin') %> + + <% } %> + + + + <% if (!noteIsDetail) { %> + + + + <% } %> +
+ + <% if (noteTitle) { %> + <% if (noteIsDetail) { %> +

<%= noteTitle %>

+ <% } else { %> +

+ <%= noteTitle %> +

+ <% } %> + <% } %> + +
+ <%- noteIsDetail ? note.content : dynamics_content(note.content, note) %> +
+ + <% if (noteTags.length) { %> + + <% } %> +
+
diff --git a/themes/fluid/layout/dynamics-note.ejs b/themes/fluid/layout/dynamics-note.ejs new file mode 100644 index 0000000..7f72473 --- /dev/null +++ b/themes/fluid/layout/dynamics-note.ejs @@ -0,0 +1,35 @@ +<% +var dynamicsConfig = theme.dynamics || {} +var avatar = dynamicsConfig.avatar || theme.about.avatar +var author = dynamicsConfig.author || theme.about.name || config.author +var dateFormat = dynamicsConfig.date_format || 'YYYY-MM-DD HH:mm' +var dynamicsUrl = url_for(dynamicsConfig.path || '/dynamics/') +var dynamicsEnabled = dynamicsConfig.enable !== false + +page.title = page.title || date(page.date, 'YYYY-MM-DD HH:mm') +page.subtitle = page.subtitle || page.title +page.banner_img = page.banner_img || dynamicsConfig.note_banner_img || dynamicsConfig.banner_img +page.banner_img_height = page.banner_img_height || dynamicsConfig.note_banner_img_height +page.banner_mask_alpha = page.banner_mask_alpha || dynamicsConfig.banner_mask_alpha +%> + +
+ <% if (dynamicsEnabled) { %> + + <% } %> + + <%- partial('_partials/dynamics/note-card', { + note: page, + isDetail: true, + avatar: avatar ? url_for(avatar) : '', + author: author, + dateFormat: dateFormat + }) %> +
+ +<%- partial('_partials/markdown-plugins') %> diff --git a/themes/fluid/layout/dynamics.ejs b/themes/fluid/layout/dynamics.ejs new file mode 100644 index 0000000..45db861 --- /dev/null +++ b/themes/fluid/layout/dynamics.ejs @@ -0,0 +1,67 @@ +<% +var dynamicsConfig = theme.dynamics || {} +var notes = page.posts || [] +var totalNotes = typeof page.dynamics_total === 'number' ? page.dynamics_total : notes.length +var current = page.current || 1 +var avatar = dynamicsConfig.avatar || theme.about.avatar +var author = dynamicsConfig.author || theme.about.name || config.author +var dateFormat = dynamicsConfig.date_format || 'YYYY-MM-DD HH:mm' +var avatarUrl = avatar ? url_for(avatar) : '' + +page.title = page.title || dynamicsConfig.title || __('dynamics.title') +page.subtitle = page.subtitle || dynamicsConfig.subtitle || page.title +if (current > 1) { + page.subtitle += ' · ' + __('dynamics.current_page', current) +} +page.banner_img = page.banner_img || dynamicsConfig.banner_img +page.banner_img_height = page.banner_img_height || dynamicsConfig.banner_img_height +page.banner_mask_alpha = page.banner_mask_alpha || dynamicsConfig.banner_mask_alpha +page.anchorjs = false +%> + +
+ +
+ + + <%- __('dynamics.total', totalNotes) %> + + <% if (dynamicsConfig.description) { %> + <%- dynamicsConfig.description %> + <% } %> +
+ + <% if (!notes.length) { %> +
+ +

<%- __('dynamics.empty') %>

+
+ <% } else { %> +
+ <% var lastYear = null %> + <% notes.forEach(function(note) { %> + <% var noteYear = date(note.date, 'YYYY') %> + <% if (noteYear !== lastYear) { lastYear = noteYear %> +
<%= noteYear %>
+ <% } %> + <%- partial('_partials/dynamics/note-card', { + note: note, + isDetail: false, + avatar: avatarUrl, + author: author, + dateFormat: dateFormat + }) %> + <% }) %> +
+ + <%- partial('_partials/paginator') %> + <% } %> +
+ +<%- partial('_partials/markdown-plugins') %> + +<% import_js(theme.static_prefix.internal_js, 'dynamics.js', 'defer') %> diff --git a/themes/fluid/scripts/console/dynamics.js b/themes/fluid/scripts/console/dynamics.js new file mode 100644 index 0000000..976ddd6 --- /dev/null +++ b/themes/fluid/scripts/console/dynamics.js @@ -0,0 +1,74 @@ +/* global hexo */ + +'use strict'; + +const fs = require('fs'); +const path = require('path'); +const moment = require('moment'); +const { slugize } = require('hexo-util'); + +/** + * 新建一条动态 + * 用法:hexo dynamics "今天的晚霞" [--slug custom-slug] + * 生成:source/dynamics//index.md,该目录同时用于放置配图等资源 + */ +hexo.extend.console.register('dynamics', '新建一条动态(source/dynamics/)', { + arguments: [ + { name: 'title', desc: '动态标题,可以省略' } + ], + options: [ + { name: 'slug', desc: '自定义文件/目录名' } + ] +}, function(args) { + const title = (args._ || []).join(' ').replace(/[\r\n]+/g, ' ').trim(); + const now = moment(); + const timestamp = now.format('YYYYMMDD-HHmmss'); + + let slug = path.basename(String(args.slug || '')); + if (!slug && title) { + slug = slugize(title, { transform: this.config.filename_case }); + } + slug = slug + .replace(/[\\/:*?"<>|\s]+/g, '-') + .replace(/^\.+/, '') + .replace(/^-+|-+$/g, ''); + + if (!slug || slug === 'index') { + slug = title ? `${timestamp}-${slug}`.replace(/-+$/, '') : timestamp; + } + + const dynamicsDir = path.join(this.source_dir, 'dynamics'); + const noteDir = path.join(dynamicsDir, slug); + const noteFile = path.join(noteDir, 'index.md'); + + // 防止 --slug 通过 .. 等方式写到动态目录之外 + if (path.resolve(noteDir) !== path.join(path.resolve(dynamicsDir), slug)) { + this.log.error('非法的 slug:%s', slug); + process.exitCode = 1; + return; + } + + if (fs.existsSync(noteFile)) { + this.log.error('动态已存在:%s', path.relative(this.base_dir, noteFile)); + process.exitCode = 1; + return; + } + + // 用 JSON 字符串作为 YAML 标量,避免标题里的 : # 等字符破坏 Front-matter + const frontMatter = [ + '---', + `title: ${JSON.stringify(title || now.format('YYYY-MM-DD HH:mm'))}`, + `date: ${now.format('YYYY-MM-DD HH:mm:ss')}`, + 'tags:', + '---', + '', + '' + ].join('\n'); + + fs.mkdirSync(noteDir, { recursive: true }); + fs.writeFileSync(noteFile, frontMatter, 'utf8'); + + this.log.info('Created: %s', path.relative(this.base_dir, noteFile)); + this.log.info('把图片放在 %s 目录,正文里用 ![](图片名) 即可引用;然后运行 hexo generate', + path.relative(this.base_dir, noteDir)); +}); diff --git a/themes/fluid/scripts/filters/dynamics.js b/themes/fluid/scripts/filters/dynamics.js new file mode 100644 index 0000000..2a8cd67 --- /dev/null +++ b/themes/fluid/scripts/filters/dynamics.js @@ -0,0 +1,185 @@ +/* global hexo */ + +'use strict'; + +const path = require('path'); +const { url_for } = require('hexo-util'); + +const DYNAMICS_DIR = 'dynamics/'; +const ABSOLUTE_URL_RE = /^(?:[a-z][a-z0-9+.-]*:|\/\/|#|\/|\?)/i; +const FENCE_OPEN_RE = /^\s{0,3}(`{3,}|~{3,})/; +const INDENTED_CODE_RE = /^(?: {4,}|\t)/; +const TAG_CODE_OPEN_RE = /\{%\s*(codeblock|code|raw)\b/; +const TAG_CODE_CLOSE_RE = /\{%\s*end(?:codeblock|code|raw)\s*%\}/; +const CODE_BLOCK_OPEN = ''; +const CODE_BLOCK_CLOSE = ''; +const PH_OPEN = '\uE000DYNAMICS_PH_'; +const PH_CLOSE = '\uE001'; +const PLACEHOLDER_RE = /\uE000DYNAMICS_PH_(\d+)\uE001/g; + +const isDynamicSource = (source) => { + return typeof source === 'string' && source.indexOf(DYNAMICS_DIR) === 0; +}; + +/** + * 动态页面的资源路径基准:把 Windows 反斜杠规范化,并去掉文件名 / index.html, + * 最终得到形如 /dynamics/foo/ 的绝对 URL。 + */ +const getBaseUrl = (pagePath) => { + if (!pagePath) { + return ''; + } + let dir = String(pagePath).replace(/\\/g, '/'); + if (/index\.html?$/i.test(dir)) { + dir = dir.replace(/index\.html?$/i, ''); + } else { + dir = dir.replace(/[^/]*$/, ''); + } + if (!dir) { + return ''; + } + const url = url_for.call(hexo, dir); + return url.endsWith('/') ? url : `${url}/`; +}; + +const rewriteUrl = (url, base) => { + if (!url || ABSOLUTE_URL_RE.test(url)) { + return url; + } + const joined = `${base}${url.replace(/^\.\//, '')}`.replace(/\\/g, '/'); + return path.posix.normalize(joined); +}; + +const protect = (matched, protectedParts) => { + protectedParts.push(matched); + return `${PH_OPEN}${protectedParts.length - 1}${PH_CLOSE}`; +}; + +// 单行内的 markdown 链接 / 图片、引用式定义、内嵌 HTML 标签 +const rewriteLine = (line, base) => { + if (!line || (line.indexOf('](') === -1 && line.indexOf('<') === -1 + && !/^\s{0,3}\[[^\]\n]+\]:/.test(line))) { + return line; + } + + const protectedParts = []; + let result = line; + + // 保护行内代码、转义的 markdown 语法和脚注语法,避免误改 + result = result.replace(/(`+)[\s\S]*?\1/g, (match) => protect(match, protectedParts)); + // 转义的链接整体保留;转义的感叹号只保护自身,后面的链接目标仍然可以补全 + result = result.replace(/\\\[[^\]]*\]\([^)]*\)/g, (match) => protect(match, protectedParts)); + result = result.replace(/\\!/g, (match) => protect(match, protectedParts)); + result = result.replace(/\[\^\d+\]\([^)]*\)/g, (match) => protect(match, protectedParts)); + + // Markdown 图片与链接:![alt](image.png "title") / [text](file.zip) + result = result.replace(/(\]\()(<[^>\n]*>|[^)\s]+)/g, (match, prefix, target) => { + if (target.startsWith('<') && target.endsWith('>')) { + return `${prefix}<${rewriteUrl(target.slice(1, -1), base)}>`; + } + return prefix + rewriteUrl(target, base); + }); + + // 引用式链接定义:[id]: file.png(脚注定义 [^1]: 内容 不处理) + result = result.replace(/^(\s{0,3}\[(?!\^)[^\]\n]+\]:[ \t]*)(]+)(>?)/, + (match, prefix, open, target, close) => { + return prefix + open + rewriteUrl(target, base) + close; + }); + + // 正文里直接写的 HTML 标签 + result = result.replace(/(<(?:img|a|video|audio|source)\b[^>]*?\b(?:src|href)=)(["'])(.*?)\2/gi, + (match, prefix, quote, target) => `${prefix}${quote}${rewriteUrl(target, base)}${quote}`); + + return result.replace(PLACEHOLDER_RE, (match, index) => protectedParts[Number(index)] || ''); +}; + +/** + * 与文章一致:动态正文里的相对图片 / 附件路径按该动态详情页的地址补全, + * 这样动态既可以直接引用站内绝对路径(/img/xxx.png), + * 也可以把图片放在动态自己的目录里用相对路径引用(![](image.png))。 + * + * 注意:hexo-renderer-marked 默认会把相对图片地址加上站点根路径(prependRoot), + * 所以在 markdown 渲染之前先转换成绝对地址;代码块 / 行内代码 / 脚注语法保持原样。 + * 用 priority 9 保证在 Hexo 自带的 backtick_code_block 之前执行, + * 从而能按原始的 markdown 代码围栏跳过代码块。 + */ +hexo.extend.filter.register('before_post_render', function(data) { + if (!data || !isDynamicSource(data.source) || !data.content) { + return data; + } + + const base = getBaseUrl(data.path); + if (!base) { + return data; + } + + let fenceChar = ''; + let fenceLength = 0; + let inTagCode = false; + let inIndentedCode = false; + let inCodeBlock = false; + let prevBlank = true; + + data.content = data.content.split('\n').map((line) => { + const isBlank = line.trim() === ''; + let result = line; + + // 兼容其它 filter 已经处理成占位符的代码块(防御性判断) + if (inCodeBlock) { + if (line.indexOf(CODE_BLOCK_CLOSE) !== -1) { + inCodeBlock = false; + } + } + else if (line.indexOf(CODE_BLOCK_OPEN) !== -1) { + inCodeBlock = true; + } + else if (TAG_CODE_OPEN_RE.test(line)) { + if (!TAG_CODE_CLOSE_RE.test(line)) { + inTagCode = true; + } + } + else if (inTagCode) { + if (TAG_CODE_CLOSE_RE.test(line)) { + inTagCode = false; + } + } + else { + const fence = FENCE_OPEN_RE.exec(line); + if (fence) { + const run = fence[1]; + const char = run.charAt(0); + if (!fenceChar) { + fenceChar = char; + fenceLength = run.length; + } + else if (char === fenceChar && run.length >= fenceLength) { + fenceChar = ''; + fenceLength = 0; + } + } + else if (fenceChar) { + // 代码围栏内,保持原样 + } + else { + const isIndented = INDENTED_CODE_RE.test(line); + if (inIndentedCode) { + if (!isBlank && !isIndented) { + inIndentedCode = false; + } + } + else if (isIndented && prevBlank) { + // 4 空格 / Tab 缩进的代码块 + inIndentedCode = true; + } + if (!inIndentedCode) { + result = rewriteLine(line, base); + } + } + } + + prevBlank = isBlank; + return result; + }).join('\n'); + + return data; +}, 9); diff --git a/themes/fluid/scripts/generators/dynamics.js b/themes/fluid/scripts/generators/dynamics.js new file mode 100644 index 0000000..5aead41 --- /dev/null +++ b/themes/fluid/scripts/generators/dynamics.js @@ -0,0 +1,121 @@ +/* global hexo */ + +'use strict'; + +const pagination = require('hexo-pagination'); + +const DYNAMICS_DIR = 'dynamics'; +const DEFAULT_PER_PAGE = 10; + +/** + * 动态文件统一存放在 source/dynamics/ 目录下,和文章一样是 Markdown + Front-matter。 + * 支持扁平文件(source/dynamics/foo.md)和文件夹形式(source/dynamics/foo/index.md)。 + * 该目录下的所有 page 都会被识别为动态,列表页由本 generator 生成。 + */ +const isDynamicPage = (page) => { + return page + && typeof page.source === 'string' + && page.source.indexOf(`${DYNAMICS_DIR}/`) === 0 + && page.source !== `${DYNAMICS_DIR}/index.md`; +}; + +const compareNotes = (a, b) => { + // top: true 置顶,其余按时间倒序 + const topA = a.top === true ? 1 : 0; + const topB = b.top === true ? 1 : 0; + if (topA !== topB) { + return topB - topA; + } + const dateA = a.date ? new Date(a.date).getTime() : 0; + const dateB = b.date ? new Date(b.date).getTime() : 0; + return dateB - dateA; +}; + +const normalizeBase = (path) => { + let base = typeof path === 'string' ? path.trim() : ''; + base = base.replace(/^\/+/, '').replace(/\/+$/, ''); + if (!base) { + base = DYNAMICS_DIR; + } + return `${base}/`; +}; + +// 给 source/dynamics/ 下的页面套用动态详情页布局 +const originalPageGenerator = hexo.extend.generator.get('page'); + +if (originalPageGenerator) { + hexo.extend.generator.register('page', function(locals) { + return Promise.resolve(originalPageGenerator.call(this, locals)).then((routes) => { + if (!Array.isArray(routes)) { + return routes; + } + return routes.map((route) => { + if (route && route.layout && isDynamicPage(route.data)) { + const layouts = Array.isArray(route.layout) ? route.layout.slice() : [route.layout]; + if (layouts[0] !== 'dynamics-note') { + layouts.unshift('dynamics-note'); + } + route.layout = layouts; + } + return route; + }); + }); + }); +} + +hexo.extend.generator.register('_dynamics', function(locals) { + const config = this.theme.config.dynamics || {}; + if (config.enable === false) { + return; + } + + const allPages = typeof locals.pages.toArray === 'function' ? locals.pages.toArray() : []; + + if (allPages.some((page) => page.source === `${DYNAMICS_DIR}/index.md`)) { + this.log.warn( + '[Fluid] 检测到 source/dynamics/index.md,动态列表页由主题自动生成,请删除该文件,' + + '否则会与自动生成的 /dynamics/ 页面冲突' + ); + return; + } + + const notes = allPages.filter(isDynamicPage).sort(compareNotes); + const base = normalizeBase(config.path); + const paginationDir = this.config.pagination_dir || 'page'; + + let perPage = Number.parseInt(config.per_page, 10); + if (Number.isNaN(perPage)) { + perPage = DEFAULT_PER_PAGE; + } else if (perPage < 0) { + this.log.warn('[Fluid] dynamics.per_page 不能为负数,已回退为 %d', DEFAULT_PER_PAGE); + perPage = DEFAULT_PER_PAGE; + } + + const data = { + dynamics_total: notes.length + }; + + if (!notes.length) { + return [{ + path: base, + layout: 'dynamics', + data: Object.assign({ + posts: [], + base, + total: 1, + current: 1, + prev: 0, + prev_link: '', + next: 0, + next_link: '' + }, data) + }]; + } + + return pagination(base, notes, { + perPage, + layout: 'dynamics', + format: `${paginationDir}/%d/`, + data + }); +}); diff --git a/themes/fluid/scripts/helpers/dynamics.js b/themes/fluid/scripts/helpers/dynamics.js new file mode 100644 index 0000000..5a4a8b2 --- /dev/null +++ b/themes/fluid/scripts/helpers/dynamics.js @@ -0,0 +1,37 @@ +/* global hexo */ + +'use strict'; + +// 已经是绝对地址 / 协议地址 / 锚点 / 查询参数,不需要补全 +const ABSOLUTE_URL_RE = /^(?:[a-z][a-z0-9+.-]*:|\/\/|#|\/|\?)/i; + +const rewriteUrl = (value, base) => { + if (typeof value !== 'string' || !value || ABSOLUTE_URL_RE.test(value)) { + return value; + } + return base + value.replace(/^\.\//, ''); +}; + +/** + * 动态列表页会复用多个页面的正文,正文中的相对资源地址会以列表页为基准而失效, + * 这里把它们统一改写为相对于该动态详情页的绝对路径。 + */ +hexo.extend.helper.register('dynamics_content', function(content, page) { + if (!content || !page || !page.path) { + return content || ''; + } + + const pageUrl = this.url_for(page.path); + const base = pageUrl.substring(0, pageUrl.lastIndexOf('/') + 1); + if (!base) { + return content; + } + + return content + .replace(/(]*?\bsrc=")([^"]*)(")/gi, + (match, prefix, value, suffix) => prefix + rewriteUrl(value, base) + suffix) + .replace(/(<(?:video|audio|source)\b[^>]*?\bsrc=")([^"]*)(")/gi, + (match, prefix, value, suffix) => prefix + rewriteUrl(value, base) + suffix) + .replace(/(]*?\bhref=")([^"]*)(")/gi, + (match, prefix, value, suffix) => prefix + rewriteUrl(value, base) + suffix); +}); diff --git a/themes/fluid/source/css/_pages/_dynamics/dynamics.styl b/themes/fluid/source/css/_pages/_dynamics/dynamics.styl new file mode 100644 index 0000000..804efb5 --- /dev/null +++ b/themes/fluid/source/css/_pages/_dynamics/dynamics.styl @@ -0,0 +1,475 @@ +// -------------------------------------- +// Dynamics +// -------------------------------------- + +$dynamics-avatar-size = 3rem +$dynamics-avatar-gap = 1rem +$dynamics-avatar-size-mobile = 2.25rem +$dynamics-avatar-gap-mobile = .75rem +$dynamics-card-padding = 1rem 1.25rem +$dynamics-card-padding-mobile = .9rem 1rem + +.dynamics-page, .dynamics-note-page + padding-left 10% + padding-right 10% + +// 顶部概览 +// -------------------------------------- +.dynamics-summary + display flex + flex-wrap wrap + align-items center + justify-content space-between + margin-bottom 1.5rem + color var(--sec-text-color) + font-size .85rem + line-height 1.6 + transition color .2s ease-in-out + +.dynamics-summary-count + display inline-flex + align-items center + + i + margin-right .35rem + font-size .95rem + opacity .85 + +.dynamics-summary-desc + flex 1 1 auto + min-width 0 + margin-left 1rem + font-size .82rem + text-align right + opacity .9 + +// 年份分隔条 +// -------------------------------------- +.dynamics-feed + // 列表首条年份不需要额外的上间距 + & > .dynamics-year:first-child + margin-top 0 + +.dynamics-year + display flex + align-items center + margin-top 1.75rem + margin-bottom 1rem + margin-left ($dynamics-avatar-size + $dynamics-avatar-gap) + color var(--sec-text-color) + font-size .85rem + font-weight 500 + line-height 1.2 + letter-spacing .02em + font-variant-numeric tabular-nums + transition color .2s ease-in-out + + &::after + content "" + flex 1 + height 1px + margin-left .75rem + background-color var(--line-color) + transition background-color .2s ease-in-out + +// 动态条目 +// -------------------------------------- +.dynamics-item + display flex + padding-bottom 1.25rem + + &:last-of-type + padding-bottom 0 + +.dynamics-item-avatar + position relative + flex 0 0 $dynamics-avatar-size + margin-right $dynamics-avatar-gap + padding-top .25rem + + &::before + content "" + position absolute + top 0 + bottom 0 + left 50% + width 2px + margin-left -1px + border-radius 1px + background-color var(--line-color) + transition background-color .2s ease-in-out + +.dynamics-avatar-box + position relative + display block + width $dynamics-avatar-size + height $dynamics-avatar-size + border-radius 50% + background-color var(--body-bg-color) + box-shadow 0 0 0 1px var(--line-color), 0 0 0 3px var(--board-bg-color) + transition background-color .2s ease-in-out, box-shadow .2s ease-in-out + +.dynamics-avatar + position relative + display block + width 100% + height 100% + border-radius 50% + object-fit cover + background-color transparent + +.dynamics-avatar-img + position absolute + top 0 + left 0 + +.dynamics-avatar-text + display flex + align-items center + justify-content center + color var(--sec-text-color) + font-size 1.1rem + font-weight 600 + line-height 1 + user-select none + +.dynamics-item-main + flex 1 + min-width 0 + padding $dynamics-card-padding + background-color var(--body-bg-color) + border-radius .5rem + transition background-color .2s ease-in-out, box-shadow .2s ease-in-out + +.dynamics-feed .dynamics-item:hover .dynamics-item-main + box-shadow 0 4px 14px rgba(0, 0, 0, .06) + +// 卡片头部 +// -------------------------------------- +.dynamics-item-header + display flex + flex-wrap wrap + align-items center + margin-bottom .5rem + font-size .9rem + line-height 1.4 + +.dynamics-author + margin-right .5rem + color var(--text-color) + font-weight 600 + transition color .2s ease-in-out + +.dynamics-pin + display inline-flex + align-items center + margin-right .5rem + padding 0 .3rem + border-radius .25rem + background-color var(--button-bg-color) + color var(--sec-text-color) + font-size .7rem + line-height 1.6 + transition background-color .2s ease-in-out, color .2s ease-in-out + + i + margin-right .15rem + font-size .9em + +.dynamics-time + color var(--sec-text-color) + font-size .8rem + font-variant-numeric tabular-nums + transition color .2s ease-in-out + +.dynamics-permalink + flex none + margin-left auto + margin-right -.25rem + padding .15rem .25rem + color var(--sec-text-color) + font-size .9rem + line-height 1 + opacity .4 + transition opacity .2s ease-in-out, color .2s ease-in-out + + &:hover, &:focus + color var(--link-hover-color) + opacity 1 + text-decoration none + +// 标题 +// -------------------------------------- +.dynamics-item-title + margin 0 0 .5rem + color var(--post-heading-color) + font-size 1.05rem + font-weight 600 + line-height 1.45 + overflow-wrap break-word + transition color .2s ease-in-out + + a + color inherit + transition color .2s ease-in-out + + &:hover + color var(--link-hover-color) + text-decoration none + +// 正文 +// -------------------------------------- +.dynamics-item-content.markdown-body + margin-bottom 0 + color var(--post-text-color) + font-size .95rem + line-height 1.7 + overflow-wrap break-word + + & > *:first-child + margin-top 0 + + & > *:last-child + margin-bottom 0 + + p + margin .5rem 0 + + h1, h2, h3, h4, h5, h6 + margin .9rem 0 .5rem + line-height 1.45 + font-weight bold + + h1 + font-size 1.3rem + + h2 + font-size 1.2rem + + h3 + font-size 1.1rem + + h4 + font-size 1rem + + h5 + font-size .95rem + + h6 + font-size .9rem + + hr + margin 1rem 0 + + img + display block + max-width 100% + height auto + margin .75rem auto + border-radius 6px + box-shadow 0 2px 10px rgba(0, 0, 0, .08) + background-color transparent + + figcaption.image-caption + margin .25rem auto .75rem + font-size .8rem + + // 多图布局:避免撑破卡片 + .group-image-container + max-width 100% + margin .75rem auto + + .group-image-row + margin-bottom .5rem + + &:last-child + margin-bottom 0 + + .group-image-wrap + min-width 0 + + img + margin 0 auto + border-radius 6px + box-shadow 0 2px 10px rgba(0, 0, 0, .08) + +// 标签 +// -------------------------------------- +.dynamics-item-footer + display flex + flex-wrap wrap + align-items center + margin-top .75rem + +.dynamics-topic + display inline-flex + align-items center + margin .15rem .5rem 0 0 + padding .1rem .55rem + border-radius .75rem + background-color var(--button-bg-color) + color var(--sec-text-color) + font-size .76rem + line-height 1.5 + transition background-color .2s ease-in-out, color .2s ease-in-out + +// 空状态 +// -------------------------------------- +.dynamics-empty + padding 5rem 1rem + color var(--sec-text-color) + text-align center + transition color .2s ease-in-out + + i + display block + margin-bottom .75rem + font-size 2.5rem + opacity .5 + + p + margin 0 + font-size .95rem + line-height 1.6 + +// 详情页 +// -------------------------------------- +.dynamics-note-nav + display flex + align-items center + margin-bottom 1.25rem + +.dynamics-note-back + display inline-flex + align-items center + margin-left -.5rem + padding .25rem .5rem + border-radius .25rem + color var(--sec-text-color) + font-size .9rem + line-height 1.4 + transition color .2s ease-in-out, background-color .2s ease-in-out + + i + margin-right .35rem + font-size .9rem + transition transform .2s ease-in-out + + &:hover, &:focus + color var(--link-hover-color) + background-color var(--link-hover-bg-color) + text-decoration none + + i + transform translateX(-.15rem) + +.dynamics-item.dynamics-item--detail + display block + padding-bottom 0 + + .dynamics-item-avatar + display none + + .dynamics-item-main + padding 1.5rem 1.75rem + + .dynamics-item-header + margin-bottom .75rem + font-size .95rem + + .dynamics-author + font-size 1rem + + .dynamics-item-title + margin-bottom .75rem + font-size 1.3rem + line-height 1.4 + + .dynamics-item-content.markdown-body + font-size 1rem + line-height 1.75 + + p + margin .6rem 0 + + & > *:first-child + margin-top 0 + + & > *:last-child + margin-bottom 0 + +// 响应式 +// -------------------------------------- +@media (max-width: 767px) + .dynamics-page, .dynamics-note-page + padding-left 1.5rem + padding-right 1.5rem + + .dynamics-summary + margin-bottom 1.25rem + + .dynamics-summary-desc + flex-basis 100% + margin-top .15rem + margin-left 0 + text-align left + + .dynamics-year + margin-top 1.5rem + margin-left ($dynamics-avatar-size-mobile + $dynamics-avatar-gap-mobile) + + .dynamics-item + padding-bottom .9rem + + .dynamics-item-avatar + flex 0 0 $dynamics-avatar-size-mobile + margin-right $dynamics-avatar-gap-mobile + padding-top .15rem + + .dynamics-avatar-box + width $dynamics-avatar-size-mobile + height $dynamics-avatar-size-mobile + + .dynamics-avatar-text + font-size .95rem + + .dynamics-item-main + padding $dynamics-card-padding-mobile + + .dynamics-item-content.markdown-body + font-size .95rem + line-height 1.7 + + .dynamics-item.dynamics-item--detail + .dynamics-item-main + padding 1.25rem 1rem + + .dynamics-item-title + font-size 1.2rem + +@media (max-width: 424px) + .dynamics-page, .dynamics-note-page + padding-left 1rem + padding-right 1rem + + .dynamics-item-avatar + margin-right .6rem + + &::before + display none + + .dynamics-year + margin-left 2.85rem + + .dynamics-item-header + font-size .85rem + + .dynamics-time + font-size .78rem + + .dynamics-item-title + font-size 1rem + + .dynamics-item.dynamics-item--detail + .dynamics-item-title + font-size 1.15rem diff --git a/themes/fluid/source/css/_pages/pages.styl b/themes/fluid/source/css/_pages/pages.styl index a999597..4123a8f 100644 --- a/themes/fluid/source/css/_pages/pages.styl +++ b/themes/fluid/source/css/_pages/pages.styl @@ -6,3 +6,4 @@ @import "_category/*" @import "_tag/*" @import "_links/*" +@import "_dynamics/*" diff --git a/themes/fluid/source/js/dynamics.js b/themes/fluid/source/js/dynamics.js new file mode 100644 index 0000000..ddbe514 --- /dev/null +++ b/themes/fluid/source/js/dynamics.js @@ -0,0 +1,112 @@ +/* global window, document */ + +// 动态列表的相对时间渐进增强: +// 服务端始终渲染完整日期,脚本仅在数据合理时替换为更亲切的相对时间。 +(function(window, document) { + 'use strict'; + + var MINUTE = 60 * 1000; + var HOUR = 60 * MINUTE; + var DAY = 24 * HOUR; + var MONTH = 30 * DAY; + function parseTime(value) { + if (!value) { + return null; + } + + // 服务端优先输出带时区的 ISO 8601(date_xml),不同时区访客也能算对相对时间 + if (/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:?\d{2})$/.test(value)) { + var isoDate = new Date(value); + return isNaN(isoDate.getTime()) ? null : isoDate; + } + + // 兼容 "YYYY-MM-DD HH:mm:ss" 形式的本地时间(部分浏览器无法直接 Date.parse) + var matched = /^(\d{4})-(\d{2})-(\d{2})[ T](\d{2}):(\d{2})(?::(\d{2}))?/.exec(value); + if (matched) { + return new Date( + Number(matched[1]), + Number(matched[2]) - 1, + Number(matched[3]), + Number(matched[4]), + Number(matched[5]), + Number(matched[6] || 0) + ); + } + + var parsed = new Date(value); + return isNaN(parsed.getTime()) ? null : parsed; + } + + // 同时兼容 "{n} 分钟前" 与历史 "%d 分钟前" 两种占位符 + function fillPattern(pattern, amount) { + if (!pattern) { + return ''; + } + if (pattern.indexOf('{n}') >= 0) { + return pattern.replace('{n}', amount); + } + if (pattern.indexOf('%d') >= 0) { + return pattern.replace('%d', amount); + } + return pattern; + } + + function enhance() { + var page = document.querySelector('.dynamics-page'); + if (!page) { + return; + } + + var times = page.querySelectorAll('time[data-dynamics-time]'); + if (!times.length) { + return; + } + + var justNow = page.getAttribute('data-just-now') || ''; + var minutesAgo = page.getAttribute('data-minutes-ago') || ''; + var hoursAgo = page.getAttribute('data-hours-ago') || ''; + var daysAgo = page.getAttribute('data-days-ago') || ''; + + var now = Date.now(); + + for (var i = 0; i < times.length; i++) { + var el = times[i]; + var raw = el.getAttribute('data-dynamics-time') || el.getAttribute('datetime'); + var time = parseTime(raw); + if (!time) { + continue; + } + + var elapsed = now - time.getTime(); + if (elapsed < 0) { + elapsed = 0; + } + + // 超过 30 天保留服务端渲染的完整日期 + var label = ''; + if (elapsed < MINUTE) { + label = justNow; + } else if (elapsed < HOUR) { + label = fillPattern(minutesAgo, Math.floor(elapsed / MINUTE)); + } else if (elapsed < DAY) { + label = fillPattern(hoursAgo, Math.floor(elapsed / HOUR)); + } else if (elapsed < MONTH) { + label = fillPattern(daysAgo, Math.floor(elapsed / DAY)); + } + + if (label) { + el.textContent = label; + } + // 悬停时仍能看到完整的原始时间,datetime 属性保持不动 + if (raw && !el.getAttribute('title')) { + el.setAttribute('title', raw); + } + } + } + + if (document.readyState === 'loading') { + document.addEventListener('DOMContentLoaded', enhance); + } else { + enhance(); + } +})(window, document); diff --git a/themes/fluid/source/xml/local-search.xml b/themes/fluid/source/xml/local-search.xml index d7d0c01..e7eb0a4 100644 --- a/themes/fluid/source/xml/local-search.xml +++ b/themes/fluid/source/xml/local-search.xml @@ -30,11 +30,11 @@ {% endif %} {% if pages %} {% for page in pages.toArray() %} - {% if post.indexing == undefined or post.indexing %} + {% if page.indexing == undefined or page.indexing %} {{ page.title }} - - {{ [url, post.path] | urlJoin | uriencode }} + + {{ [url, page.path] | urlJoin | uriencode }} {% if content %} {% endif %}