Skip to content

Commit

Permalink
Fix broken restoration of remark directives. (#2327)
Browse files Browse the repository at this point in the history
Co-authored-by: Chris Swithinbank <swithinbank@gmail.com>
  • Loading branch information
tritao and delucis committed Sep 18, 2024
1 parent bf93994 commit d7a295e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/gold-coats-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/starlight": patch
---

Fixes restoration of remark directives for nodes with custom data attached.
32 changes: 32 additions & 0 deletions packages/starlight/__tests__/remark-rehype/asides.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,35 @@ test('lets remark plugin injected by Starlight plugins handle text and leaf dire
"<p>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.</p>"
`);
});

test('does not transform back directive nodes with data', async () => {
const processor = await createMarkdownProcessor({
remarkPlugins: [
...starlightAsides({
starlightConfig,
astroConfig: {
root: new URL(import.meta.url),
srcDir: new URL('./_src/', import.meta.url),
},
useTranslations,
}),
// A custom remark plugin updating the node with data that should be consumed by rehype.
function customRemarkPlugin() {
return function transformer(tree: Root) {
visit(tree, (node) => {
if (node.type !== 'textDirective') return;
node.data ??= {};
node.data.hName = 'span';
node.data.hProperties = { class: `api` };
});
};
},
remarkDirectivesRestoration,
],
});

const res = await processor.render(`This method is available in the :api[thing] API.`);
expect(res.code).toMatchInlineSnapshot(
`"<p>This method is available in the <span class="api">thing</span> API.</p>"`
);
});
3 changes: 2 additions & 1 deletion packages/starlight/integrations/asides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ export function remarkDirectivesRestoration() {
if (
index !== undefined &&
parent &&
(node.type === 'textDirective' || node.type === 'leafDirective')
(node.type === 'textDirective' || node.type === 'leafDirective') &&
node.data === undefined
) {
transformUnhandledDirective(node, index, parent);
return;
Expand Down

0 comments on commit d7a295e

Please sign in to comment.