Skip to content

Commit d799ed9

Browse files
authored
Merge pull request #1283 from harehare/feat/section-ops-examples
✨ feat(section): Add section operations and examples
2 parents 8fa735f + 1e33286 commit d799ed9

File tree

4 files changed

+209
-1
lines changed

4 files changed

+209
-1
lines changed

crates/mq-lang/modules/section.mq

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
def _is_section(section): is_dict(section) && section[:type] == :section;
22

3+
# Returns sections whose title contains the specified pattern.
4+
def section(md_nodes, pattern):
5+
sections(md_nodes) | title_contains(pattern) | flatten()
6+
end
7+
38
# Splits markdown nodes into sections based on headers.
49
def sections(md_nodes):
510
if (is_empty(md_nodes)):
@@ -182,7 +187,14 @@ end
182187

183188
# Flattens sections back to markdown nodes for output.
184189
# This converts section objects back to their original markdown node arrays.
190+
# Deprecated: use `flatten` instead.
185191
def collect(sections):
192+
flatten(sections)
193+
end
194+
195+
# Flattens sections back to markdown nodes for output.
196+
# This converts section objects back to their original markdown node arrays.
197+
def flatten(sections):
186198
if (is_empty(sections)):
187199
[]
188200
else:
@@ -193,3 +205,4 @@ def collect(sections):
193205
section
194206
end)
195207
end
208+

docs/books/src/start/example.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,116 @@ $ mq 'sitemap(__FILE__, "https://example.com")' docs/**/*.md
209209
</url>
210210
```
211211

212+
## Section Operations
213+
214+
### Extract Sections by Title
215+
216+
Extract sections whose title contains a specific text:
217+
218+
```mq
219+
include "section" | nodes | section("Installation")
220+
```
221+
222+
**Input example**:
223+
224+
```markdown
225+
# Introduction
226+
227+
Welcome to the project.
228+
229+
## Installation
230+
231+
Run the following command:
232+
233+
## Usage
234+
235+
Use the tool like this.
236+
```
237+
238+
**Output**: Returns the "Installation" section with its header and content.
239+
240+
### Split Document by Header Level
241+
242+
Split a document into sections based on header level:
243+
244+
```mq
245+
include "section" | nodes | split(2)
246+
```
247+
248+
**Input example**:
249+
250+
```markdown
251+
# Main Title
252+
253+
## Section 1
254+
255+
Content of section 1.
256+
257+
## Section 2
258+
259+
Content of section 2.
260+
261+
## Section 3
262+
263+
Content of section 3.
264+
```
265+
266+
**Output**: Returns an array of section objects split at `##` headers.
267+
268+
### Generate Table of Contents from Sections
269+
270+
Generate a table of contents from sections:
271+
272+
```mq
273+
include "section" | nodes | sections() | toc()
274+
```
275+
276+
**Input example**:
277+
278+
```markdown
279+
# Introduction
280+
281+
Some text.
282+
283+
## Getting Started
284+
285+
More text.
286+
287+
### Prerequisites
288+
289+
Details here.
290+
291+
## Advanced Usage
292+
293+
Advanced content.
294+
```
295+
296+
**Output**: `["- Introduction", " - Getting Started", " - Prerequisites", " - Advanced Usage"]`
297+
298+
### Filter Sections with Content
299+
300+
Filter sections that have content beyond the header:
301+
302+
```mq
303+
include "section" | nodes | sections() | filter(fn(s): has_content(s);) | titles()
304+
```
305+
306+
**Input example**:
307+
308+
```markdown
309+
# Introduction
310+
311+
Welcome to the project.
312+
313+
## Empty Section
314+
315+
## Usage
316+
317+
Use the tool like this.
318+
```
319+
320+
**Output**: `["Introduction", "Usage"]`
321+
212322
## Custom Functions and Programming
213323

214324
### Define Custom Function

docs/books/src/start/install.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Invoke-WebRequest -Uri https://github.com/harehare/mq/releases/download/v0.5.15/
4747

4848
```sh
4949
# Using Homebrew (macOS and Linux)
50-
$ brew install harehare/tap/mq
50+
$ brew install mq
5151
```
5252

5353
## Docker

packages/mq-playground/src/examples.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,91 @@ end
254254
- [Chapter2](Chapter2.md)
255255
- [Chapter3](Chapter3.md)
256256
- [Chapter4](Chapter4.md)
257+
`,
258+
isUpdate: false,
259+
format: "markdown",
260+
},
261+
],
262+
},
263+
{
264+
name: "Section Operations",
265+
examples: [
266+
{
267+
name: "Extract section by title",
268+
code: `include "section" | nodes | section("Installation")`,
269+
markdown: `# Introduction
270+
271+
Welcome to the project.
272+
273+
## Installation
274+
275+
Run the following command:
276+
277+
\`\`\`bash
278+
npm install mq
279+
\`\`\`
280+
281+
## Usage
282+
283+
Use the tool like this.
284+
`,
285+
isUpdate: false,
286+
format: "markdown",
287+
},
288+
{
289+
name: "Split by header level",
290+
code: `include "section" | nodes | split(2) | titles()`,
291+
markdown: `# Main Title
292+
293+
## Section 1
294+
295+
Content of section 1.
296+
297+
## Section 2
298+
299+
Content of section 2.
300+
301+
## Section 3
302+
303+
Content of section 3.
304+
`,
305+
isUpdate: false,
306+
format: "markdown",
307+
},
308+
{
309+
name: "Table of contents",
310+
code: `include "section" | nodes | sections() | toc()`,
311+
markdown: `# Introduction
312+
313+
Some text.
314+
315+
## Getting Started
316+
317+
More text.
318+
319+
### Prerequisites
320+
321+
Details here.
322+
323+
## Advanced Usage
324+
325+
Advanced content.
326+
`,
327+
isUpdate: false,
328+
format: "markdown",
329+
},
330+
{
331+
name: "Filter sections with content",
332+
code: `include "section" | nodes | sections() | filter(fn(s): has_content(s);) | titles()`,
333+
markdown: `# Introduction
334+
335+
Welcome to the project.
336+
337+
## Empty Section
338+
339+
## Usage
340+
341+
Use the tool like this.
257342
`,
258343
isUpdate: false,
259344
format: "markdown",

0 commit comments

Comments
 (0)