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

Allow asides titles to contain markdown formatting #2126

Merged
merged 8 commits into from
Jul 23, 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/mean-tigers-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/starlight': patch
---

Adds support for markdown formatting in aside titles
33 changes: 33 additions & 0 deletions packages/starlight/__tests__/remark-rehype/asides.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,39 @@ Some text
});
});

describe('custom labels with nested markdown', () => {
test.each(['note', 'tip', 'caution', 'danger'])('%s with custom code label', async (type) => {
const label = 'Custom `code` Label';
const labelWithoutMarkdown = 'Custom code Label';
const labelHtml = 'Custom <code>code</code> Label';
const res = await processor.render(`
:::${type}[${label}]
Some text
:::
`);
expect(res.code).includes(`aria-label="${labelWithoutMarkdown}"`);
expect(res.code).includes(`</svg>${labelHtml}</p>`);
});
});

describe('custom labels with doubly-nested markdown', () => {
test.each(['note', 'tip', 'caution', 'danger'])(
'%s with custom doubly-nested label',
async (type) => {
const label = 'Custom **strong with _emphasis_** Label';
const labelWithoutMarkdown = 'Custom strong with emphasis Label';
const labelHtml = 'Custom <strong>strong with <em>emphasis</em></strong> Label';
const res = await processor.render(`
:::${type}[${label}]
Some text
:::
`);
expect(res.code).includes(`aria-label="${labelWithoutMarkdown}"`);
expect(res.code).includes(`</svg>${labelHtml}</p>`);
}
);
});

test('ignores unknown directive variants', async () => {
const res = await processor.render(`
:::unknown
Expand Down
15 changes: 8 additions & 7 deletions packages/starlight/integrations/asides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import type { AstroConfig, AstroIntegration, AstroUserConfig } from 'astro';
import { h as _h, s as _s, type Properties } from 'hastscript';
import type { Node, Paragraph as P, Parent, Root } from 'mdast';
import type { Node, Paragraph as P, Parent, PhrasingContent, Root } from 'mdast';
import {
type Directives,
directiveToMarkdown,
type TextDirective,
type LeafDirective,
} from 'mdast-util-directive';
import { toMarkdown } from 'mdast-util-to-markdown';
import { toString } from 'mdast-util-to-string';
import remarkDirective from 'remark-directive';
import type { Plugin, Transformer } from 'unified';
import { visit } from 'unist-util-visit';
Expand Down Expand Up @@ -156,16 +157,16 @@ function remarkAsides(options: AsidesOptions): Plugin<[], Root> {
// prop to <Aside>, so when we find a directive label, we store it for the title prop and
// remove the paragraph from the container’s children.
let title = t(`aside.${variant}`);
let titleNode: PhrasingContent[] = [{ type: 'text', value: title }];
const firstChild = node.children[0];
if (
firstChild?.type === 'paragraph' &&
firstChild.data &&
'directiveLabel' in firstChild.data
'directiveLabel' in firstChild.data &&
firstChild.children.length > 0
) {
const firstGrandChild = firstChild.children[0];
if (firstGrandChild?.type === 'text') {
title = firstGrandChild.value;
}
titleNode = firstChild.children;
title = toString(firstChild.children);
// The first paragraph contains a directive label, we can safely remove it.
node.children.splice(0, 1);
}
Expand All @@ -189,7 +190,7 @@ function remarkAsides(options: AsidesOptions): Plugin<[], Root> {
},
iconPaths[variant]
),
{ type: 'text', value: title },
...titleNode,
]),
h('section', { class: 'starlight-aside__content' }, node.children),
]
Expand Down
1 change: 1 addition & 0 deletions packages/starlight/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@
"hastscript": "^9.0.0",
"mdast-util-directive": "^3.0.0",
"mdast-util-to-markdown": "^2.1.0",
"mdast-util-to-string": "^4.0.0",
"pagefind": "^1.0.3",
"rehype": "^13.0.1",
"rehype-format": "^5.0.0",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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

Loading