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 more diagnostic help to error messages thrown by <Steps> #1838

Merged
merged 4 commits into from
May 7, 2024
Merged
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
5 changes: 5 additions & 0 deletions .changeset/breezy-cats-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/starlight': patch
---

Adds extra information to the errors thrown by the `<Steps>` component to help locate misformatted code
28 changes: 24 additions & 4 deletions packages/starlight/__tests__/remark-rehype/rehype-steps.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,36 @@ test('component with non-`<ol>` content throws an error', () => {
"[AstroUserError]:
The \`<Steps>\` component expects its content to be a single ordered list (\`<ol>\`) but found the following element: \`<p>\`.
Hint:
To learn more about the \`<Steps>\` component, see https://starlight.astro.build/guides/components/#steps"
To learn more about the \`<Steps>\` component, see https://starlight.astro.build/guides/components/#steps

Full HTML passed to \`<Steps>\`:

<p>A paragraph is not an ordered list</p>
"
`);
});

test('component with multiple children throws an error', () => {
expect(() => processSteps('<ol></ol><ol></ol>')).toThrowErrorMatchingInlineSnapshot(`
expect(() =>
processSteps(
'<ol><li>List item</li></ol><p>I intended this to be part of the same list item</p><ol><li>Other list item</li></ol>'
)
).toThrowErrorMatchingInlineSnapshot(`
"[AstroUserError]:
The \`<Steps>\` component expects its content to be a single ordered list (\`<ol>\`) but found multiple child elements: \`<ol>\`, \`<ol>\`.
The \`<Steps>\` component expects its content to be a single ordered list (\`<ol>\`) but found multiple child elements: \`<ol>\`, \`<p>\`, \`<ol>\`.
Hint:
To learn more about the \`<Steps>\` component, see https://starlight.astro.build/guides/components/#steps"
To learn more about the \`<Steps>\` component, see https://starlight.astro.build/guides/components/#steps

Full HTML passed to \`<Steps>\`:

<ol>
<li>List item</li>
</ol>
<p>I intended this to be part of the same list item</p>
<ol>
<li>Other list item</li>
</ol>
"
`);
});

Expand Down
1 change: 1 addition & 0 deletions packages/starlight/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@
"mdast-util-to-markdown": "^2.1.0",
"pagefind": "^1.0.3",
"rehype": "^13.0.1",
"rehype-format": "^5.0.0",
"remark-directive": "^3.0.0",
"unified": "^11.0.4",
"unist-util-visit": "^5.0.0",
Expand Down
26 changes: 18 additions & 8 deletions packages/starlight/user-components/rehype-steps.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { AstroError } from 'astro/errors';
import type { Element, Root } from 'hast';
import { rehype } from 'rehype';
import rehypeFormat from 'rehype-format';
import type { VFile } from 'vfile';

const prettyPrintProcessor = rehype().data('settings', { fragment: true }).use(rehypeFormat);
const prettyPrintHtml = (html: string) =>
prettyPrintProcessor.processSync({ value: html }).toString();

const stepsProcessor = rehype()
.data('settings', { fragment: true })
.use(function steps() {
return (tree: Root) => {
return (tree: Root, vfile: VFile) => {
const rootElements = tree.children.filter((item): item is Element => item.type === 'element');
const [rootElement] = rootElements;

Expand All @@ -17,12 +23,14 @@ const stepsProcessor = rehype()
throw new StepsError(
'The `<Steps>` component expects its content to be a single ordered list (`<ol>`) but found multiple child elements: ' +
rootElements.map((element: Element) => `\`<${element.tagName}>\``).join(', ') +
'.'
'.',
vfile.value.toString()
);
} else if (rootElement.tagName !== 'ol') {
throw new StepsError(
'The `<Steps>` component expects its content to be a single ordered list (`<ol>`) but found the following element: ' +
`\`<${rootElement.tagName}>\`.`
`\`<${rootElement.tagName}>\`.`,
vfile.value.toString()
);
}

Expand All @@ -49,10 +57,12 @@ export const processSteps = (html: string | undefined) => {
};

class StepsError extends AstroError {
constructor(message: string) {
super(
message,
'To learn more about the `<Steps>` component, see https://starlight.astro.build/guides/components/#steps'
);
constructor(message: string, html?: string) {
let hint =
'To learn more about the `<Steps>` component, see https://starlight.astro.build/guides/components/#steps';
if (html) {
hint += '\n\nFull HTML passed to `<Steps>`:\n' + prettyPrintHtml(html);
}
super(message, hint);
}
}
53 changes: 53 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading