From 5062d30c08f6ede9e6c39174537bb61280e7c23d Mon Sep 17 00:00:00 2001 From: HiDeoo Date: Wed, 4 Sep 2024 23:36:35 +0200 Subject: Ensure unhandled directives are restored without any extra whitespace (#2281) --- .changeset/ninety-spies-count.md | 5 +++++ .../starlight/__tests__/remark-rehype/asides.test.ts | 19 +++++++++++-------- packages/starlight/integrations/asides.ts | 18 ++++++++++++++---- 3 files changed, 30 insertions(+), 12 deletions(-) create mode 100644 .changeset/ninety-spies-count.md diff --git a/.changeset/ninety-spies-count.md b/.changeset/ninety-spies-count.md new file mode 100644 index 00000000..da37022f --- /dev/null +++ b/.changeset/ninety-spies-count.md @@ -0,0 +1,5 @@ +--- +'@astrojs/starlight': patch +--- + +Fixes a potential text rendering issue that could include extra whitespaces for text containing colons. diff --git a/packages/starlight/__tests__/remark-rehype/asides.test.ts b/packages/starlight/__tests__/remark-rehype/asides.test.ts index 327766e4..88110f68 100644 --- a/packages/starlight/__tests__/remark-rehype/asides.test.ts +++ b/packages/starlight/__tests__/remark-rehype/asides.test.ts @@ -217,20 +217,25 @@ test('transforms back unhandled text directives', async () => { `This is a:test of a sentence with a text:name[content]{key=val} directive.` ); expect(res.code).toMatchInlineSnapshot(` - "
This is a:test - of a sentence with a text:name[content]{key="val"} - directive.
" + "This is a:test of a sentence with a text:name[content]{key="val"} directive.
" `); }); test('transforms back unhandled leaf directives', async () => { const res = await processor.render(`::video[Title]{v=xxxxxxxxxxx}`); expect(res.code).toMatchInlineSnapshot(` - "::video[Title]{v="xxxxxxxxxxx"} -
" + "::video[Title]{v="xxxxxxxxxxx"}
" `); }); +test('does not add any whitespace character after any unhandled directive', async () => { + const res = await processor.render(`## Environment variables (astro:env)`); + expect(res.code).toMatchInlineSnapshot( + `"This is a:test - of a sentence with a TEXT FROM REMARK PLUGIN directive handled by another remark plugin and some other text:name[content]{key="val"} - directives not handled by any plugin.
" + "This is a:test of a sentence with a TEXT FROM REMARK PLUGIN directive handled by another remark plugin and some other text:name[content]{key="val"} directives not handled by any plugin.
" `); }); diff --git a/packages/starlight/integrations/asides.ts b/packages/starlight/integrations/asides.ts index f98cc090..d98e6a40 100644 --- a/packages/starlight/integrations/asides.ts +++ b/packages/starlight/integrations/asides.ts @@ -64,10 +64,20 @@ function transformUnhandledDirective( index: number, parent: Parent ) { - const textNode = { - type: 'text', - value: toMarkdown(node, { extensions: [directiveToMarkdown()] }), - } as const; + let markdown = toMarkdown(node, { extensions: [directiveToMarkdown()] }); + /** + * `mdast-util-to-markdown` assumes that the tree represents a complete document (as it's an AST + * and not a CST) and to follow the POSIX definition of a line (a sequence of zero or more + * non-