Skip to content

Commit

Permalink
Enhance file tree documentation and tests for handling special quotes…
Browse files Browse the repository at this point in the history
… in file and folder names
  • Loading branch information
iskandersierra committed Nov 28, 2024
1 parent 1292ae2 commit 2228547
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
15 changes: 9 additions & 6 deletions docs/src/content/docs/components/file-tree.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,9 @@ import { FileTree } from '@astrojs/starlight/components';

<FileTree>

- "main folder/"
- "The Header.astro"
- "main folder/" file <strong>container</strong>
- "main folder/" file container
- "The Header.astro" main file
-

</FileTree>
Expand All @@ -240,8 +241,9 @@ import { FileTree } from '@astrojs/starlight/components';

```markdoc {2,3}
{% filetree %}
- "main folder/"
- "The Header.astro"
- "main folder/" file <strong>container</strong>
- "main folder/" file container
- "The Header.astro" main file
- …
{% /filetree %}
```
Expand All @@ -250,8 +252,9 @@ import { FileTree } from '@astrojs/starlight/components';

<FileTree slot="preview">

- "main folder/"
- "The Header.astro"
- "main folder/" file <strong>container</strong>
- "main folder/" file container
- "The Header.astro" main file
-

</FileTree>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,42 @@ describe('processor', () => {
expect(extractFileTree(html)).toMatchFileSnapshot('./snapshots/file-tree-double-quotes-folder-node.html');
});

test('special quotes allows to add white-space to file names', () => {
const html = processTestFileTree(`<ul><li>“file name”</li></ul>`);

expect(extractFileTree(html)).toMatchFileSnapshot('./snapshots/file-tree-double-quotes-file.html');
});

test('special quotes file name with text comments', () => {
const html = processTestFileTree(`<ul><li>“file name” with some comments</li></ul>`);

expect(extractFileTree(html)).toMatchFileSnapshot('./snapshots/file-tree-double-quotes-file-text.html');
});

test('special quotes file name with node comments', () => {
const html = processTestFileTree(`<ul><li>“file name” with <strong>important</strong> comments</li></ul>`);

expect(extractFileTree(html)).toMatchFileSnapshot('./snapshots/file-tree-double-quotes-file-node.html');
});

test('special quotes allows to add white-space to folder names', () => {
const html = processTestFileTree(`<ul><li>“folder name/”</li></ul>`);

expect(extractFileTree(html)).toMatchFileSnapshot('./snapshots/file-tree-double-quotes-folder.html');
});

test('special quotes folder name with text comments', () => {
const html = processTestFileTree(`<ul><li>“folder name/” with some comments</li></ul>`);

expect(extractFileTree(html)).toMatchFileSnapshot('./snapshots/file-tree-double-quotes-folder-text.html');
});

test('special quotes folder name with node comments', () => {
const html = processTestFileTree(`<ul><li>“folder name/” with <strong>important</strong> comments</li></ul>`);

expect(extractFileTree(html)).toMatchFileSnapshot('./snapshots/file-tree-double-quotes-folder-node.html');
});

test('identifies directory with either a file name ending with a slash or a nested list', () => {
const html = processTestFileTree(`<ul>
<li>directory/</li>
Expand Down
5 changes: 5 additions & 0 deletions packages/starlight/user-components/rehype-file-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,11 @@ function splitFileNameAndTextComments(value: string): [string, string] {
if (match) return [match[1]!, match[2]!];
}

if (value.startsWith('“')) {
const match = value.match(/^“([^”]+)”\s*(.*)$/);
if (match) return [match[1]!, match[2]!];
}

const [filename, ...fragments] = value.split(' ');
const textComments = fragments.join(' ');
return [filename!, textComments];
Expand Down

0 comments on commit 2228547

Please sign in to comment.