1. 基础配置¶
步骤1¶
创建reading_time.py
reading_time.py
```python import re import threading import time from functools import lru_cache from collections import OrderedDict import hashlib
预编译正则表达式(性能优化版本)¶
⚡ AI摘要 (GLM)
本文介绍了如何为MkDocs文档添加阅读信息统计功能。首先,创建并配置reading_time.py
文件,然后将其放置在指定目录并修改配置文件。接着,展示效果,并介绍如何排除特定页面和自定义统计信息。
EXCLUDE_PATTERNS = [ re.compile(r'^index.md\('), re.compile(r'^trip/index\.md\)'), re.compile(r'^relax/index.md\('), re.compile(r'^blog/indexblog\.md\)'), re.compile(r'^blog/posts.md\('), re.compile(r'^develop/index\.md\)'), re.compile(r'waline.md\('), re.compile(r'link\.md\)'), re.compile(r'404.md$'), ]
高度优化的正则表达式(一次性编译)¶
CHINESE_CHARS_PATTERN = re.compile(r'[\u4e00-\u9fff\u3400-\u4dbf]')
CODE_BLOCK_PATTERN = re.compile(r'.*?
', re.DOTALL)
INLINE_CODE_PATTERN = re.compile(r'[^
]+`')
YAML_FRONT_PATTERN = re.compile(r'^---.?---\s', re.DOTALL)
HTML_TAG_PATTERN = re.compile(r'<[^>]+>')
IMAGE_PATTERN = re.compile(r'![.*?]\([^)]+\)')
LINK_PATTERN = re.compile(r'[([^]]+)]\([^)]+\)')
预定义排除类型¶
EXCLUDE_TYPES = frozenset({'landing', 'special', 'widget'})
扩展非编程行内代码词汇(更全面的过滤)¶
NON_CODE_WORDS = frozenset({ 'markdown', 'target', 'blank', 'lg', 'middle', 'small', 'large', 'left', 'right', 'center', 'top', 'bottom', 'primary', 'secondary', 'success', 'warning', 'danger', 'info', 'light', 'dark', 'grid', 'cards', 'octicons', 'bookmark', 'div', 'class', 'img', 'src', 'alt', 'width', 'height', 'style', 'id', 'data', 'href', 'title' })
支持的编程和标记语言(扩展版本)¶
PROGRAMMING_LANGUAGES = frozenset({ # 编程语言 'python', 'py', 'javascript', 'js', 'typescript', 'ts', 'java', 'cpp', 'c', 'go', 'rust', 'php', 'ruby', 'swift', 'kotlin', 'csharp', 'cs', # 脚本语言 'bash', 'sh', 'powershell', 'ps1', 'zsh', 'fish', 'bat', 'cmd', # 标记和配置语言 'html', 'css', 'scss', 'sass', 'less', 'yaml', 'yml', 'json', 'xml', 'toml', 'ini', 'conf', 'dockerfile', 'makefile', # 数据库和查询 'sql', 'mysql', 'postgresql', 'sqlite', 'mongodb', # 其他 'r', 'matlab', 'scala', 'perl', 'lua', 'dart', 'tex', 'latex', # 数据格式 'csv', 'properties', # 无标识符(空字符串也算作有效语言) '' })
@lru_cache(maxsize=256) def clean_markdown_content_for_chinese(content_hash, markdown): """清理Markdown内容,只保留中文文本用于统计(添加缓存)""" content = markdown
1 2 3 4 5 6 7 8 9 |
|
def count_code_lines(markdown): """统计代码行数(修复版本 - 正确处理所有代码行)""" code_blocks = CODE_BLOCK_PATTERN.findall(markdown) total_code_lines = 0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
|
def calculate_reading_stats(markdown): """计算中文字符数和代码行数""" # 生成内容哈希用于缓存 content_hash = hash(markdown)
1 2 3 4 5 6 7 8 9 10 11 |
|
def on_page_markdown(markdown, **kwargs): page = kwargs['page']
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
""" else: reading_info = f"""!!! info "📖 阅读信息" 阅读时间:{reading_time} 分钟 | 中文字符:{chinese_chars}
"""
1 |
|
```
步骤2¶
把reading_time.py放到docs/overrides/hooks目录下,然后在mkdocs.yml中添加:
步骤3¶
配置MkDocs主题以及覆写路径custom_dir
到这里检查下目录树状图:
步骤4¶
2. 效果展示¶
3.高级配置¶
3.1 排除特定页面¶
如果有一些页面不想统计阅读时间,可以在页面的元数据中添加 hide_reading_time: true
。例如:
或者直接在reading_time.py中添加:
3.2 自定义统计信息¶
如果需要自定义统计信息的格式,可以修改reading_time.py中的calculate_reading_stats函数。例如:
💬 评论
评论系统加载中...