Skip to content

Commit 131d9b8

Browse files
committed
Support <Code> component in lists
1 parent 39e6906 commit 131d9b8

File tree

1 file changed

+51
-20
lines changed

1 file changed

+51
-20
lines changed
Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
2-
import { Code } from '@astrojs/starlight/components';
3-
import { marked } from 'marked';
2+
import { Code } from "@astrojs/starlight/components";
3+
import { marked } from "marked";
4+
import type { Token } from "marked";
45
56
interface Props {
67
content?: string;
@@ -9,9 +10,9 @@ interface Props {
910
1011
const { content, inline = false } = Astro.props;
1112
12-
let slotMarkdown = '';
13-
if (!content && Astro.slots.has('default')) {
14-
slotMarkdown = await Astro.slots.render('default');
13+
let slotMarkdown = "";
14+
if (!content && Astro.slots.has("default")) {
15+
slotMarkdown = await Astro.slots.render("default");
1516
}
1617
1718
const rawContent = content ?? slotMarkdown ?? "";
@@ -21,16 +22,16 @@ const rawContent = content ?? slotMarkdown ?? "";
2122
2223
function convertMediaWikiLinks(text: string): string {
2324
const redirects: Record<string, string> = {
24-
ACL: 'acl',
25+
ACL: "acl",
2526
};
2627
2728
return text.replace(
2829
/\[\[([^|\]#]+)(?:#([^\]]+))?(?:\|([^\]]+))?\]\]/g,
2930
(_, link, hash, text) => {
3031
const redirected = redirects[link] ?? link;
31-
const url = `/reference/${redirected}${hash ? `#${hash}` : ''}`;
32+
const url = `/reference/${redirected}${hash ? `#${hash}` : ""}`;
3233
return `[${text || link}](${url})`;
33-
}
34+
},
3435
);
3536
}
3637
@@ -39,15 +40,45 @@ const tokens = inline ? marked.Lexer.lexInline(processed) : marked.lexer(process
3940
---
4041

4142
{
42-
tokens.map((token) => {
43-
if (token.type === 'code') {
44-
return <Code code={token.text} lang={token.lang || 'text'} />;
45-
}
46-
47-
if (inline) {
48-
return <Fragment set:html={marked.parseInline(token.raw ?? token.text ?? '')} />;
49-
}
50-
51-
return <Fragment set:html={marked.parser([token])} />;
52-
})
53-
}
43+
(function processTokens(tokens: Token[]) {
44+
return tokens.map((token) => {
45+
if (token.type === "code") {
46+
return <Code code={token.text} lang={token.lang || "text"} />;
47+
}
48+
49+
if (!inline) {
50+
if (token.type === "list") {
51+
const listToken = token as any;
52+
const Tag = listToken.ordered ? "ol" : "ul";
53+
54+
return (
55+
<Tag>
56+
{listToken.items.map((item: Token) => processTokens([item])[0])}
57+
</Tag>
58+
);
59+
}
60+
61+
if (token.type === "list_item") {
62+
const listItemToken = token as any;
63+
64+
if (listItemToken.tokens && listItemToken.tokens.length > 0) {
65+
const content = processTokens(listItemToken.tokens);
66+
return <li>{content}</li>;
67+
}
68+
69+
return <li>{listItemToken.text}</li>;
70+
}
71+
}
72+
73+
if (inline) {
74+
return (
75+
<Fragment
76+
set:html={marked.parseInline(token.raw ?? token.text ?? "")}
77+
/>
78+
);
79+
}
80+
81+
return <Fragment set:html={marked.parser([token])} />;
82+
});
83+
})(tokens)
84+
}

0 commit comments

Comments
 (0)