Skip to content

Commit

Permalink
Add support for specifying the path inline including mode sigils
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
TomasHubelbauer committed Oct 28, 2024
1 parent 88d99a4 commit 50fe1c7
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 22 deletions.
78 changes: 78 additions & 0 deletions extractBlocks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,3 +492,81 @@ test(
}
])
);

test(
'path: internal, sigil: create, meta: no',
() => expect(extractBlocks('test\n```txt ..file-name.ext\ntest\n```\n')).toEqual([
{
tag: 'txt',
meta: '',
code: 'test\n',
path: 'file-name.ext',
mode: 'create'
}
])
);

test(
'path: internal, sigil: append, meta: no',
() => expect(extractBlocks('test\n```txt !!file-name.ext\ntest\n```\n')).toEqual([
{
tag: 'txt',
meta: '',
code: 'test\n',
path: 'file-name.ext',
mode: 'append'
}
])
);

test(
'path: internal, sigil: match, meta: no',
() => expect(extractBlocks('test\n```txt ??file-name.ext\ntest\n```\n')).toEqual([
{
tag: 'txt',
meta: '',
code: 'test\n',
path: 'file-name.ext',
mode: 'match'
}
])
);

test(
'path: internal, sigil: create, meta: yes',
() => expect(extractBlocks('test\n```txt ..file-name.ext test\ntest\n```\n')).toEqual([
{
tag: 'txt',
meta: 'test',
code: 'test\n',
path: 'file-name.ext',
mode: 'create'
}
])
);

test(
'path: internal, sigil: append, meta: yes',
() => expect(extractBlocks('test\n```txt !!file-name.ext test\ntest\n```\n')).toEqual([
{
tag: 'txt',
meta: 'test',
code: 'test\n',
path: 'file-name.ext',
mode: 'append'
}
])
);

test(
'path: internal, sigil: match, meta: yes',
() => expect(extractBlocks('test\n```txt ??file-name.ext test\ntest\n```\n')).toEqual([
{
tag: 'txt',
meta: 'test',
code: 'test\n',
path: 'file-name.ext',
mode: 'match'
}
])
);
30 changes: 30 additions & 0 deletions extractBlocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export default function extractBlocks(text: string) {
| 'block-tag'
| 'block-meta-or-mode'
| 'block-meta-after-mode'
| 'block-path'
| 'block-meta'
| 'code-line-start'
| 'code-line'
Expand Down Expand Up @@ -206,6 +207,11 @@ export default function extractBlocks(text: string) {
state = 'code-line-start';
break;
}
case '.': {
mode = 'create';
state = 'block-meta-after-mode';
break;
}
case '!': {
mode = 'append';
state = 'block-meta-after-mode';
Expand All @@ -231,6 +237,12 @@ export default function extractBlocks(text: string) {
state = 'code-line-start';
break;
}
case '.':
case '!':
case '?': {
state = 'block-path';
break;
}
case ' ': {
state = 'block-meta';
break;
Expand All @@ -256,6 +268,24 @@ export default function extractBlocks(text: string) {

break;
}
case 'block-path': {
switch (character) {
case '\n': {
state = 'code-line-start';
break;
}
case ' ': {
state = 'block-meta';
break;
}
default: {
path += character;
break;
}
}

break;
}
case 'block-meta': {
switch (character) {
case '\n': {
Expand Down
30 changes: 8 additions & 22 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,31 +148,17 @@ Do this in a Bun-friendly way, something easy, not a PITA.

Clear the terminal in between runs.

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

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

~~~
`file-name.ext`:
```
test
```
~~~

Maybe something like this:

~~~
```txt file-name.txt. meta
```
~~~
Doing it this way simplifies the parser implementation but makes for a worse
user experience.
It is a worthwhile trade-off for now, but should be improved down the line.

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

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

Expand Down

0 comments on commit 50fe1c7

Please sign in to comment.