diff --git a/.github/CODE_OF_CONDUCT.md b/.github/CODE_OF_CONDUCT.md index 14a1c86..98dab73 100644 --- a/.github/CODE_OF_CONDUCT.md +++ b/.github/CODE_OF_CONDUCT.md @@ -1,6 +1,4 @@ -

- 简体中文 · English -

+English | [简体中文](/.github/CODE_OF_CONDUCT.zh_CN.md) # Contributor Covenant Code of Conduct @@ -71,7 +69,7 @@ All community leaders are obligated to respect the privacy and security of the reporter of any incident. Security vulnerabilities are not a Code of Conduct issue — please report them -privately via [`SECURITY.md`](SECURITY.md). +privately via [`SECURITY.md`](/.github/SECURITY.md). ## Enforcement Guidelines diff --git a/.github/CODE_OF_CONDUCT.zh_CN.md b/.github/CODE_OF_CONDUCT.zh_CN.md index 1fdcb95..eb6e8d4 100644 --- a/.github/CODE_OF_CONDUCT.zh_CN.md +++ b/.github/CODE_OF_CONDUCT.zh_CN.md @@ -1,6 +1,4 @@ -

- 简体中文 · English -

+[English](/.github/CODE_OF_CONDUCT.md) | 简体中文 # 贡献者公约行为准则 @@ -51,7 +49,7 @@ wiki 编辑、issue 及其他贡献,并在适当时说明处理原因。 所有社区维护者都有义务保护事件报告者的隐私与安全。 -安全漏洞不属于行为准则问题——请按 [`SECURITY.md`](SECURITY.md) 私下报告。 +安全漏洞不属于行为准则问题——请按 [`SECURITY.zh_CN.md`](/.github/SECURITY.zh_CN.md) 私下报告。 ## 执行指引 diff --git a/.github/CONTRIBUTING.md b/.github/CONTRIBUTING.md index 2bc625e..936be15 100644 --- a/.github/CONTRIBUTING.md +++ b/.github/CONTRIBUTING.md @@ -1,6 +1,4 @@ -

- 简体中文 · English -

+English | [简体中文](/.github/CONTRIBUTING.zh_CN.md) # Contributing @@ -8,17 +6,17 @@ Thank you for contributing to FoloToy AI Passport — code, documentation, firmware, and feedback. This repository is the development baseline for open-source wearable AI hardware designed for AI agents. It is often forked for second development; the fork conventions are in -[`docs/fork-guide.md`](../docs/fork-guide.md). +[`docs/fork-guide.md`](/docs/fork-guide.md). ## Before you start -- Read [`AGENTS.md`](../AGENTS.md): it is the authoritative entry and index for the +- Read [`AGENTS.md`](/AGENTS.md): it is the authoritative entry and index for the rules an AI agent should follow. It is not a replacement for this guide. -- Read [`README.md`](../docs/README.md) for the hardware capability contract, - and the [AI Hardware Development Guide](../docs/hardware-design/AI_HARDWARE_DEVELOPMENT_GUIDE.md) +- Read [`README.md`](/docs/README.md) for the hardware capability contract, + and the [AI Hardware Development Guide](/docs/hardware-design/AI_HARDWARE_DEVELOPMENT_GUIDE.md) for the complete hardware context. -- Follow [`CODE_OF_CONDUCT.md`](CODE_OF_CONDUCT.md) when participating in the - community. For ordinary usage questions, see [`SUPPORT.md`](SUPPORT.md). +- Follow [`CODE_OF_CONDUCT.md`](/.github/CODE_OF_CONDUCT.md) when participating in the + community. For ordinary usage questions, see [`SUPPORT.md`](/.github/SUPPORT.md). - Do not commit credentials, tokens, authorization files, or personal data. - The repository's `main` branch stays in sync with the upstream baseline; fork users develop feature work in `feature/*` branches (see `docs/fork-guide.md`). @@ -26,7 +24,7 @@ second development; the fork conventions are in ## Development and verification Use ESP-IDF 5.5.3. For a clean-machine setup, follow the -[environment bootstrap](../docs/development/engineering/environment-setup.md). +[environment bootstrap](/docs/development/engineering/environment-setup.md). Prefer the repository firmware gate for builds and flash its verified merged image at `0x0`. The direct IDF commands below are for incremental development. @@ -59,7 +57,7 @@ The repository provides one validation entry point for local development and CI: ``` Follow the verification requirements in -[`docs/development/build-and-test.md`](../docs/development/engineering/build-and-test.md): +[`docs/development/engineering/build-and-test.md`](/docs/development/engineering/build-and-test.md): a clean `idf.py build` is the minimum automated check, not hardware validation. Record build results and on-device results separately; never present a successful build as successful hardware validation. @@ -70,7 +68,7 @@ build as successful hardware validation. focused on one clear problem. 2. Use `(): ` for the pull request title, for example `feat(bsp): ...`, `docs: ...`. Available types are defined in - [`docs/contribution/commit-and-pr.md`](../docs/contribution/commit-and-pr.md). + [`docs/contribution/commit-and-pr.md`](/docs/contribution/commit-and-pr.md). 3. Review the complete diff and confirm that it contains no credentials, unrelated generated files, or unintended changes. 4. Follow the PR requirements in `docs/contribution/commit-and-pr.md`: state the @@ -86,11 +84,11 @@ scope and compatibility. ## Licensing of contributions -This repository is licensed under [MIT](../LICENSE). By contributing, you agree that +This repository is licensed under [MIT](/LICENSE). By contributing, you agree that your contribution is submitted under the MIT license terms of the repository. ## Security issues Do not disclose vulnerabilities, credentials, or exploitable details in public issues, pull requests, or discussions. Follow the private reporting process in -[`SECURITY.md`](SECURITY.md). +[`SECURITY.md`](/.github/SECURITY.md). diff --git a/.github/CONTRIBUTING.zh_CN.md b/.github/CONTRIBUTING.zh_CN.md index 7816175..d6e26df 100644 --- a/.github/CONTRIBUTING.zh_CN.md +++ b/.github/CONTRIBUTING.zh_CN.md @@ -1,26 +1,24 @@ -

- 简体中文 · English -

+[English](/.github/CONTRIBUTING.md) | 简体中文 # 贡献指南 感谢你为 FoloToy AI Passport 贡献代码、文档、固件和反馈。本仓库是面向 AI agent 的 开源可穿戴 AI 硬件的开发基线。它常被 fork 后二次开发,fork 用户约定见 -[`docs/fork-guide.zh_CN.md`](../docs/fork-guide.zh_CN.md)。 +[`docs/fork-guide.zh_CN.md`](/docs/fork-guide.zh_CN.md)。 ## 开始之前 -- 先读 [`AGENTS.zh_CN.md`](../AGENTS.zh_CN.md):它是 AI agent 应遵循规则的权威入口与索引,不是本指南的替代品。 -- 先读 [`README.zh_CN.md`](../docs/README.zh_CN.md) 的硬件能力契约,以及 - [AI 硬件开发指南](../docs/hardware-design/AI_HARDWARE_DEVELOPMENT_GUIDE.zh_CN.md) 的完整硬件上下文。 -- 参与社区时请遵守 [`CODE_OF_CONDUCT.md`](CODE_OF_CONDUCT.md);普通使用问题见 [`SUPPORT.md`](SUPPORT.md)。 +- 先读 [`AGENTS.zh_CN.md`](/AGENTS.zh_CN.md):它是 AI agent 应遵循规则的权威入口与索引,不是本指南的替代品。 +- 先读 [`README.zh_CN.md`](/docs/README.zh_CN.md) 的硬件能力契约,以及 + [AI 硬件开发指南](/docs/hardware-design/AI_HARDWARE_DEVELOPMENT_GUIDE.zh_CN.md) 的完整硬件上下文。 +- 参与社区时请遵守 [`CODE_OF_CONDUCT.zh_CN.md`](/.github/CODE_OF_CONDUCT.zh_CN.md);普通使用问题见 [`SUPPORT.zh_CN.md`](/.github/SUPPORT.zh_CN.md)。 - 不要提交凭证、令牌、授权文件或个人数据。 - 仓库的 `main` 分支始终与上游基线保持同步;fork 用户在 `feature/*` 分支开发功能(见 `docs/fork-guide.md`)。 ## 开发与验证 使用 ESP-IDF 5.5.3。全新机器先按 -[环境引导](../docs/development/engineering/environment-setup.zh_CN.md)完成安装。 +[环境引导](/docs/development/engineering/environment-setup.zh_CN.md)完成安装。 编译优先运行仓库固件门禁,烧录优先把验证过的合并镜像写入 `0x0`。以下直接 IDF 命令只用于增量开发。 @@ -52,7 +50,7 @@ cc -std=c11 -Wall -Wextra -Werror -Imain \ ./tools/validate.sh # 完整门禁 ``` -遵守 [`docs/development/build-and-test.zh_CN.md`](../docs/development/engineering/build-and-test.zh_CN.md) 的 +遵守 [`docs/development/engineering/build-and-test.zh_CN.md`](/docs/development/engineering/build-and-test.zh_CN.md) 的 验证要求:干净的 `idf.py build` 是最低自动检查,不是硬件验收;构建结果与真机结果 分开记录,禁止把编译通过描述成硬件验证通过。 @@ -60,7 +58,7 @@ cc -std=c11 -Wall -Wextra -Werror -Imain \ 1. 从 `main` 创建短生命周期 `feature/*` 分支,保持一个 PR 只解决一个清晰的问题。 2. PR 标题使用 `(): <简短描述>`,例如 `feat(bsp): ...`、`docs: ...`。 - 可用 type 见 [`docs/contribution/commit-and-pr.zh_CN.md`](../docs/contribution/commit-and-pr.zh_CN.md)。 + 可用 type 见 [`docs/contribution/commit-and-pr.zh_CN.md`](/docs/contribution/commit-and-pr.zh_CN.md)。 3. Review 完整 diff,确认没有凭证、无关生成文件或意外改动。 4. 遵守 `docs/contribution/commit-and-pr.md` 的 PR 要求:说明测试的硬件/版本、 行为变更摘要、构建与真机结果,链接相关 issue,并对引脚、显示旋转、codec 时钟、 @@ -72,9 +70,9 @@ cc -std=c11 -Wall -Wextra -Werror -Imain \ ## 贡献的许可 -本仓库使用 [MIT](../LICENSE) 许可证。提交贡献即表示你同意按本仓库的 MIT 条款并入你的贡献。 +本仓库使用 [MIT](/LICENSE) 许可证。提交贡献即表示你同意按本仓库的 MIT 条款并入你的贡献。 ## 安全漏洞 不要在公开 issue、Pull Request 或讨论中披露漏洞、凭证或可利用细节。请按 -[`SECURITY.md`](SECURITY.md) 的私密报告流程处理。 +[`SECURITY.zh_CN.md`](/.github/SECURITY.zh_CN.md) 的私密报告流程处理。 diff --git a/.github/SECURITY.md b/.github/SECURITY.md index 8025a26..3e77991 100644 --- a/.github/SECURITY.md +++ b/.github/SECURITY.md @@ -1,6 +1,4 @@ -

- 简体中文 · English -

+English | [简体中文](/.github/SECURITY.zh_CN.md) # Security Policy diff --git a/.github/SECURITY.zh_CN.md b/.github/SECURITY.zh_CN.md index 2d32ab1..8888f55 100644 --- a/.github/SECURITY.zh_CN.md +++ b/.github/SECURITY.zh_CN.md @@ -1,6 +1,4 @@ -

- 简体中文 · English -

+[English](/.github/SECURITY.md) | 简体中文 # 安全漏洞报告 diff --git a/.github/SUPPORT.md b/.github/SUPPORT.md index 043b263..fb54622 100644 --- a/.github/SUPPORT.md +++ b/.github/SUPPORT.md @@ -1,6 +1,4 @@ -

- 简体中文 · English -

+English | [简体中文](/.github/SUPPORT.zh_CN.md) # Support @@ -12,12 +10,12 @@ guaranteed. ## Where to start - Hardware facts, wiring, and known traps: read - [`README.md`](../docs/README.md) and the - [AI Hardware Development Guide](../docs/hardware-design/AI_HARDWARE_DEVELOPMENT_GUIDE.md). + [`README.md`](/docs/README.md) and the + [AI Hardware Development Guide](/docs/hardware-design/AI_HARDWARE_DEVELOPMENT_GUIDE.md). - AI agent workflow and engineering rules: read - [`AGENTS.md`](../AGENTS.md) and its rule index. + [`AGENTS.md`](/AGENTS.md) and its rule index. - Security vulnerabilities, credentials, or exploitable details: follow - [`SECURITY.md`](SECURITY.md) and report privately. + [`SECURITY.md`](/.github/SECURITY.md) and report privately. - Ordinary bugs: open a GitHub issue with the board revision, firmware version, and reproduction steps. - Feature requests: use the feature request template and describe the use case diff --git a/.github/SUPPORT.zh_CN.md b/.github/SUPPORT.zh_CN.md index 74b62cf..e80a6a7 100644 --- a/.github/SUPPORT.zh_CN.md +++ b/.github/SUPPORT.zh_CN.md @@ -1,6 +1,4 @@ -

- 简体中文 · English -

+[English](/.github/SUPPORT.md) | 简体中文 # 使用支持 @@ -9,10 +7,10 @@ FoloToy AI Passport 是开源硬件与固件。社区可以帮助确认可复现 ## 先查什么 -- 硬件事实、接线和已知陷阱:先看 [`README.zh_CN.md`](../docs/README.zh_CN.md) 和 - [AI 硬件开发指南](../docs/hardware-design/AI_HARDWARE_DEVELOPMENT_GUIDE.zh_CN.md)。 -- AI agent 工作流和工程规则:先看 [`AGENTS.zh_CN.md`](../AGENTS.zh_CN.md) 及其规则索引。 -- 安全漏洞、凭证或可利用细节:按照 [`SECURITY.md`](SECURITY.md) 私下报告。 +- 硬件事实、接线和已知陷阱:先看 [`README.zh_CN.md`](/docs/README.zh_CN.md) 和 + [AI 硬件开发指南](/docs/hardware-design/AI_HARDWARE_DEVELOPMENT_GUIDE.zh_CN.md)。 +- AI agent 工作流和工程规则:先看 [`AGENTS.zh_CN.md`](/AGENTS.zh_CN.md) 及其规则索引。 +- 安全漏洞、凭证或可利用细节:按照 [`SECURITY.zh_CN.md`](/.github/SECURITY.zh_CN.md) 私下报告。 - 普通 Bug:提交 GitHub issue,并提供板卡版本、固件版本和复现步骤。 - 功能建议:提交 feature request,说明使用场景和期望结果。 - 使用问题:用「Usage question / 使用问题」issue 模板提问。 diff --git a/docs/contribution/doc-conventions.md b/docs/contribution/doc-conventions.md index c8b63dd..a333646 100644 --- a/docs/contribution/doc-conventions.md +++ b/docs/contribution/doc-conventions.md @@ -15,6 +15,21 @@ These rules apply equally to human contributors and AI agents. Documentation is - Code, commands, paths, URLs, identifiers, and data fields remain unchanged between translations where appropriate. - The repository check rejects an unpaired document, a missing language switch, or CJK prose in an English default file. +### GitHub community-document links + +GitHub also renders `.github/CONTRIBUTING`, `CODE_OF_CONDUCT`, `SECURITY`, and +`SUPPORT` documents outside their file view. In both language versions, put a +Markdown language switch on the first nonempty line, outside HTML blocks, with +a repository-root path, for example +`[简体中文](/.github/CODE_OF_CONDUCT.zh_CN.md)`, and use repository-root paths for +other internal links, such as `/docs/README.md`. Bare filenames in HTML language +switches can lose the `.github/` directory in the repository overview. Do not +hardcode the upstream owner or `main` into these document links: root-relative +links keep the current repository and branch. See GitHub's +[relative-link rules](https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#relative-links). +The repository gate checks these community-document links; after deployment, +also click both language directions from the overview and individual file views. + ## Vendored third-party documentation Preserve upstream documentation when copying a third-party component into the diff --git a/docs/contribution/doc-conventions.zh_CN.md b/docs/contribution/doc-conventions.zh_CN.md index 1fd7d8d..8546488 100644 --- a/docs/contribution/doc-conventions.zh_CN.md +++ b/docs/contribution/doc-conventions.zh_CN.md @@ -14,6 +14,18 @@ - 新增、移动或删除任一语言文件时,必须同步处理配对文件和所有索引。 - `tools/check_repo.py` 与 CI 会拒绝缺少配对文件、缺少切换链接或英文默认页包含中文正文的变更。 +### GitHub 社区文档链接 + +GitHub 还会在文件页面之外展示 `.github/` 中的 `CONTRIBUTING`、`CODE_OF_CONDUCT`、 +`SECURITY` 和 `SUPPORT` 文档。两种语言的切换均须放在第一个非空行、HTML 块之外, +使用仓库根路径的 Markdown 链接,例如 `[简体中文](/.github/CODE_OF_CONDUCT.zh_CN.md)`;其他仓库内链接也使用 +根路径,例如 `/docs/README.md`。HTML 语言切换中的裸文件名在仓库首页可能丢失 +`.github/` 目录。不要把上游仓库所有者或 `main` 写死在这些文档链接中,根相对 +链接会保留当前仓库与分支。参见 GitHub 的 +[相对链接规则](https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#relative-links)。 +仓库门禁会检查这些社区文档链接;上线后还需从首页和单文件页面实际点击中英文 +双向切换。 + ## 随仓库引入的第三方文档 把第三方组件复制到仓库时,应保留其上游原始文档。如需豁免其中 Markdown diff --git a/tests/test_check_repo.py b/tests/test_check_repo.py index fb70691..9d5482a 100644 --- a/tests/test_check_repo.py +++ b/tests/test_check_repo.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -"""Host tests for bounded third-party documentation exemptions.""" +"""Host tests for documentation exemptions and community-document navigation.""" from __future__ import annotations @@ -179,5 +179,163 @@ class VendoredDocumentationTest(unittest.TestCase): self.assertTrue(any(expected in error for error in errors), errors) +class CommunityDocumentLinksTest(unittest.TestCase): + def setUp(self) -> None: + temporary = tempfile.TemporaryDirectory(prefix="ai-passport-community-doc-tests-") + self.addCleanup(temporary.cleanup) + self.root = Path(temporary.name).resolve() + root_patch = patch.object(CHECKS, "ROOT", self.root) + root_patch.start() + self.addCleanup(root_patch.stop) + + def document(self, name: str, content: str) -> Path: + path = self.root / name + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text(content, encoding="utf-8") + return path + + def errors(self, files: list[Path]) -> list[str]: + errors: list[str] = [] + CHECKS.check_community_document_links(files, errors) + return errors + + def test_all_community_language_pairs_use_existing_root_targets(self) -> None: + files = [] + for name in sorted(CHECKS.COMMUNITY_DOCUMENT_NAMES): + peer = ( + name.removesuffix(".zh_CN.md") + ".md" + if name.endswith(".zh_CN.md") else name.removesuffix(".md") + ".zh_CN.md" + ) + files.append(self.document(f".github/{name}", f"[Language](/.github/{peer})\n")) + errors = self.errors(files) + CHECKS.check_markdown_links(files, errors) + CHECKS.check_document_languages(files, errors) + self.assertEqual(errors, []) + + def test_original_html_switch_is_rejected_despite_existing_peer(self) -> None: + page = self.document( + ".github/CODE_OF_CONDUCT.md", + '

Language

\n', + ) + self.document(".github/CODE_OF_CONDUCT.zh_CN.md", "# Translation\n") + errors = self.errors([page]) + self.assertTrue(any("language switch must use a Markdown link" in error for error in errors)) + self.assertTrue(any("repository-root path" in error for error in errors)) + + def test_wrong_directory_and_fixed_upstream_switches_are_rejected(self) -> None: + for target in ( + "CODE_OF_CONDUCT.zh_CN.md", + "/CODE_OF_CONDUCT.zh_CN.md", + "https://github.com/FoloToy/ai-passport/blob/main/.github/CODE_OF_CONDUCT.zh_CN.md", + ): + with self.subTest(target=target): + page = self.document(".github/CODE_OF_CONDUCT.md", f"[Language]({target})\n") + self.assertTrue(any("language switch" in error for error in self.errors([page]))) + + def test_root_target_still_requires_a_real_file(self) -> None: + page = self.document( + ".github/SUPPORT.md", "[Language](/.github/SUPPORT.zh_CN.md)\n" + ) + errors = self.errors([page]) + CHECKS.check_markdown_links([page], errors) + self.assertEqual(len(errors), 1) + self.assertIn("missing link target", errors[0]) + + def test_language_switch_allows_markdown_titles_and_angle_targets(self) -> None: + for target in ( + '/.github/SUPPORT.zh_CN.md "Chinese"', + ' "Chinese"', + ): + with self.subTest(target=target): + page = self.document(".github/SUPPORT.md", f"\nEnglish | [Language]({target})\n") + self.assertEqual(self.errors([page]), []) + + def test_markdown_switch_inside_raw_html_is_rejected(self) -> None: + for content in ( + '

\n[Language](/.github/SUPPORT.zh_CN.md)\n

\n', + '

[Language](/.github/SUPPORT.zh_CN.md)

\n', + '\n', + ): + with self.subTest(content=content): + page = self.document(".github/SUPPORT.md", content) + self.assertTrue(any("language switch" in error for error in self.errors([page]))) + + def test_relative_body_links_are_rejected_for_markdown_and_html(self) -> None: + for link in ( + "[Security](SECURITY.md)", + "[Guide](../docs/README.md)", + 'Security', + "Security", + "[Security](//example.com/SECURITY.md)", + "[Security][security]\n\n[security]: SECURITY.md", + '[Security][security]\n\n[security]: "Policy"', + ): + with self.subTest(link=link): + page = self.document( + ".github/SUPPORT.md", "[Language](/.github/SUPPORT.zh_CN.md)\n\n" + link + ) + self.assertTrue(any("repository-root path" in error for error in self.errors([page]))) + + def test_root_links_external_links_and_fragments_are_allowed(self) -> None: + page = self.document( + ".github/CONTRIBUTING.md", + "[Language](/.github/CONTRIBUTING.zh_CN.md)\n\n" + "[Guide](/docs/README.md) [Rules](/AGENTS.md) [License](/LICENSE)\n" + "[Security](/.github/SECURITY.md#reporting-a-vulnerability)\n" + "[Site](https://example.com) [Mail](mailto:security@example.com) [Top](#top)\n", + ) + self.assertEqual(self.errors([page]), []) + + def test_root_reference_links_and_external_attribution_are_allowed(self) -> None: + page = self.document( + ".github/CODE_OF_CONDUCT.md", + "[Language](/.github/CODE_OF_CONDUCT.zh_CN.md)\n\n" + "[Security][security] [Upstream][attribution]\n\n" + '[security]: "Security policy"\n' + "[attribution]: https://github.com/mozilla/diversity\n", + ) + self.assertEqual(self.errors([page]), []) + + def test_body_cannot_pin_internal_docs_to_upstream_but_reporting_is_allowed(self) -> None: + for link in ( + "[Rules](https://github.com/FoloToy/ai-passport/blob/main/AGENTS.md)", + "[Docs](https://github.com/FoloToy/ai-passport/tree/main/docs)", + "[Rules][rules]\n[rules]: https://github.com/FoloToy/ai-passport/blob/main/AGENTS.md", + 'Rules', + ): + with self.subTest(link=link): + page = self.document( + ".github/SUPPORT.md", "[Language](/.github/SUPPORT.zh_CN.md)\n\n" + link + ) + self.assertTrue(any("must not hardcode" in error for error in self.errors([page]))) + page = self.document( + ".github/SECURITY.md", + "[Language](/.github/SECURITY.zh_CN.md)\n\n" + "[Report](https://github.com/FoloToy/ai-passport/security/advisories/new)\n", + ) + self.assertEqual(self.errors([page]), []) + + def test_other_documents_and_pr_template_keep_their_own_context(self) -> None: + files = [ + self.document("docs/README.md", "[Language](README.zh_CN.md)\n"), + self.document(".github/PULL_REQUEST_TEMPLATE.md", "[Language](PULL_REQUEST_TEMPLATE.zh_CN.md)\n"), + ] + self.assertEqual(self.errors(files), []) + + def test_main_runs_community_link_guard(self) -> None: + page = self.document( + ".github/SECURITY.md", 'Language\n' + ) + with ( + patch.object(CHECKS, "git_files", return_value=[page]), + patch.object(CHECKS, "check_required_files"), + patch.object(CHECKS, "check_action_pins"), + patch.object(CHECKS, "check_issue_forms"), + contextlib.redirect_stderr(io.StringIO()) as output, + ): + self.assertEqual(CHECKS.main(), 1) + self.assertIn("language switch must use a Markdown link", output.getvalue()) + + if __name__ == "__main__": unittest.main() diff --git a/tools/check_repo.py b/tools/check_repo.py index ad4240d..4d9249c 100755 --- a/tools/check_repo.py +++ b/tools/check_repo.py @@ -7,12 +7,19 @@ import re import subprocess import sys from pathlib import Path -from urllib.parse import unquote +from urllib.parse import unquote, urlsplit ROOT = Path(__file__).resolve().parent.parent FULL_SHA_RE = re.compile(r"^[0-9a-f]{40}$") MARKDOWN_LINK_RE = re.compile(r"!?\[[^\]]*\]\(([^)]+)\)") +HTML_LINK_RE = re.compile(r"\bhref\s*=\s*['\"]([^'\"]+)['\"]", re.IGNORECASE) +MARKDOWN_REFERENCE_RE = re.compile(r"(?m)^ {0,3}\[[^\]]+\]:\s*(<[^>\n]+>|\S+)") +COMMUNITY_DOCUMENT_NAMES = { + f"{stem}{suffix}.md" + for stem in ("CONTRIBUTING", "CODE_OF_CONDUCT", "SECURITY", "SUPPORT") + for suffix in ("", ".zh_CN") +} CJK_RE = re.compile(r"[\u3400-\u4dbf\u4e00-\u9fff]") SECRET_PATTERNS = { "GitHub token": re.compile(r"(?:ghp_|github_pat_)[A-Za-z0-9_]{20,}"), @@ -146,6 +153,57 @@ def check_markdown_links( errors.append(f"{path.relative_to(ROOT)}: missing link target {target}") +def check_community_document_links(files: list[Path], errors: list[str]) -> None: + """Keep overview/file-view navigation independent of the render directory.""" + def target_path(raw: str) -> str: + # Ignore optional link titles, just as the existing local-link check does. + fields = raw.strip().split(maxsplit=1) + return fields[0].strip("<>") if fields else "" + + for path in files: + if path.parent != ROOT / ".github" or path.name not in COMMUNITY_DOCUMENT_NAMES: + continue + text = path.read_text(encoding="utf-8") + peer = ( + path.name.removesuffix(".zh_CN.md") + ".md" + if path.name.endswith(".zh_CN.md") + else path.stem + ".zh_CN.md" + ) + expected = f"/.github/{peer}" + # Keep the switch on the first nonempty line, outside raw HTML blocks + # (Markdown inside

is not rendered as a link by GitHub). + first_line = next((line.strip() for line in text.splitlines() if line.strip()), "") + opening_targets = [target_path(raw) for raw in MARKDOWN_LINK_RE.findall(first_line)] + if expected not in opening_targets or re.search(r"