harehare/mq

GitHub: harehare/mq

mq 是一款采用类 jq 语法的命令行 Markdown 查询与转换工具,旨在高效地对 Markdown 及多种结构化文本进行过滤、提取和格式转换。

Stars: 917 | Forks: 15

访问网站 🌐阅读书籍 📖练习场 🎮

mq

**查询。过滤。转换 Markdown。** [![ci](https://img.shields.io/github/actions/workflow/status/harehare/mq/ci.yml?style=flat-square&logo=github-actions&label=ci)](https://github.com/harehare/mq/actions/workflows/ci.yml) [![audit](https://img.shields.io/github/actions/workflow/status/harehare/mq/audit.yml?style=flat-square&logo=github-actions&label=audit)](https://github.com/harehare/mq/actions/workflows/audit.yml) [![crates.io](https://img.shields.io/crates/v/mq-markdown?logo=rust&style=flat-square)](https://crates.io/crates/mq-markdown) [![codecov](https://img.shields.io/codecov/c/github/harehare/mq?logo=codecov&style=flat-square)](https://codecov.io/gh/harehare/mq) [![codspeed badge](https://img.shields.io/endpoint?url=https://codspeed.io/badge.json&style=flat-square)](https://codspeed.io/harehare/mq) mq 是一个命令行工具,它使用类似于 jq 的语法来处理 Markdown。 它使用 Rust 编写,让您可以轻松地对结构化数据进行切片、过滤、映射和转换。
![演示](https://static.pigsec.cn/wp-content/uploads/repos/2026/06/0eceddb511121323.gif) ## 为什么选择 mq? mq 让处理 Markdown 文件变得像 jq 处理 JSON 一样简单。它特别适用于: - **LLM 工作流**:高效地操作和处理 LLM prompt 和输出中使用的 Markdown - **LLM 输入生成**:生成针对 LLM 消费优化的结构化 Markdown 内容,因为 Markdown 是大多数语言模型的主要输入格式 - **文档管理**:跨多个文档文件提取、转换和组织内容 - **内容分析**:快速从 Markdown 文档中提取特定部分或模式 - **批处理**:跨多个 Markdown 文件应用一致的转换 由于 LLM 输入主要为 Markdown 格式,mq 提供了高效的工具,用于生成和处理 LLM 所需的结构化 Markdown 内容。 ## 功能 - **切片与过滤**:轻松提取 Markdown 文档的特定部分。 - **映射与转换**:对您的 Markdown 内容应用各种转换。 - **命令行界面**:简单直观的 CLI,便于快速操作。 - **可扩展性**:可通过自定义函数轻松扩展。 - **内置支持**:提供许多内置函数和选择器来过滤和转换内容。 - **REPL 支持**:提供交互式命令行 REPL,方便测试和实验。 - **IDE 支持**:提供 VSCode 扩展和语言服务器**协议** (LSP) 支持,用于自定义函数开发。 - **调试器**:包含一个实验性调试器 (`mq-dbg`),用于以交互方式检查和单步执行 mq 查询。 - **外部子命令**:通过将名为 `mq-` 开头的可执行文件放入 `~/.local/bin/`,即可使用自定义子命令扩展 mq。 ## 安装 ### 快速安装 ``` curl -sSL https://mqlang.org/install.sh | bash ``` 安装程序将会: - 为您的平台下载最新的 mq 二进制文件 - 将其安装到 `~/.local/bin/` - 更新您的 shell profile,将 mq 添加到您的 PATH 中 ### Cargo ``` # 从 crates.io 安装 cargo install mq-run # 从 Github 安装 cargo install --git https://github.com/harehare/mq.git mq-run --tag v0.6.0 # 最新开发版本 cargo install --git https://github.com/harehare/mq.git mq-run --bin mq # 安装 debugger cargo install --git https://github.com/harehare/mq.git mq-run --bin mq-dbg --features="debugger" # 使用 binstall 安装 cargo binstall mq-run@0.6.0 ``` ### 二进制文件 您可以从 [GitHub 发布页面](https://github.com/harehare/mq/releases) 下载预构建的二进制文件: ``` # macOS (Apple Silicon) curl -L https://github.com/harehare/mq/releases/download/v0.6.0/mq-aarch64-apple-darwin -o /usr/local/bin/mq && chmod +x /usr/local/bin/mq # Linux x86_64 curl -L https://github.com/harehare/mq/releases/download/v0.6.0/mq-x86_64-unknown-linux-gnu -o /usr/local/bin/mq && chmod +x /usr/local/bin/mq # Linux arm64 curl -L https://github.com/harehare/mq/releases/download/v0.6.0/mq-aarch64-unknown-linux-gnu -o /usr/local/bin/mq && chmod +x /usr/local/bin/mq # Windows (PowerShell) Invoke-WebRequest -Uri https://github.com/harehare/mq/releases/download/v0.6.0/mq-x86_64-pc-windows-msvc.exe -OutFile "$env:USERPROFILE\bin\mq.exe" ``` ### Homebrew ``` # 使用 Homebrew (macOS 和 Linux) brew install mq ``` ### Arch ``` # 使用 yay (ArchLinux) yay -S mq-bin ``` ### Docker ``` $ docker run --rm ghcr.io/harehare/mq:0.6.0 ``` ### Visual Studio Code 扩展 您可以从 [Visual Studio Marketplace](https://marketplace.visualstudio.com/items?itemName=harehare.vscode-mq) 安装 VSCode 扩展。 对于兼容 VS Code 的编辑器,也可以在 [Open VSX Registry](https://open-vsx.org/extension/harehare/vscode-mq) 上获取。 ### Neovim 您可以按照 [mq.nvim README](https://github.com/harehare/mq/blob/main/editors/neovim/README.md) 中的说明安装 Neovim 插件。 ### Zed 您可以从 [harehare/zed-mq](https://github.com/harehare/zed-mq) 仓库安装 Zed 扩展。 ### GitHub Actions 您可以在 GitHub Actions 工作流中使用 [Setup mq](https://github.com/marketplace/actions/setup-mq) 操作来运行 mq: ``` steps: - uses: actions/checkout@v6 - uses: harehare/setup-mq@v1 - run: mq '.code' README.md ``` ## Web ### 练习场 [练习场](https://mqlang.org/playground) 允许您在浏览器中运行 mq 查询,无需安装。 ### mq-web (npm) [mq-web](https://www.npmjs.com/package/mq-web) 是官方提供的用于浏览器的 WebAssembly 构建版本。 ## 语言绑定 我们提供了用于 Elixir、Python、Ruby、Java 和 Go 的语言绑定。详情请参阅 [语言绑定文档](https://mqlang.org/book/start/language_bindings.html)。 ## 用法 有关更详细的用法和示例,请参阅 [文档](https://mqlang.org/book/)。 有关实用示例的综合集合,请参阅 [示例指南](https://mqlang.org/book/start/example/)。 ### 基本用法
完整选项列表(点击展开) ``` Usage: mq [OPTIONS] [QUERY OR FILE] [FILES]... [COMMAND] Commands: repl Start a REPL session for interactive query execution help Print this message or the help of the given subcommand(s) Arguments: [QUERY OR FILE] [FILES]... Options: -A, --aggregate Aggregate all input files/content into a single array -f, --from-file load filter from the file -I, --input-format Set input format [possible values: markdown, mdx, html, text, null, raw, bytes, cbor, csv, hcl, json, psv, toml, toon, tsv, xml, yaml] -L, --directory Search modules from the directory -M, --module-names Load additional modules from specified files -m, --import-module-names Import modules by name, making them available as `name::fn()` in queries --args Sets a named string argument. NAME is accessible directly in queries, and also via ARGS."named" when --args or --argv is given --rawfile Sets file contents that can be referenced at runtime --stream Enable streaming mode for processing large files line by line -F, --output-format Set output format [default: markdown] [possible values: markdown, html, text, json, table, grep, raw, none] -U, --update Update the input markdown (aliases: -i, --in-place, --inplace) --unbuffered Unbuffered output --list-style Set the list style for markdown output [default: dash] [possible values: dash, plus, star] --link-title-style Set the link title surround style for markdown output [default: double] [possible values: double, single, paren] --link-url-style Set the link URL surround style for markdown links [default: none] [possible values: none, angle] -S, --separator Specify a query to insert between files as a separator -o, --output Output to the specified file -C, --color-output Colorize markdown output -B, --before-context Show NUM nodes before each match. Only effective with -F grep --after-context Show NUM nodes after each match. Only effective with -F grep --context Show NUM nodes before and after each match. Only effective with -F grep --list List all available subcommands (built-in and external) --doc Use the built-in reference document as input instead of a file -P Number of files to process before switching to parallel processing [default: 10] --argv [...] Positional string arguments, available as ARGS."positional" in queries -O, --optimize-level Optimization level for AST transformations (none = no changes, basic = constant folding and dead-branch elimination, full = all passes) [default: none] [possible values: none, basic, full] -h, --help Print help -V, --version Print version # 示例 mq 'query' file.md mq -f 'file' file.md # read query from file mq repl # start a REPL session # 通过文件扩展名或 -I 标志自动解析 mq automatically imports the matching module based on the file extension. Use -I to force a specific format: .cbor / -I cbor import "cbor" | cbor::cbor_parse() (reads as bytes) .csv / -I csv import "csv" | csv::csv_parse(true) .hcl / -I hcl import "hcl" | hcl::hcl_parse() .json / -I json import "json" | json::json_parse() .psv / -I psv import "csv" | csv::psv_parse(true) .toml / -I toml import "toml" | toml::toml_parse() .toon / -I toon import "toon" | toon::toon_parse() .tsv / -I tsv import "csv" | csv::tsv_parse(true) .xml / -I xml import "xml" | xml::xml_parse() .yaml / -I yaml import "yaml" | yaml::yaml_parse() Use -I raw to disable auto-parsing and receive the raw string. Use -I bytes to read input as raw bytes without parsing. # 将参数传递给查询 (ARGS) When --args or --argv is given, ARGS = {"positional": [...], "named": {...}} mq -I null 'name' --args name Alice mq -I null 'ARGS | ."named"' --args name Alice # => {"name": "Alice"} mq -I null 'ARGS | ."positional"' --argv x y z # must come after query and files # => ["x", "y", "z"] mq -I null 'ARGS' file.md --args name Alice --argv x y z # => {"positional": ["x","y","z"], "named": {"name": "Alice"}} ```
这是一个如何使用 `mq` 的基本示例: ``` # 从文档中提取所有标题 mq '.h' README.md # 仅提取 h1 标题 mq '.h(1)' README.md # 提取 h1 和 h2 标题 mq '.h(1, 2)' README.md # 使用范围提取级别 1 到 3 的标题 mq '.h(1..3)' README.md # 仅提取 Rust 代码块 mq '.code("rust")' example.md # 提取包含 "name" 的代码块 mq '.code | select(contains("name"))' example.md # 从代码块中提取代码值 mq -A 'pluck(.code.value)' example.md # 从代码块中提取语言名称 mq '.code.lang' documentation.md # 从所有链接中提取 URL mq '.link.url' README.md # 过滤包含 "name" 的表格单元格 mq '.[][] | select(contains("name"))' data.md # 选择包含 "name" 的列表或标题 mq 'select(.[] || .h) | select(contains("name"))' docs.md # 排除 JavaScript 代码块 mq '.code | select(.code.lang != "js")' examples.md # 将 CSV 转换为 Markdown 表格 mq 'include "csv" | csv_parse(true) | csv_to_markdown_table()' example.csv # 通过标题提取部分 mq -A 'section::section("Installation")' README.md # 按标题级别过滤部分(标量或范围) mq -A 'section::sections() | section::by_level(2)' README.md mq -A 'section::sections() | section::by_level(1..2)' README.md ``` ### 通过子命令组合工作流 `mq` 子命令旨在通过 Unix 管道协同工作。 ``` # 将 Excel 报告转换为 Markdown,然后提取所有标题 mq conv report.xlsx | mq '.h' # 转换 Word 文档并提取特定部分 mq conv document.docx | mq -A 'section::section("Summary")' # 在终端中直接转换并查看 Markdown mq conv slides.pdf | mq view ``` 运行 `mq --list` 以查看所有可用的子命令(内置和外部)。 ## 外部子命令 您可以通过将名为 `mq-` 开头的可执行文件放入 `~/.local/bin/` 或您的 `PATH` 中的任何位置,来使用自定义子命令扩展 `mq`。 这使您可以轻松地将自己的工具和工作流添加到 `mq`,而无需修改核心二进制文件。 有关完整列表和详细信息,请参阅 [外部子命令文档](https://mqlang.org/book/start/external_subcommands)。 ## 支持 - 🐛 [报告 bug](https://github.com/harehare/mq/issues) - 💡 [请求新功能](https://github.com/harehare/mq/issues) - ⭐ 如果您觉得它有用,请给[项目加星](https://github.com/harehare/mq)! ## 许可证 本项目基于 MIT 许可证授权。详情请参阅 [LICENSE](LICENSE) 文件。
标签:AI工具, LLM辅助工具, Markdown, Rust, 可视化界面, 文本处理, 查询语言, 网络流量审计, 请求拦截, 通知系统