Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests and functionality for handling double quotes in file names in FileTree #2641

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions docs/src/content/docs/components/file-tree.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,49 @@ import { FileTree } from '@astrojs/starlight/components';

</Preview>

### Escape white-space in file names

To include white-space in a file or directory name, escape it with double quotes.

<Preview>

```mdx {5,6}
import { FileTree } from '@astrojs/starlight/components';

<FileTree>

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

</FileTree>
```

<Fragment slot="markdoc">

```markdoc {2,3}
{% filetree %}
- "main folder/" file <strong>container</strong>
- "main folder/" file container
- "The Header.astro" main file
- …
{% /filetree %}
```

</Fragment>

<FileTree slot="preview">

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

</FileTree>

</Preview>

## `<FileTree>` Props

**Implementation:** [`FileTree.astro`](https://github.com/withastro/starlight/blob/main/packages/starlight/user-components/FileTree.astro)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,78 @@ describe('processor', () => {
expect(extractFileTree(html)).toMatchFileSnapshot('./snapshots/file-tree-comment-nodes.html');
});

test('double 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('double 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('double 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('double 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('double 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('double 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('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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<ul><li class="file"><span class="tree-entry"><span class=""><span><svg></svg></span>file name</span> <span class="comment">with <strong>important</strong> comments</span></span></li></ul>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<ul><li class="file"><span class="tree-entry"><span class=""><span><svg></svg></span>file name</span> <span class="comment">with some comments</span></span></li></ul>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<ul><li class="file"><span class="tree-entry"><span class=""><span><svg></svg></span>file name</span></span></li></ul>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<ul><li class="directory"><details><summary><span class="tree-entry"><span class=""><span><span class="sr-only">Directory</span><svg></svg></span>folder name/</span> <span class="comment">with <strong>important</strong> comments</span></span></summary><ul><li class="file empty"><span class="tree-entry"><span class="">…</span></span></li></ul></details></li></ul>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<ul><li class="directory"><details><summary><span class="tree-entry"><span class=""><span><span class="sr-only">Directory</span><svg></svg></span>folder name/</span> <span class="comment">with some comments</span></span></summary><ul><li class="file empty"><span class="tree-entry"><span class="">…</span></span></li></ul></details></li></ul>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<ul><li class="directory"><details><summary><span class="tree-entry"><span class=""><span><span class="sr-only">Directory</span><svg></svg></span>folder name/</span></span></summary><ul><li class="file empty"><span class="tree-entry"><span class="">…</span></span></li></ul></details></li></ul>
28 changes: 23 additions & 5 deletions packages/starlight/user-components/rehype-file-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,10 @@ const fileTreeProcessor = rehype()

// Extract text comment that follows the file name, e.g. `README.md This is a comment`
if (firstChild?.type === 'text') {
const [filename, ...fragments] = firstChild.value.split(' ');
firstChild.value = filename || '';
const textComment = fragments.join(' ').trim();
if (textComment.length > 0) {
comment.push(fragments.join(' '));
const [filename, textComment] = splitFileNameAndTextComments(firstChild.value);
firstChild.value = filename;
if (textComment.trim().length > 0) {
comment.push(textComment);
}
}

Expand Down Expand Up @@ -244,6 +243,25 @@ function throwFileTreeValidationError(message: string): never {
);
}

function splitFileNameAndTextComments(value: string): [string, string] {
if (!value || value.trim() === '') return ['', ''];

// Handles the case where the file name is wrapped in double quotes, e.g. `"READ ME.md" This is a comment`.
if (value.startsWith('"')) {
const match = value.match(/^"([^"]+)"\s*(.*)$/);
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];
}

export interface Definitions {
files: Record<string, string>;
extensions: Record<string, string>;
Expand Down