@udx/mq 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json ADDED
@@ -0,0 +1,67 @@
1
+ {
2
+ "name": "@udx/mq",
3
+ "version": "0.1.1",
4
+ "description": "Markdown Query - jq for Markdown documents",
5
+ "main": "mq.js",
6
+ "type": "module",
7
+ "bin": {
8
+ "mq": "./mq.js"
9
+ },
10
+ "scripts": {
11
+ "test": "NODE_OPTIONS=--experimental-vm-modules npx mocha",
12
+ "test:gist": "NODE_OPTIONS=--experimental-vm-modules npx mocha test/gist-integration.test.mjs",
13
+ "test:integration": "NODE_OPTIONS=--experimental-vm-modules npx mocha test/integration.test.mjs",
14
+ "test:all": "npm test",
15
+ "start": "node mq.js",
16
+ "prepublishOnly": "npm test"
17
+ },
18
+ "keywords": [
19
+ "markdown",
20
+ "query",
21
+ "jq",
22
+ "transform",
23
+ "documentation",
24
+ "cli",
25
+ "toc",
26
+ "headings",
27
+ "code-blocks",
28
+ "links",
29
+ "analysis"
30
+ ],
31
+ "author": "UDX",
32
+ "license": "MIT",
33
+ "repository": {
34
+ "type": "git",
35
+ "url": "git+https://github.com/andypotanin/udx.dev.git",
36
+ "directory": "tools/mq"
37
+ },
38
+ "bugs": {
39
+ "url": "https://github.com/andypotanin/udx.dev/issues"
40
+ },
41
+ "homepage": "https://github.com/andypotanin/udx.dev/tree/main/tools/mq#readme",
42
+ "files": [
43
+ "mq.js",
44
+ "README.md",
45
+ "lib/**/*.js",
46
+ "examples"
47
+ ],
48
+ "engines": {
49
+ "node": ">=14.16"
50
+ },
51
+ "dependencies": {
52
+ "commander": "^11.1.0",
53
+ "js-yaml": "^4.1.0",
54
+ "mdast-util-from-markdown": "^1.3.0",
55
+ "mdast-util-gfm": "^3.1.0",
56
+ "mdast-util-to-markdown": "^1.5.0",
57
+ "mdast-util-to-string": "^3.2.0",
58
+ "micromark-extension-gfm": "^3.0.0",
59
+ "unist-util-visit": "^4.1.1"
60
+ },
61
+ "peerDependencies": {
62
+ "@udx/mcurl": "^0.3.0"
63
+ },
64
+ "devDependencies": {
65
+ "mocha": "^11.1.0"
66
+ }
67
+ }
package/readme.md ADDED
@@ -0,0 +1,242 @@
1
+ # mq - Markdown Query
2
+
3
+ A powerful tool for querying, transforming, and analyzing markdown documents, designed as an extension for [@udx/mcurl](../mcurl).
4
+
5
+ ## Quick Start
6
+
7
+ Installing mq
8
+
9
+ ```bash
10
+ npm install -g @udx/mq
11
+ ```
12
+
13
+ Test installation:
14
+
15
+ ```bash
16
+ mq --version
17
+ ```
18
+
19
+ ### Basic Usage
20
+
21
+ ```bash
22
+ # Extract all headings
23
+ mq '.headings[]' -i ../../README.md
24
+
25
+ # Get the table of contents
26
+ mq '.toc' -i ../../README.md
27
+
28
+ # Extract all code blocks with their language
29
+ mq '.codeBlocks[] | {language, content}' -i ../../README.md
30
+
31
+ # Make all code blocks collapsible
32
+ mq -t '.codeBlocks[] |= makeCollapsible' -i ../../README.md
33
+
34
+ # Analyze document structure
35
+ mq --analyze -i ../../README.md
36
+
37
+ # Show document structure (headings hierarchy)
38
+ mq --structure -i ../../README.md
39
+
40
+ # Count document elements
41
+ mq --count -i ../../README.md
42
+ ```
43
+
44
+ ### Using Piped Input
45
+
46
+ You can also use mq with piped input from the tools/mq directory:
47
+
48
+ ```bash
49
+ cat ../../README.md | mq '.headings[]'
50
+ ```
51
+
52
+ ## Concept
53
+
54
+ Just as `jq` allows you to query and transform JSON data, `mq` provides a similar experience for markdown documents. It parses markdown into a structured object that you can query, transform, and analyze using a familiar syntax.
55
+
56
+ ## Features
57
+
58
+ - Query markdown documents with a jq-like syntax
59
+ - Transform markdown content with various operations
60
+ - Extract specific elements like headings, code blocks, links
61
+ - Generate table of contents
62
+ - Analyze document structure and content
63
+ - Count document elements
64
+ - Integration with mcurl
65
+
66
+ ## Query Syntax
67
+
68
+ mq uses a simplified jq-like syntax for querying markdown elements:
69
+
70
+ ```bash
71
+ # Basic element selection
72
+ .headings[] # All headings
73
+ .headings[0] # First heading
74
+ .headings[] | select(.level == 2) # All h2 headings
75
+ .links[] # All links
76
+ .codeBlocks[] # All code blocks
77
+ .toc # Table of contents
78
+
79
+ # Filtering and transformations
80
+ .headings[] | select(.text | contains("Introduction")) # Headings containing "Introduction"
81
+ .codeBlocks[] | select(.language == "javascript") # JavaScript code blocks
82
+ .links[] | select(.href | startswith("http")) # External links
83
+ ```
84
+
85
+ ## Transform Operations
86
+
87
+ Transform operations modify the markdown structure:
88
+
89
+ ```bash
90
+ # Make code blocks collapsible
91
+ mq -t '.codeBlocks[] |= makeCollapsible' -i test/fixtures/content-website.md
92
+
93
+ # Add a descriptive TOC
94
+ mq -t '.toc |= makeDescriptive' -i test/fixtures/content-website.md
95
+
96
+ # Add cross-links section after TOC
97
+ mq -t '.toc |= addCrossLinks(["worker.md", "platform.md"])' -i test/fixtures/content-website.md
98
+
99
+ # Fix heading hierarchy
100
+ mq -t '.headings |= fixHierarchy' -i test/fixtures/content-website.md
101
+ ```
102
+
103
+ ## Analysis Features
104
+
105
+ mq provides powerful analysis capabilities:
106
+
107
+ ```bash
108
+ # Analyze document structure and content
109
+ mq --analyze -i test/fixtures/content-website.md
110
+
111
+ # Show document structure (headings hierarchy)
112
+ mq --structure -i test/fixtures/content-website.md
113
+
114
+ # Count document elements
115
+ mq --count -i test/fixtures/content-website.md
116
+ ```
117
+
118
+ ## Piping with Other Tools
119
+
120
+ mq works seamlessly with Unix pipes and other tools:
121
+
122
+ ```bash
123
+ # Find markdown files with inconsistent heading hierarchy
124
+ find ../../content/ -name "*.md" | xargs -I{} sh -c 'cat {} | mq ".headings | validateHierarchy" || echo "Issue in {}"'
125
+
126
+ # Extract all external links from documentation
127
+ find ../../content/ -name "*.md" | xargs cat | mq '.links[] | select(.href | startswith("http")) | .href' | sort | uniq
128
+
129
+ # Generate a combined TOC from multiple files
130
+ find ../../content/architecture/ -name "*.md" | xargs cat | mq '.headings[] | select(.level <= 2) | {file: input_filename, heading: .text}'
131
+ ```
132
+
133
+ ## Integration with mCurl
134
+
135
+ Use mq as a content handler extension for mCurl:
136
+
137
+ ```javascript
138
+ import { registerContentHandler } from '@udx/mcurl';
139
+ import { markdownHandler } from '@udx/mq';
140
+
141
+ // Register the markdown handler with mcurl
142
+ registerContentHandler('text/markdown', markdownHandler);
143
+
144
+ // Fetch and transform markdown content
145
+ const result = await mcurl('https://udx.io', {
146
+ mqQuery: '.headings[] | select(.level == 2) | .text'
147
+ });
148
+ ```
149
+
150
+ ## Common Use Cases
151
+
152
+ ### 1. Documentation Analysis
153
+
154
+ ```bash
155
+ # Find documents missing a proper TOC
156
+ find test/fixtures/ -name "*.md" | xargs -I{} sh -c 'cat {} | mq ".toc | length" | grep -q "^0$" && echo "{} missing TOC"'
157
+
158
+ # List all unique tags used in documentation
159
+ find test/fixtures/ -name "*.md" | xargs cat | mq '.tags[]?' | sort | uniq -c | sort -nr
160
+
161
+ # Analyze content distribution
162
+ find test/fixtures/ -name "*.md" | xargs -I{} sh -c 'cat {} | mq --analyze > {}.analysis.md'
163
+ ```
164
+
165
+ ### 2. Batch Transformations
166
+
167
+ ```bash
168
+ # Add cross-links between related documents
169
+ for file in test/fixtures/*.md; do
170
+ cat $file | mq -t '.toc |= addCrossLinks(["worker.md", "platform.md"])' > $file.new
171
+ mv $file.new $file
172
+ done
173
+
174
+ # Make all code blocks collapsible
175
+ find test/fixtures/ -name "*.md" | xargs -I{} sh -c 'cat {} | mq -t ".codeBlocks[] |= makeCollapsible" > {}.new && mv {}.new {}'
176
+ ```
177
+
178
+ ### 3. Documentation Validation
179
+
180
+ ```bash
181
+ # Validate all links are working
182
+ find test/fixtures/ -name "*.md" | xargs cat | mq '.links[] | .href' | xargs -I{} curl -s -o /dev/null -w "%{http_code} {}\n" {} | grep -v "^200"
183
+
184
+ # Check for consistent heading structure
185
+ find test/fixtures/ -name "*.md" | xargs -I{} sh -c 'cat {} | mq ".headings | validateHierarchy" || echo "Issue in {}"'
186
+ ```
187
+
188
+ ## Examples
189
+
190
+ See the [examples](./examples) directory for more usage examples.
191
+
192
+ ## Document Comparison Examples
193
+
194
+ Use `mq` to compare and analyze differences between architecture documents. Run these commands from the tools/mq directory:
195
+
196
+ ```bash
197
+
198
+ # Compare document statistics between files
199
+ mq --count -i test/fixtures/stateless.md | grep 'Headings\|Paragraphs\|Links\|CodeBlocks'
200
+
201
+ # Find unique sections in a specific document (e.g., mobile credential-related sections)
202
+ mq '.headings[] | select(.text | contains("Mobile Credential"))' -i test/fixtures/stateless.md
203
+
204
+ # Compare content distribution to identify most detailed sections
205
+ mq --analyze -i test/fixtures/stateless.md | grep -A20 'Content Distribution'
206
+
207
+ # Extract links to see different external references
208
+ mq '.links[]' -i test/fixtures/stateless.md
209
+ ```
210
+
211
+ For inline comparison without creating temporary files, use command substitution:
212
+
213
+ ```bash
214
+ # Make sure to run these from the tools/mq directory
215
+
216
+ # Directly compare heading counts
217
+ echo "Stateless: $(mq --count -i test/fixtures/stateless.md | grep 'Headings' | awk '{print $2}')"
218
+
219
+ # Find terms that appear in one document but not the other
220
+ comm -23 <(mq '.headings[].text' -i test/fixtures/stateless.md | sort) \
221
+ <(mq '.headings[].text' -i test/fixtures/cloud-automation-group.md | sort)
222
+ ```
223
+
224
+ ### Document Comparison Script
225
+
226
+ Create a simple shell script to help compare architecture documents:
227
+
228
+ ```bash
229
+ #!/bin/bash
230
+ # Save as tools/mq/compare-architectures.sh and make executable with chmod +x
231
+
232
+ echo "=== Comparing Document Statistics ==="
233
+ echo "Stateless:"
234
+ mq --count -i test/fixtures/stateless.md | grep 'Headings\|Paragraphs\|Links\|CodeBlocks'
235
+
236
+ echo "\n=== Finding Mobile Credential Sections ==="
237
+ mq '.headings[] | select(.text | contains("Mobile") or .text | contains("Credential"))' -i test/fixtures/stateless.md | grep "text"
238
+ ```
239
+
240
+ ## License
241
+
242
+ MIT