Skip to main content

A tool for checking references in Markdown files

Project description

Markdown Reference Checker

一个用于检查 Markdown 文件中引用完整性的工具,特别适用于 Obsidian 风格的 wiki 链接。

特性

  • 检查文件引用 ([[file]][[file|alias]])
  • 检查标题引用 ([[file#heading]])
  • 检查图片引用 (![[image]]![alt](image))
  • 检测未使用的图片
  • 检测单向链接(A引用B但B没有引用A)
  • 支持 .gitignore 和自定义忽略规则
  • 详细的错误报告(包含行号和列号)
  • 生成引用统计信息

安装

使用 pip 安装

pip install md-ref-checker

从源码安装

# 克隆仓库
git clone https://github.com/yourusername/md-ref-checker.git
cd md-ref-checker

# 创建并激活虚拟环境
python -m venv venv
source venv/bin/activate  # Linux/macOS
# 或
venv\Scripts\activate  # Windows

# 安装开发依赖
pip install -e ".[dev]"

使用方法

命令行工具

# 检查当前目录
md-ref-checker

# 检查指定目录
md-ref-checker -d /path/to/docs

# 显示详细信息
md-ref-checker -v 2

# 忽略特定文件
md-ref-checker -i "*.tmp" -i "draft/*"

# 删除未使用的图片
md-ref-checker -r

# 显示调试信息
md-ref-checker -D

命令行选项

  • -d, --dir: 要检查的目录路径(默认为当前目录)
  • -v, --verbosity: 输出详细程度(0-2)
    • 0: 只显示无效引用和未使用的图片
    • 1: 显示无效引用、未使用的图片和单向链接
    • 2: 显示所有引用统计信息
  • -n, --no-color: 禁用彩色输出
  • -i, --ignore: 添加要忽略的文件模式(可多次使用)
  • -r, --delete-unused-images: 删除未被引用的图片文件
  • -D, --debug: 显示调试信息

Python API

from md_ref_checker import ReferenceChecker

# 创建检查器
checker = ReferenceChecker("docs")

# 添加忽略规则
checker.fs.ignore_patterns.extend(["*.tmp", "draft/*"])

# 检查整个目录
result = checker.check_directory()

# 检查单个文件
result = checker.check_file("docs/note.md")

# 处理结果
if result.invalid_refs:
    print("发现无效引用:")
    for ref in result.invalid_refs:
        print(f"{ref.source_file}:{ref.line_number} - {ref.target}")

if result.unused_images:
    print("未使用的图片:")
    for image in result.unused_images:
        print(image)

if result.unidirectional_links:
    print("单向链接:")
    for source, target in result.unidirectional_links:
        print(f"{source} -> {target}")

开发

项目使用 pre-commit 钩子和 make 命令来简化开发流程。

初始化开发环境

# 安装所有依赖并设置 pre-commit 钩子
make install

常用命令

# 格式化代码
make format

# 运行所有代码检查
make lint

# 运行测试
make test

# 清理临时文件和缓存
make clean

运行测试

# 运行所有测试
pytest

# 运行特定测试
pytest tests/test_models.py

# 显示测试覆盖率
pytest --cov=md_ref_checker

代码质量工具

项目使用以下工具保证代码质量:

  • Black: 代码格式化
  • Ruff: 代码检查和导入排序
  • MyPy: 类型检查
  • pre-commit: Git 提交前自动运行检查

所有这些检查都会在提交代码时自动运行。你也可以手动运行它们:

# 手动运行 pre-commit 检查
pre-commit run --all-files

# 单独运行格式化
black src tests

# 单独运行代码检查
ruff check src tests

# 单独运行类型检查
mypy src tests

贡献

欢迎贡献!请参考以下步骤:

  1. Fork 项目
  2. 创建功能分支 (git checkout -b feature/amazing-feature)
  3. 提交更改 (git commit -m 'Add amazing feature')
  4. 推送到分支 (git push origin feature/amazing-feature)
  5. 创建 Pull Request

许可证

本项目采用 MIT 许可证 - 详见 LICENSE 文件。

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

md_ref_checker-0.1.0.tar.gz (17.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

md_ref_checker-0.1.0-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

Details for the file md_ref_checker-0.1.0.tar.gz.

File metadata

  • Download URL: md_ref_checker-0.1.0.tar.gz
  • Upload date:
  • Size: 17.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for md_ref_checker-0.1.0.tar.gz
Algorithm Hash digest
SHA256 17ee17a024baeb518081c5f3a9a6bc421bed03aef49b41caa34bfbca4c5318d8
MD5 46061c5c2db42d224969652c78a87584
BLAKE2b-256 233fe55631f8cc167e38c63b1020efc7a797a8ff24331dec549e60af95157552

See more details on using hashes here.

Provenance

The following attestation bundles were made for md_ref_checker-0.1.0.tar.gz:

Publisher: publish.yml on chess99/md-ref-checker

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file md_ref_checker-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: md_ref_checker-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for md_ref_checker-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 39752de6ed02fc96f23527178430a9548ba2c56a9e93d7909aeb89ddf68cd248
MD5 c07ad28461faa4d47754eb2667656754
BLAKE2b-256 b1184d32c9b9dfbae543a9e58f5198617710ec8b19990763597e499cdfb9ed08

See more details on using hashes here.

Provenance

The following attestation bundles were made for md_ref_checker-0.1.0-py3-none-any.whl:

Publisher: publish.yml on chess99/md-ref-checker

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page