
Repomix 输出格式完全指南XML、Markdown、JSON 与纯文本四种结构的选型与实践【免费下载链接】repomix Repomix is a powerful tool that packs your entire repository into a single, AI-friendly file. Perfect for when you need to feed your codebase to Large Language Models (LLMs) or other AI tools like Claude, ChatGPT, DeepSeek, Perplexity, Gemini, Gemma, Llama, Grok, and more.项目地址: https://gitcode.com/GitHub_Trending/rep/repomixRepomix 将整个代码库打包为单一、AI 友好的文件时提供了 XML、Markdown、JSON、纯文本四种输出格式。本文以官方输出格式文档为核心结合仓库源码outputGenerate.ts 及三种样式模板深入讲解每种格式的生成原理、结构差异与适用场景并给出基于jq的 JSON 结构化提取实战命令以及通过repomix.config.json固化默认格式的完整配置方案。读完本文你将能够按 Claude、ChatGPT、Gemini、API 集成与自动化脚本等不同场景为 Repomix 打包产物选择并配置最合适的输出结构。四种输出格式总览Repomix 支持四种输出格式可通过--style参数随时切换格式命令特点XML默认repomix --style xml针对 AI 处理优化解析精度最高Markdownrepomix --style markdown可读性好带语法高亮JSONrepomix --style json结构化、可编程访问适合 API 与自动化纯文本Plain Textrepomix --style plain简单通用兼容性最广在源码层面格式枚举定义于 configSchema.tsrepomixOutputStyleSchema v.picklist([xml, markdown, json, plain])四者之外的值会在配置校验阶段被直接拒绝。同时每种格式都有默认输出文件名defaultFilePathMapxml → repomix-output.xml、markdown → repomix-output.md、plain → repomix-output.txt、json → repomix-output.json可以通过-o/--output或配置中的output.filePath覆盖。XML 格式默认且为 AI 解析优化repomix --style xmlXML 格式的输出结构如下This file is a merged representation of the entire codebase... file_summary (Metadaten und KI-Anweisungen) /file_summary directory_structure src/ index.ts utils/ helper.ts /directory_structure files file pathsrc/index.ts // Dateiinhalt hier /file /files git_logs 2025-08-20 00:47:19 0900|feat(cli): Add --include-logs option for git commit history README.md src/cli/cliRun.ts src/core/git/gitCommand.ts src/core/git/gitLogHandle.ts src/core/output/outputGenerate.ts 2025-08-21 00:09:43 0900|Merge pull request #795 from yamadashy/chore/ratchet-update-ci .github/workflows/ratchet-update.yml /git_logs为什么 XML 是默认格式Repomix 将 XML 作为默认格式是基于大量研究与测试的决策既包含实证依据也包含 AI 辅助代码分析的实践考量。这一选择主要受主流 AI 厂商官方推荐的影响AnthropicClaude官方明确建议使用 XML 标签组织提示词并说明 Claude 在训练过程中接触过此类结构化的提示GoogleGemini官方建议在复杂任务中使用包括 XML 在内的结构化格式OpenAIGPT官方在复杂场景中同样倡导结构化提示。源码视角XML 模板的两条生成路径从实现看outputGenerate.ts 在config.output.style xml时按parsableStyle开关分流默认路径Handlebar 模板渲染使用 xmlStyle.ts 中定义的 Handlebars 模板。模板按开关条件渲染file_summary含purpose、file_format、usage_guidelines、notes四个子节、user_provided_header、directory_structure、files每个文件一个file path...节点、git_diffs、git_logs与instruction。模板内容由createRenderContextoutputGenerate.ts根据output.fileSummary、output.directoryStructure、output.files等配置动态控制未启用的节不会出现在输出中。--parsable-style路径可解析 XML当需要输出严格合法、可被解析器读取的 XML 时走generateParsableXmlOutputoutputGenerate.ts它惰性加载fast-xml-builder约 3ms构建文档树并对特殊字符做转义。文档中默认的 XML 示例为人类可读版本而 parsable 模式会为file_summary、files等节点生成带属性的完整 XML 结构。Markdown 格式可读性与语法高亮repomix --style markdownMarkdown 提供可读的排版结构This file is a merged representation of the entire codebase... # File Summary (Metadaten und KI-Anweisungen) # Directory Structure src/ index.ts utils/ helper.ts # Files ## File: src/index.ts (代码块中的文件内容) # Git Logs 2025-08-20 00:47:19 0900|feat(cli): Add --include-logs option for git commit history README.md src/cli/cliRun.ts src/core/git/gitCommand.ts src/core/git/gitLogHandle.ts src/core/output/outputGenerate.ts源码视角Markdown 的两个实现细节自动语法高亮markdownStyle.ts 为每个文件渲染## File: 路径标题后通过getFileExtension帮助函数注册于 outputStyleUtils.ts查询extensionToLanguageMap映射表为代码块附加语言标识。该映射覆盖 JavaScript/TypeScript、Python、Rust、Go、Vue、Dockerfile、SQL、Prisma 等数百种扩展名且按文件名而非完整路径匹配从而正确处理docker/Dockerfile这类无扩展名文件。动态代码围栏outputGenerate.ts 中的calculateMarkdownDelimiter会扫描所有文件内容、目录树与 git diff 中最长的反引号连续段自动将代码围栏长度扩展为max(3, 最长反引号段 1)。这是为了防止某个 Markdown 源文件内部携带裸的行提前闭合围栏、破坏输出结构——例如 diff 内容中的上下文行就可能包含三个反引号。JSON 格式面向程序化处理repomix --style jsonJSON 格式提供结构化、可编程访问的输出属性名采用 camelCase{ fileSummary: { generationHeader: This file is a merged representation of the entire codebase, combined into a single document by Repomix., purpose: This file contains a packed representation of the entire repositorys contents..., fileFormat: The content is organized as follows..., usageGuidelines: - This file should be treated as read-only..., notes: - Some files may have been excluded based on .gitignore rules... }, userProvidedHeader: Custom header text if specified, directoryStructure: src/ cli/ cliOutput.ts index.ts config/ configLoader.ts, files: { src/index.js: // File contents here, src/utils.js: // File contents here }, instruction: Custom instructions from instructionFilePath }源码视角JSON 的组装逻辑JSON 输出由generateParsableJsonOutputoutputGenerate.ts直接构造对象树并JSON.stringify(..., null, 2)格式化生成。与 XML 模板不同JSON 是纯程序化组装fileSummary、userProvidedHeader、directoryStructure、files、gitDiffs、gitLogs、instruction各字段均根据对应配置开关条件展开如files通过reduce将每个文件的路径映射为键、内容映射为值。因此 JSON 输出天然与配置一致——关闭output.fileSummary后fileSummary键会整体消失不会出现空对象占位。JSON 格式的优势程序化处理可被任何编程语言的 JSON 库轻松解析与操作API 集成可直接被 Web 服务与应用程序消费AI 工具兼容性结构化格式对机器学习与 AI 系统友好数据分析可用jq等工具直接提取特定信息。使用jq处理 JSON 输出JSON 格式使程序化提取特定信息变得非常简单以下是常用示例。基本文件操作# 列出所有文件路径 cat repomix-output.json | jq -r .files | keys[] # 统计文件总数 cat repomix-output.json | jq .files | keys | length # 提取特定文件内容 cat repomix-output.json | jq -r .files[README.md] cat repomix-output.json | jq -r .files[src/index.js]文件过滤与分析# 按扩展名查找文件 cat repomix-output.json | jq -r .files | keys[] | select(endswith(.ts)) cat repomix-output.json | jq -r .files | keys[] | select(endswith(.js) or endswith(.ts)) # 查找包含特定文本的文件 cat repomix-output.json | jq -r .files | to_entries[] | select(.value | contains(function)) | .key # 生成带字符数的文件列表 cat repomix-output.json | jq -r .files | to_entries[] | \(.key): \(.value | length) characters元数据提取# 提取目录结构 cat repomix-output.json | jq -r .directoryStructure # 获取文件摘要信息 cat repomix-output.json | jq .fileSummary.purpose cat repomix-output.json | jq -r .fileSummary.generationHeader # 提取自定义头部若存在 cat repomix-output.json | jq -r .userProvidedHeader // No header provided # 获取自定义指令 cat repomix-output.json | jq -r .instruction // No instructions provided高级分析# 按内容长度找出最大的文件 cat repomix-output.json | jq -r .files | to_entries[] | [.key, (.value | length)] | tsv | sort -k2 -nr | head -10 # 搜索包含特定模式的文件 cat repomix-output.json | jq -r .files | to_entries[] | select(.value | test(import.*react; i)) | .key # 提取匹配多个扩展名的文件路径 cat repomix-output.json | jq -r .files | keys[] | select(test(\\.(js|ts|jsx|tsx)$))注意jq的test()使用正则表达式.需写成\.转义.files在--style json下是一个路径 → 内容的映射对象这是上述所有管道命令能成立的前提。纯文本格式极简与通用repomix --style plain输出结构如下This file is a merged representation of the entire codebase... File Summary (Metadaten und KI-Anweisungen) Directory Structure src/ index.ts utils/ helper.ts Files File: src/index.ts // Dateiinhalt hier Git Logs 2025-08-20 00:47:19 0900|feat(cli): Add --include-logs option for git commit history README.md src/cli/cliRun.ts src/core/git/gitCommand.ts src/core/git/gitLogHandle.ts src/core/output/outputGenerate.ts源码视角纯文本的分隔线约定plainStyle.ts 定义了两种分隔线常量PLAIN_SEPARATOR .repeat(16)16 个等号用于文件条目与 git 提交之间的细分隔PLAIN_LONG_SEPARATOR .repeat(64)64 个等号用于File Summary、Directory Structure、Files、Git Logs等大节的标题分隔并在文件末尾追加End of Codebase长分隔线标记结束。纯文本模板同样受fileSummaryEnabled、directoryStructureEnabled、filesEnabled、gitLogEnabled等开关控制且不依赖任何 Markdown 语法或 XML 解析因此可以被任何文本处理工具无差别读取。与 AI 模型配合使用四种格式都能很好地配合 AI 模型但按场景选择效果更佳XML推荐用于 Claude解析精度最佳Markdown用于通用场景可读性最好JSON用于程序化处理与 API 集成纯文本用于追求简单性与最大兼容性的场景。值得一提的是输出文件头部generationHeader会根据当前打包配置自动生成说明文字源码 outputStyleDecorate.ts 中的generateHeader会动态拼接整库 / 子集描述并在启用了removeComments、removeEmptyLines、showLineNumbers、parsableStyle、compress、关闭安全检测等处理时追加对应提示generateSummaryNotes同文件 L150-L212则会在notes节中列出被.gitignore排除、二进制文件未包含、git 日志数量默认 50 条等元信息。把这些说明文本一并喂给 AI 模型能显著提升其对打包产物的理解准确度。自定义默认格式在repomix.config.json中设置默认输出格式{ output: { style: json, filePath: output.json } }output.style可选xml、markdown、json、plain配置校验由 configSchema.ts 的picklist完成默认值为xmlL129output.filePath输出文件路径未指定时使用defaultFilePathMap中对应格式的默认文件名output.parsableStyle布尔值默认falseL131开启后 XML 输出将转义特殊字符、生成可被解析器读取的严格合法结构。命令行下等价的操作方式包括# 一次性指定输出文件与格式 repomix -o my-output.md --style markdown repomix -o my-output.json --style json # 在输出开头注入自定义文本 repomix --style xml --header-text 项目说明... # 从文件注入自定义指令 repomix --style json --instruction-file-path ./INSTRUCTION.mdCLI 侧对应参数为-o/--output、--style、--header-text、--instruction-file-path、--parsable-style等完整清单见命令行选项。headerText与instruction会分别进入四种格式输出的user_provided_header/User Provided Header与instruction/# Instruction节并在 JSON 中呈现为userProvidedHeader与instruction字段。相关资源配置指南全部配置选项的完整参考命令行选项--style等输出参数说明代码压缩在保留结构的前提下降低 Token 数量提示词示例如何将不同格式的输出用于各类 AI 模型。【免费下载链接】repomix Repomix is a powerful tool that packs your entire repository into a single, AI-friendly file. Perfect for when you need to feed your codebase to Large Language Models (LLMs) or other AI tools like Claude, ChatGPT, DeepSeek, Perplexity, Gemini, Gemma, Llama, Grok, and more.项目地址: https://gitcode.com/GitHub_Trending/rep/repomix创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考