Skip to content

Commit

Permalink
Update: jekyll
Browse files Browse the repository at this point in the history
  • Loading branch information
shlu committed Dec 23, 2024
1 parent b91034f commit b436ce5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 134 deletions.
8 changes: 7 additions & 1 deletion github/为Jekyll添加评论系统.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ e.classList.remove("giscus-frame--loading")});b=document.getElementById("giscus-
console.error("".concat(h(a)," No session is stored initially. ").concat("Please consider reporting this error at https://github.com/giscus/giscus/issues/new.")):a.includes("Discussion not found")?console.warn("[giscus] ".concat(a,". A new discussion will be created if a comment/reaction is submitted.")):a.includes("API rate limit exceeded")?console.warn(h(a)):console.error("".concat(h(a)," ").concat("Please consider reporting this error at https://github.com/giscus/giscus/issues/new.")))))})})();
```

补丁位置:支持url解码,否则评论里标题不显示中文
补丁:支持url解码,否则评论里标题不显示中文

![image-20241223151219375](./为Jekyll添加评论系统/image-20241223151219375.png)

![image-20241223151447138](./为Jekyll添加评论系统/image-20241223151447138.png)

补丁:去掉描述,优化web 链接显示

![image-20241223152556536](./为Jekyll添加评论系统/image-20241223152556536.png)

![image-20241223152611939](./为Jekyll添加评论系统/image-20241223152611939.png)

136 changes: 3 additions & 133 deletions github/将github仓库无侵入转换为github-pages.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,139 +14,9 @@ jekyll-gh-pages.yml

## 建立readme.md索引

```python
import os
import urllib.parse

def has_readme(directory):
"""检查目录中是否存在 README.md(不区分大小写)"""
for item in os.listdir(directory):
if item.lower() == 'readme.md':
return True
return False

def is_markdown_file(filename):
"""检查文件是否为 Markdown 文件(不区分大小写)"""
return filename.lower().endswith('.md')

def generate_markdown_index(base_path, current_path='.', level=0, exclude_dirs=None):
"""
递归生成 Markdown 目录索引,包含:
- 仅包含 README.md 的目录,链接指向目录本身。
- 其他 Markdown 文件,链接指向文件本身,链接文本为文件名(不含 .md)。
"""
if exclude_dirs is None:
exclude_dirs = ['.git', '.github', 'node_modules', '__pycache__']

markdown = ""
full_path = os.path.join(base_path, current_path)
try:
items = sorted(os.listdir(full_path))
except PermissionError:
return markdown

for item in items:
item_path = os.path.join(full_path, item)
relative_path = os.path.join(current_path, item).replace('\\', '/')
if os.path.isdir(item_path) and item not in exclude_dirs:
if has_readme(item_path):
indent = ' ' * level
# 确保目录路径以 '/' 结尾,并进行 URL 编码
dir_link = urllib.parse.quote(relative_path.rstrip('/') + '/')
markdown += f"{indent}- 📁 [{item}/]({dir_link})\n"
# 递归查找子目录
markdown += generate_markdown_index(base_path, relative_path, level + 1, exclude_dirs)
else:
indent = ' ' * level
# 不包含 README.md 的目录,仅显示目录名称,不包含链接
markdown += f"{indent}- 📁 {item}/\n"
# 递归查找子目录
markdown += generate_markdown_index(base_path, relative_path, level + 1, exclude_dirs)
elif os.path.isfile(item_path) and is_markdown_file(item) and item.lower() != 'readme.md':
indent = ' ' * level
# 文件名不包含后缀
file_name = os.path.splitext(item)[0]
# 进行 URL 编码
file_link = urllib.parse.quote(relative_path)
markdown += f"{indent}- 📄 [{file_name}]({file_link})\n"

return markdown

def main():
base_path = '.' # 仓库根目录
start_marker = '<!-- DIRECTORY INDEX START -->'
end_marker = '<!-- DIRECTORY INDEX END -->'

directory_index = generate_markdown_index(base_path)

directory_section = f"{start_marker}\n{directory_index}{end_marker}\n"

readme_path = 'README.md'

# 读取现有 README.md 内容
if os.path.exists(readme_path):
with open(readme_path, 'r', encoding='utf-8') as f:
readme_content = f.read()
else:
readme_content = ''

# 替换或添加目录索引部分
if start_marker in readme_content and end_marker in readme_content:
# 替换现有的目录索引
parts = readme_content.split(start_marker)
if len(parts) > 1:
before = parts[0]
after = parts[1].split(end_marker, 1)[1] if end_marker in parts[1] else ''
new_readme = before + directory_section + after
else:
new_readme = readme_content + "\n" + directory_section
else:
# 添加目录索引到 README.md
new_readme = readme_content + "\n" + directory_section

# 写回 README.md
with open(readme_path, 'w', encoding='utf-8') as f:
f.write(new_readme)

print("README.md 已更新目录索引。")

if __name__ == "__main__":
main()
通过py脚本遍历仓库下所有md文档,然后在主md里建立一个索引目录,脚本代码见:[generate_directory_index.py](https://github.com/xpko/xpko.github.io/blob/master/generate_directory_index.py)

```

github action:

```yaml
name: Update README Directory Index

on:
push:
branches:
- main # 根据你的默认分支名称调整

jobs:
update-readme:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'

- name: Run directory index script
run: python generate_directory_index.py

- name: Commit and Push changes
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: "🛠️ 自动更新目录索引"
file_pattern: "README.md"
```
对应的github action: [update-readme-index.yml](https://github.com/xpko/xpko.github.io/blob/master/.github/workflows/update-readme-index.yml)



Expand All @@ -158,7 +28,7 @@ jobs:

相关issue: [reason for blacklisting plugins?](https://github.com/github/pages-gem/issues/926)

​ ^ TODO: 该issue下给出了一个绕过白名单限制的解决方案,后面有时间可以研究下
^ TODO: 该issue下给出了一个绕过白名单限制的解决方案,后面有时间可以研究下

相关issue: [Links with titles not handled properly](https://github.com/github/pages-gem/issues/876)

Expand Down

0 comments on commit b436ce5

Please sign in to comment.