Skip to content

Commit 50fe1c7

Browse files
Add support for specifying the path inline including mode sigils
I ended up doing this on easy mode for now which results in the sigils prefixing the path and needed to be doubled up. It will work and I'll be able to improve the parser down the line to make it so that the sigils do not have to be doubled-up.
1 parent 88d99a4 commit 50fe1c7

File tree

3 files changed

+116
-22
lines changed

3 files changed

+116
-22
lines changed

extractBlocks.test.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,3 +492,81 @@ test(
492492
}
493493
])
494494
);
495+
496+
test(
497+
'path: internal, sigil: create, meta: no',
498+
() => expect(extractBlocks('test\n```txt ..file-name.ext\ntest\n```\n')).toEqual([
499+
{
500+
tag: 'txt',
501+
meta: '',
502+
code: 'test\n',
503+
path: 'file-name.ext',
504+
mode: 'create'
505+
}
506+
])
507+
);
508+
509+
test(
510+
'path: internal, sigil: append, meta: no',
511+
() => expect(extractBlocks('test\n```txt !!file-name.ext\ntest\n```\n')).toEqual([
512+
{
513+
tag: 'txt',
514+
meta: '',
515+
code: 'test\n',
516+
path: 'file-name.ext',
517+
mode: 'append'
518+
}
519+
])
520+
);
521+
522+
test(
523+
'path: internal, sigil: match, meta: no',
524+
() => expect(extractBlocks('test\n```txt ??file-name.ext\ntest\n```\n')).toEqual([
525+
{
526+
tag: 'txt',
527+
meta: '',
528+
code: 'test\n',
529+
path: 'file-name.ext',
530+
mode: 'match'
531+
}
532+
])
533+
);
534+
535+
test(
536+
'path: internal, sigil: create, meta: yes',
537+
() => expect(extractBlocks('test\n```txt ..file-name.ext test\ntest\n```\n')).toEqual([
538+
{
539+
tag: 'txt',
540+
meta: 'test',
541+
code: 'test\n',
542+
path: 'file-name.ext',
543+
mode: 'create'
544+
}
545+
])
546+
);
547+
548+
test(
549+
'path: internal, sigil: append, meta: yes',
550+
() => expect(extractBlocks('test\n```txt !!file-name.ext test\ntest\n```\n')).toEqual([
551+
{
552+
tag: 'txt',
553+
meta: 'test',
554+
code: 'test\n',
555+
path: 'file-name.ext',
556+
mode: 'append'
557+
}
558+
])
559+
);
560+
561+
test(
562+
'path: internal, sigil: match, meta: yes',
563+
() => expect(extractBlocks('test\n```txt ??file-name.ext test\ntest\n```\n')).toEqual([
564+
{
565+
tag: 'txt',
566+
meta: 'test',
567+
code: 'test\n',
568+
path: 'file-name.ext',
569+
mode: 'match'
570+
}
571+
])
572+
);

extractBlocks.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default function extractBlocks(text: string) {
1414
| 'block-tag'
1515
| 'block-meta-or-mode'
1616
| 'block-meta-after-mode'
17+
| 'block-path'
1718
| 'block-meta'
1819
| 'code-line-start'
1920
| 'code-line'
@@ -206,6 +207,11 @@ export default function extractBlocks(text: string) {
206207
state = 'code-line-start';
207208
break;
208209
}
210+
case '.': {
211+
mode = 'create';
212+
state = 'block-meta-after-mode';
213+
break;
214+
}
209215
case '!': {
210216
mode = 'append';
211217
state = 'block-meta-after-mode';
@@ -231,6 +237,12 @@ export default function extractBlocks(text: string) {
231237
state = 'code-line-start';
232238
break;
233239
}
240+
case '.':
241+
case '!':
242+
case '?': {
243+
state = 'block-path';
244+
break;
245+
}
234246
case ' ': {
235247
state = 'block-meta';
236248
break;
@@ -256,6 +268,24 @@ export default function extractBlocks(text: string) {
256268

257269
break;
258270
}
271+
case 'block-path': {
272+
switch (character) {
273+
case '\n': {
274+
state = 'code-line-start';
275+
break;
276+
}
277+
case ' ': {
278+
state = 'block-meta';
279+
break;
280+
}
281+
default: {
282+
path += character;
283+
break;
284+
}
285+
}
286+
287+
break;
288+
}
259289
case 'block-meta': {
260290
switch (character) {
261291
case '\n': {

readme.md

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -148,31 +148,17 @@ Do this in a Bun-friendly way, something easy, not a PITA.
148148

149149
Clear the terminal in between runs.
150150

151-
### Support specifying related path in the code block syntax as an alternative
151+
### Improve the parser to not require duplicating mode sigils with inline path
152152

153-
We need an alternative for specifying the related path without printing it in
154-
the document:
153+
Currently `tag ..file-name.ext` is needed to specify a tag and the inline path
154+
while also distinguishing it from the meta.
155155

156-
~~~
157-
`file-name.ext`:
158-
159-
```
160-
test
161-
```
162-
~~~
163-
164-
Maybe something like this:
165-
166-
~~~
167-
```txt file-name.txt. meta
168-
```
169-
~~~
156+
Doing it this way simplifies the parser implementation but makes for a worse
157+
user experience.
158+
It is a worthwhile trade-off for now, but should be improved down the line.
170159

171-
The period signifies the string is the file name and not the first item of the
172-
meta argument list.
173-
Other sigils supported instead of the trailing `.` are `!` to mark as append to
174-
the file and `?` to mark as a check for the file's contents match with the code
175-
block content.
160+
One option to solve this would be to require the paths to start with a period
161+
like in ESM path module specifiers, but I don't like that option too much.
176162

177163
### Respect the file management sigils on `Block` (`append` and `match`)
178164

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy