querying-markdown
Query, filter, and transform Markdown structurally with mq — a jq-like CLI for Markdown. Use to extract headings/sections/code-blocks/links from .md files, build a table of contents, pull code blocks of a given language, slice or reshape LLM prompt/output Markdown, or batch-transform docs. Triggers on "extract sections from this markdown", "get all the code blocks", "jq for markdown", "mq", or any structural query over Markdown that grep/Read can't do cleanly.
What this skill does
# querying-markdown
`mq` is "jq for Markdown" — it parses a `.md` file into a node stream and lets
you select, filter, and transform by structure (`.h2`, `.code("rust")`,
`.link`) instead of by line-matching. Reach for it when the task is structural:
"every H2 title", "all bash code blocks", "a table of contents", "strip the
frontmatter". For plain substring search, `grep` is still the right tool; for
code (not prose) structure, use `tree-sitting`.
## Before you use mq: is this actually a structural task?
mq parses the whole document into a node tree before it answers, and that parse
cost is real (see [Empirical findings](#empirical-findings)). Most "query a
markdown file" tasks don't need it. Decide first, using the target — not the
file type:
| Your target | Use | Why |
| --- | --- | --- |
| Lines with a fixed prefix — `#`/`##` headings, `>` quotes, `-` bullets, a leading line/verse number | **grep / awk** | Line-matching, not structure. grep is faster and already installed. |
| A substring anywhere | **grep** | mq adds nothing. |
| Code *structure* inside fences (ASTs, symbols, call sites) | **tree-sitting** | mq sees the fence, not the code inside it. |
| Language-filtered code blocks (`.code("bash")`); links as structured `(text, url)` (`-F json '.link'`) | **mq** | grep can't filter a fenced block by language without a brittle hand-rolled fence state machine. |
| Markdown→Markdown transforms that must emit valid Markdown — demote/promote headings, rebuild a TOC with anchors, in-place edit | **mq** | `sed` doesn't know structure and will corrupt nesting/fences. |
If your task lands in a grep/awk row, **do not install mq** — close this skill
and use the line tool. Diagnosed 2026-06-04: a full-KJV smoke test queried
books/chapters/verses (all line-prefix structure) with mq — ~3.3 s per query
where grep is milliseconds, the same answers, and a grep post-filter still
needed on top. Wrong-shape corpus; mq's selectors earn their parse cost only on
the structural rows.
The judgment call is whether the case is *actually* line-prefix or only looks
it. A heading is a prefix; a heading you want demoted **with its subtree**, or a
match you must re-emit as valid Markdown, is structure — mq's row even when the
match looks like a prefix.
## Setup
mq is a single static binary, not preinstalled. Install on first use (idempotent
— exits early if already present, ~1s, no build step):
```bash
bash /mnt/skills/user/querying-markdown/scripts/install-mq.sh
```
This drops the pinned `mq` release into `/usr/local/bin`. Override the version
with `MQ_VERSION=vX.Y.Z`.
## Usage
```bash
mq 'QUERY' file.md # query a file
cat file.md | mq 'QUERY' # query stdin
mq repl # interactive REPL — use to test syntax fast
```
A node stream flows left→right through `|`. Selectors (`.h`, `.code`, `.link`)
pick nodes; functions (`to_text`, `slugify`, `map`, `len`) transform them.
`self` is the current node.
```bash
mq '.h2 | to_text()' README.md # every H2 as plain text
mq '.code("python") | to_text()' file.md # all python code blocks
mq '.h.level' file.md # heading depth per heading
mq -F json '.h2 | to_text()' file.md # results as JSON
mq '.h2 | to_text()' file.md | wc -l # count matches (reliable idiom)
```
## Empirical findings
Measured 2026-06-04 against a full public-domain KJV Bible (66 files, 4.28 MB).
**Parse-bound, not query-bound.** mq reparses the whole document on every
invocation; latency tracks document *size*, not selector or match count. On the
4.28 MB file every query — whether it returned 66 matches or 32,418 — ran
~3.2–3.3 s (~1.3 MB/s); on a normal-sized doc it is single-digit ms. Never loop
mq per query over a large corpus: extract once with `-F json` and work on the
result, or accept a constant per-call parse tax.
**Selectors return nodes, not your domain concepts.** `.h2` over the KJV
returned 1,250 nodes — 1,184 chapter headings plus 66 `eof` markers the source
appended per file, while single-chapter books emitted no chapter heading at all.
`.text` also pulled heading text into the paragraph stream. A raw selector count
is a *node* count; map it to your concept with an explicit predicate
(e.g. `grep -E '^[0-9]+ '` for verses) and check it against a known total before
trusting the number.
**An empty result is ambiguous.** Zero output means *either* the selector
matched nothing *or* mq never ran — a wrapper like `time`/`env` failed in `dash`,
or a malformed heredoc swallowed the command. Re-run the bare
`mq 'QUERY' file.md` before concluding a selector or function is broken.
(Self-inflicted 2026-06-04: a `time: not found` shell error read as a
`to_text()` defect; `to_text()` on code blocks works.)
## Reference
Selector aliases, the built-in function library, table-of-contents and
transform recipes, in-place-edit caveats, and CLI flags live in
[references/cheatsheet.md](references/cheatsheet.md). Read it before writing a
non-trivial query — the dialect is jq-*like*, not jq, so the function names
differ. When unsure of syntax, `mq repl` gives instant feedback.
Related in AI Agents
skill-development
IncludedComprehensive meta-skill for creating, managing, validating, auditing, and distributing Claude Code skills and slash commands (unified in v2.1.3+). Provides skill templates, creation workflows, validation patterns, audit checklists, naming conventions, YAML frontmatter guidance, progressive disclosure examples, and best practices lookup. Use when creating new skills, validating existing skills, auditing skill quality, understanding skill architecture, needing skill templates, learning about YAML frontmatter requirements, progressive disclosure patterns, tool restrictions (allowed-tools), skill composition, skill naming conventions, troubleshooting skill activation issues, creating custom slash commands, configuring command frontmatter, using command arguments ($ARGUMENTS, $1, $2), bash execution in commands, file references in commands, command namespacing, plugin commands, MCP slash commands, Skill tool configuration, or deciding between skills vs slash commands. Delegates to docs-management skill for official documentation.
reprompter
IncludedTransform messy prompts into well-structured, effective prompts — single or multi-agent. Use when: "reprompt", "reprompt this", "clean up this prompt", "structure my prompt", rough text needing XML tags and best practices, "reprompter teams", "repromptception", "run with quality", "smart run", "smart agents", multi-agent tasks, audits, parallel work, anything going to agent teams. Don't use when: simple Q&A, pure chat, immediate execution-only tasks. See "Don't Use When" section for details. Outputs: Structured XML/Markdown prompt, quality score (before/after), optional team brief + per-agent sub-prompts, agent team output files. Success criteria: Single mode quality score ≥ 7/10; Repromptception per-agent prompt quality score 8+/10; all required sections present, actionable and specific.
adaptive-compaction
IncludedAdaptive add-on policy and recovery layer that decides WHEN to compact, prune, snapshot, or fork -- replacing fixed-percent auto-compaction across Claude Code, Codex, and MCP-capable hosts. Trigger on auto-compact timing or damage: "when should I compact", "is it safe to compact now or start a fresh session", "auto-compact fires too early/mid-task", "switching to an unrelated task but the window still has space", "context rot", "answers get worse the longer the session runs", "the agent forgot the plan or my decisions after it summarized", "add a layer on top that manages context without changing the agent", raising autoCompactWindow to give the policy room, or installing/tuning a cross-tool compaction policy or PreCompact hook -- even when "compaction" is never said but the problem is context-window pressure or post-summarization memory loss. Do NOT use to summarize a conversation, build RAG, write a summarization prompt (decides WHEN not HOW), or answer max-context-length trivia.
agent-skill-creator
IncludedCreate cross-platform agent skills from workflow descriptions. Activates when users ask to create an agent, automate a repetitive workflow, create a custom skill, or need advanced agent creation. Triggers on phrases like create agent for, automate workflow, create skill for, every day I have to, daily I need to, turn process into agent, need to automate, create a cross-platform skill, validate this skill, export this skill, migrate this skill. Supports single skills, multi-agent suites, transcript processing, template-based creation, interactive configuration, cross-platform export, and spec validation.
llm-wiki
IncludedUse when building or maintaining a persistent personal knowledge base (second brain) in Obsidian where an LLM incrementally ingests sources, updates entity/concept pages, maintains cross-references, and keeps a synthesis current. Triggers include "second brain", "Obsidian wiki", "personal knowledge management", "ingest this paper/article/book", "build a research wiki", "compound knowledge", "Memex", or whenever the user wants knowledge to accumulate across sessions instead of being re-derived by RAG on every query.
skill-master
IncludedAgent Skills authoring, evaluation, and optimization. Create, edit, validate, benchmark, and improve skills following the agentskills.io specification. Use when designing SKILL.md files, structuring skill folders (references, scripts, assets), ingesting external documentation into skills, running trigger evals, benchmarking skill quality, optimizing descriptions, or performing blind A/B comparisons. Keywords: agentskills.io, SKILL.md, skill authoring, eval, benchmark, trigger optimization.