processing-markdown

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Processing Markdown with mq

使用mq处理Markdown

Quick Reference

快速参考

Selectors

选择器

SelectorDescription
.h
All headings
.h1
.h6
Specific heading level
.text
Text nodes
.code
Code blocks
.code_inline
Inline code
.strong
Bold text
.emphasis
Italic text
.delete
Strikethrough
.link
Links
.image
Images
.list
List items
.blockquote
Block quotes
.[][]
Table cells
.html
or
.<>
HTML nodes
.footnote
Footnotes
.math
Math blocks
.yaml
,
.toml
Frontmatter
选择器描述
.h
所有标题
.h1
.h6
指定层级的标题
.text
文本节点
.code
代码块
.code_inline
行内代码
.strong
粗体文本
.emphasis
斜体文本
.delete
删除线文本
.link
链接
.image
图片
.list
列表项
.blockquote
块引用
.[][]
表格单元格
.html
.<>
HTML节点
.footnote
脚注
.math
数学公式块
.yaml
,
.toml
前置元数据(Frontmatter)

Attribute Access

属性访问

mq
.h.level           # Heading level (1-6)
.h.depth           # Same as .h.level
.code.lang         # Code block language
.code.value        # Code block content
.link.url          # Link URL
.link.title        # Link title
.image.url         # Image URL
.image.alt         # Image alt text
.list.index        # List item index
.list.level        # Nesting level
.list.ordered      # Whether ordered list
.list.checked      # Checkbox state
.[0][0].row        # Table cell row
.[0][0].column     # Table cell column
mq
.h.level           # 标题层级(1-6)
.h.depth           # 与.h.level功能相同
.code.lang         # 代码块语言
.code.value        # 代码块内容
.link.url          # 链接URL
.link.title        # 链接标题
.image.url         # 图片URL
.image.alt         # 图片替代文本
.list.index        # 列表项索引
.list.level        # 嵌套层级
.list.ordered      # 是否为有序列表
.list.checked      # 复选框状态
.[0][0].row        # 表格单元格行号
.[0][0].column     # 表格单元格列号

Common Patterns

常用模式

Extract Elements

提取元素

bash
mq '.h' file.md                         # All headings
mq '.h1' file.md                        # Only h1 headings
mq '.code' file.md                      # All code blocks
mq '.link.url' file.md                  # All URLs
mq '.image.alt' file.md                 # All image alt texts
bash
mq '.h' file.md                         # 提取所有标题
mq '.h1' file.md                        # 仅提取h1标题
mq '.code' file.md                      # 提取所有代码块
mq '.link.url' file.md                  # 提取所有URL
mq '.image.alt' file.md                 # 提取所有图片替代文本

Filter with select

使用select过滤

bash
mq 'select(.code)' file.md              # Only code blocks
mq 'select(!.code)' file.md             # Everything except code blocks
mq 'select(.h.level <= 2)' file.md      # h1 and h2 only
mq 'select(.code.lang == "rust")' file.md  # Rust code blocks only
mq 'select(contains("TODO"))' file.md   # Nodes containing "TODO"
bash
mq 'select(.code)' file.md              # 仅保留代码块
mq 'select(!.code)' file.md             # 保留除代码块外的所有内容
mq 'select(.h.level <= 2)' file.md      # 仅保留h1和h2标题
mq 'select(.code.lang == "rust")' file.md  # 仅保留Rust代码块
mq 'select(contains("TODO"))' file.md   # 保留包含"TODO"的节点

Transform Content

转换内容

bash
mq '.h | to_text()' file.md             # Headings as plain text
mq '.code | to_text()' file.md          # Code block content as text
mq '.link | to_string()' file.md        # Links as strings
bash
mq '.h | to_text()' file.md             # 将标题转换为纯文本
mq '.code | to_text()' file.md          # 将代码块内容转换为文本
mq '.link | to_string()' file.md        # 将链接转换为字符串

Generate Table of Contents

生成目录

mq
.h
| let link = to_link("#" + to_text(self), to_text(self), "")
| let level = .h.depth
| if (!is_none(level)): to_md_list(link, level - 1)
mq
.h
| let link = to_link("#" + to_text(self), to_text(self), "")
| let level = .h.depth
| if (!is_none(level)): to_md_list(link, level - 1)

Multi-File Processing

多文件处理

bash
mq -A 'pluck(.code.value)' *.md         # Collect all code values
mq -S 's"\n---\n"' 'identity()' *.md    # Merge files with separator
bash
mq -A 'pluck(.code.value)' *.md         # 收集所有代码块内容
mq -S 's"\n---\n"' 'identity()' *.md    # 使用分隔符合并多个文件

CLI Options

CLI选项

FlagPurpose
-A, --aggregate
Combine all inputs into single array
-I, --input-format
Input format:
markdown
,
mdx
,
html
,
text
,
null
,
raw
-F, --output-format
Output format:
markdown
,
html
,
text
,
json
,
none
-U, --update
Update file in place
-f, --from-file
Load query from
.mq
file
-o, --output
Write to output file
-S, --separator
Insert query result between files
--args NAME VALUE
Set runtime variable
--rawfile NAME FILE
Load file into variable
--stream
Process line by line
--csv, --yaml, --toml, --xml
Load built-in modules
-C, --color-output
Colorize output
-P THRESHOLD
Parallel processing threshold
For complete function reference, see REFERENCE.md. For advanced patterns and examples, see EXAMPLES.md.
标志用途
-A, --aggregate
将所有输入合并为单个数组
-I, --input-format
输入格式:
markdown
,
mdx
,
html
,
text
,
null
,
raw
-F, --output-format
输出格式:
markdown
,
html
,
text
,
json
,
none
-U, --update
原地更新文件
-f, --from-file
.mq
文件加载查询语句
-o, --output
将结果写入输出文件
-S, --separator
在多个文件的查询结果之间插入指定分隔符
--args NAME VALUE
设置运行时变量
--rawfile NAME FILE
将文件内容加载到变量中
--stream
逐行处理输入
--csv, --yaml, --toml, --xml
加载内置模块
-C, --color-output
为输出添加颜色
-P THRESHOLD
并行处理阈值
完整的函数参考请查看REFERENCE.md。 高级模式及示例请查看EXAMPLES.md

When NOT to Use mq

不适合使用mq的场景

  • Binary file processing
  • Non-Markdown structured data (use jq for JSON, yq for YAML instead)
  • Simple
    cat
    or
    echo
    operations where no transformation is needed
  • 二进制文件处理
  • 非Markdown格式的结构化数据(JSON用jq,YAML用yq)
  • 无需转换的简单
    cat
    echo
    操作