Skip to content

Commit f2f15db

Browse files
committed
adds support for setting tags in fenced code blocks
1 parent 1b1a8e8 commit f2f15db

File tree

5 files changed

+77
-1
lines changed

5 files changed

+77
-1
lines changed

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ as YAML frontmatter.
2121
- [Frontmatter tags](#frontmatter-tags)
2222
- [Metadata file tags](#metadata-file-tags)
2323
- [Heading tags](#heading-tags)
24+
- [Tags in code blocks](#tags-in-code-blocks)
2425
- [Filtering by tag](#filtering-by-tag)
2526
- [Filtering by checked state](#filtering-by-checked-state)
2627
- [Sorting by tag](#sorting-by-tag)
@@ -233,6 +234,43 @@ a pending task | bar | foo | 20241010-foo.md | 20241010
233234
a completed task | baz | foo | 20241010-foo.md | 20241010
234235
```
235236
237+
### Tags in code blocks
238+
239+
YAML fenced code blocks can also be used to set tags at the heading
240+
level when using the `taskparser` language identifier. Given:
241+
242+
~~~markdown
243+
# primary
244+
245+
```taskparser
246+
client: foo
247+
project: bar
248+
```
249+
250+
- [ ] a pending task
251+
252+
## secondary
253+
254+
```taskparser
255+
project: baz
256+
```
257+
258+
- [X] a completed task
259+
~~~
260+
261+
`taskparser` will produce:
262+
263+
```
264+
$ taskparser -t text,client,project /foo/bar
265+
```
266+
267+
```
268+
text | client | project
269+
---- | ------ | -------
270+
a pending task | foo | bar
271+
a completed task | foo | baz
272+
```
273+
236274
### Filtering by tag
237275
238276
`taskparser` accepts filter expression via the `-f` argument:

src/parse.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
import type { Parent, Node, Yaml, ListItem, Text, Heading } from 'mdast';
2+
import type { Parent, Node, Yaml, ListItem, Text, Heading, Code } from 'mdast';
33
import type { TagMap, Task, Worklog, ParseContext, ParseFileContext } from './types.js';
44

55
import { readdir, readFile, stat } from 'node:fs/promises';
@@ -90,6 +90,16 @@ const parseYamlNode = (node: Yaml, ctx: ParseFileContext, item: Task | Worklog |
9090
}
9191
};
9292

93+
const parseCodeNode = (node: Code, ctx: ParseFileContext, item: Task | Worklog | null) => {
94+
if (node.lang === 'taskparser' && ctx.heading) {
95+
try {
96+
extractTagsFromYaml(node.value, ctx.heading.tags);
97+
} catch (err) {
98+
throw new Error(`could not parse YAML code block in file ${ctx.file}: ${(err as Error).message}`);
99+
}
100+
}
101+
};
102+
93103
const parseNode = (node: Node, ctx: ParseFileContext, item: Task | Worklog | null) => {
94104
switch (node.type) {
95105
case 'yaml':
@@ -101,6 +111,9 @@ const parseNode = (node: Node, ctx: ParseFileContext, item: Task | Worklog | nul
101111
case 'heading':
102112
parseHeadingNode(node as Heading, ctx, item);
103113
break;
114+
case 'code':
115+
parseCodeNode(node as Code, ctx, item);
116+
break;
104117
default:
105118
if ('children' in node) {
106119
parseParentNode(node as Parent, ctx, item);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
# primary
3+
4+
```taskparser
5+
client: foo
6+
project: bar
7+
```
8+
9+
- [ ] a pending task
10+
11+
## secondary
12+
13+
```taskparser
14+
project: baz
15+
```
16+
17+
- [X] a completed task
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"description": "should parse frontmatter tags",
3+
"argv": ["-t", "text,date,client,project"]
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
text | date | client | project
2+
---- | ---- | ------ | -------
3+
a pending task | 20240101 | foo | bar
4+
a completed task | 20240101 | foo | baz

0 commit comments

Comments
 (0)