Meekdai / Gmeek

Gmeek is a Blog All in Github
https://meekdai.github.io
MIT License
1.56k stars 223 forks source link

增加 `disableCamo` 配置项,以支持图片可使用创建文章(issue)时输入的原始链接。 #164

Closed ouxch closed 1 month ago

ouxch commented 1 month ago

增加 disableCamo 配置项,控制文章中的图片是否使用创建文章(issue)时输入的原始链接。 背景:

  1. Gmeek 使用了 GitHub API 将 markdown 渲染为 html;
  2. 而在这个过程 GitHub 会使用开源项目 Camo 将 markdown 中的图片进行托管;
  3. Camo 会为每幅图像生成开头为 https://camo.githubusercontent.com/ 的匿名 URL 代理;

Github 这么做的理由是:

  1. 代理这些图像将有助于保护用户的隐私:用户访问这些图片时,浏览器信息不会泄露给第三方服务;
  2. Github 通过 CDN 加速图像的访问;
  3. 有 CDN 可以减少因原始图片链接失效而无法正常渲染的情况;

然而如果你的图片本身托管在国内,GitHub 这么做反而会降低图片的访问速度,甚至导致无法访问(不能科学上网时)。 因此,增加一个单独的配置项,如果配置了 "disableCamo": 1,Gmeek 会在 GitHub 渲染网页后将图片链接简单替换为原始链接。

参考:

https://docs.github.com/zh/authentication/keeping-your-account-and-data-secure/about-anonymized-urls https://github.blog/news-insights/product-news/proxying-user-images/

Meekdai commented 1 month ago

这个其实可以通过Gmeek-html来实现。具体见如下链接 https://blog.meekdai.com/post/%E3%80%90Gmeek-jin-jie-%E3%80%91-wen-zhang-cha-ru-html-biao-qian.html

ouxch commented 1 month ago

这个其实可以通过Gmeek-html来实现。具体见如下链接 https://blog.meekdai.com/post/%E3%80%90Gmeek-jin-jie-%E3%80%91-wen-zhang-cha-ru-html-biao-qian.html

谢谢👌,学习了。可以任意加 HTML 标签的话确实方便,后面加视频用得上👍。 我暂时自己 fork 了一份跑 workflow,只是单纯解决这个图片问题的话,感觉通过配置项让框架来处理更灵活一些:

  1. 可以随时开关;
  2. 保持原生的 markdown 语法,不用修改 post;
  3. 网页和 issue 都能正常渲染;
Meekdai commented 1 month ago

通过markdown添加的图片,确实包含data-canonical-src图片的真正地址。这个只需要通过一小段js插件就能实现,而且不会误判在code中的内容被替换。

  1. 所有文章页实现替换Camo,在config.json中添加如下配置

    "script":"<script>document.querySelectorAll('a').forEach(anchor => {const img = anchor.querySelector('img');if (img && img.hasAttribute('data-canonical-src')) {const canonicalSrc = img.getAttribute('data-canonical-src');anchor.setAttribute('href', canonicalSrc);img.setAttribute('src', canonicalSrc);img.removeAttribute('data-canonical-src');}});</script>",
  2. 单篇文章页替换Camo,在文章页最后一行添加

    <!-- ##{"script":"<script>document.querySelectorAll('a').forEach(anchor => {const img = anchor.querySelector('img');if (img && img.hasAttribute('data-canonical-src')) {const canonicalSrc = img.getAttribute('data-canonical-src');anchor.setAttribute('href', canonicalSrc);img.setAttribute('src', canonicalSrc);img.removeAttribute('data-canonical-src');}});</script>"}## -->
ouxch commented 1 month ago

通过markdown添加的图片,确实包含data-canonical-src图片的真正地址。这个只需要通过一小段js插件就能实现,而且不会误判在code中的内容被替换。

  1. 所有文章页实现替换Camo,在config.json中添加如下配置
"script":"<script>document.querySelectorAll('a').forEach(anchor => {const img = anchor.querySelector('img');if (img && img.hasAttribute('data-canonical-src')) {const canonicalSrc = img.getAttribute('data-canonical-src');anchor.setAttribute('href', canonicalSrc);img.setAttribute('src', canonicalSrc);img.removeAttribute('data-canonical-src');}});</script>",
  1. 单篇文章页替换Camo,在文章页最后一行添加
<!-- ##{"script":"<script>document.querySelectorAll('a').forEach(anchor => {const img = anchor.querySelector('img');if (img && img.hasAttribute('data-canonical-src')) {const canonicalSrc = img.getAttribute('data-canonical-src');anchor.setAttribute('href', canonicalSrc);img.setAttribute('src', canonicalSrc);img.removeAttribute('data-canonical-src');}});</script>"}## -->

嗯嗯,能解决问题就行。正则确实可能误判,严谨点的话可以把 html 字符串解析一遍再做属性的处理。我想的是编译期能解决的尽量不要留到运行时,页面加载时执行的脚本能少一点是一点嘛,快速验证了一下这个思路发现可以。其实关键就是要知道 GitHub 有 Camo 重新托管图片的这个机制,解决的办法就多了。开始不知道有这个机制,直接添加附件/markdown标记/img标签都尝试了,图片链接一直是 github 的,还以为是没重新部署成功或者有缓存 bug 之类的,折腾了几遍就去搜了一下这个域名才定位到问题😂

tiengming commented 1 month ago

通过markdown添加的图片,确实包含data-canonical-src图片的真正地址。这个只需要通过一小段js插件就能实现,而且不会误判在code中的内容被替换。

  1. 所有文章页实现替换Camo,在config.json中添加如下配置

    "script":"<script>document.querySelectorAll('a').forEach(anchor => {const img = anchor.querySelector('img');if (img && img.hasAttribute('data-canonical-src')) {const canonicalSrc = img.getAttribute('data-canonical-src');anchor.setAttribute('href', canonicalSrc);img.setAttribute('src', canonicalSrc);img.removeAttribute('data-canonical-src');}});</script>",
  2. 单篇文章页替换Camo,在文章页最后一行添加

    <!-- ##{"script":"<script>document.querySelectorAll('a').forEach(anchor => {const img = anchor.querySelector('img');if (img && img.hasAttribute('data-canonical-src')) {const canonicalSrc = img.getAttribute('data-canonical-src');anchor.setAttribute('href', canonicalSrc);img.setAttribute('src', canonicalSrc);img.removeAttribute('data-canonical-src');}});</script>"}## -->

有个问题:尖括号里面多了个单引号

Meekdai commented 1 month ago

谢谢,已经修改

tiengming commented 1 month ago

谢谢,已经修改

看看我的新页面,咋样,目标是首页清晰明朗点,我之前用的自己的代码复制按钮,我看你后来增加复制功能了,不过之前看的时候好像还没有代码高亮?现在,目录按钮和返回顶部统一了css,看看要不要更新到你的仓库里。文章内图片增加了边框和阴影,悬停动画。

Meekdai commented 4 weeks ago

@tiengming 额,你确定你的首页改成下面这个图片这样了? image

代码复制按钮上一个版本新增的功能。

tiengming commented 4 weeks ago

@tiengming 额,你确定你的首页改成下面这个图片这样了? image

代码复制按钮上一个版本新增的功能。

等你把代码高亮的功能增加了,我就舍弃我的代码复制插件了。你的图片是我改的,主要是把标签和时间放在了文章标题的下一行

Meekdai commented 4 weeks ago

@tiengming 代码高亮不是一直都有的啊? https://blog.meekdai.com/post/markdown-ce-shi-ye-mian.html#%E4%BB%A3%E7%A0%81%E9%AB%98%E4%BA%AE

tiengming commented 4 weeks ago

@tiengming 代码高亮不是一直都有的啊? https://blog.meekdai.com/post/markdown-ce-shi-ye-mian.html#%E4%BB%A3%E7%A0%81%E9%AB%98%E4%BA%AE

好嘞,那我修改成你的代码插件