starlight-heading-badges 0.4.1 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +22 -0
- package/components/HeadingBadgesMobileTableOfContents.astro +13 -11
- package/components/HeadingBadgesTableOfContents.astro +1 -3
- package/components/TableOfContentHeading.astro +7 -5
- package/index.ts +1 -1
- package/libs/badge.ts +18 -7
- package/libs/plugin.ts +2 -2
- package/libs/rehype.ts +25 -17
- package/overrides/MobileTableOfContents.astro +1 -3
- package/overrides/TableOfContents.astro +1 -3
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
# starlight-heading-badges
|
|
2
2
|
|
|
3
|
+
## 0.6.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#18](https://github.com/HiDeoo/starlight-heading-badges/pull/18) [`261fda4`](https://github.com/HiDeoo/starlight-heading-badges/commit/261fda465d560146c55a2f148f36a28de384fdc8) Thanks [@HiDeoo](https://github.com/HiDeoo)! - Adds support for rendering multiple badges to the same heading.
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- [#16](https://github.com/HiDeoo/starlight-heading-badges/pull/16) [`3a590c7`](https://github.com/HiDeoo/starlight-heading-badges/commit/3a590c7332c0bdaacd6109b5597b84cd5a415daf) Thanks [@HiDeoo](https://github.com/HiDeoo)! - Fixes an issue for heading badges with multiple spaces.
|
|
12
|
+
|
|
13
|
+
## 0.5.0
|
|
14
|
+
|
|
15
|
+
### Minor Changes
|
|
16
|
+
|
|
17
|
+
- [#12](https://github.com/HiDeoo/starlight-heading-badges/pull/12) [`508982f`](https://github.com/HiDeoo/starlight-heading-badges/commit/508982fc5050e700669382f195aaa420a4b59748) Thanks [@HiDeoo](https://github.com/HiDeoo)! - ⚠️ **BREAKING CHANGE:** The minimum supported version of Starlight is now version `0.32.0`.
|
|
18
|
+
|
|
19
|
+
Please use the `@astrojs/upgrade` command to upgrade your project:
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
npx @astrojs/upgrade
|
|
23
|
+
```
|
|
24
|
+
|
|
3
25
|
## 0.4.1
|
|
4
26
|
|
|
5
27
|
### Patch Changes
|
|
@@ -5,11 +5,10 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import { Icon } from '@astrojs/starlight/components'
|
|
8
|
-
import type { Props } from '@astrojs/starlight/props'
|
|
9
8
|
|
|
10
9
|
import TableOfContentsList from './TableOfContentsList.astro'
|
|
11
10
|
|
|
12
|
-
const { toc } = Astro.
|
|
11
|
+
const { toc } = Astro.locals.starlightRoute
|
|
13
12
|
---
|
|
14
13
|
|
|
15
14
|
{
|
|
@@ -114,7 +113,7 @@ const { toc } = Astro.props
|
|
|
114
113
|
</style>
|
|
115
114
|
|
|
116
115
|
<script>
|
|
117
|
-
import {
|
|
116
|
+
import { deserializeBadges } from '../libs/badge'
|
|
118
117
|
import { StarlightTOC } from '../libs/starlight-toc'
|
|
119
118
|
|
|
120
119
|
class MobileStarlightTOC extends StarlightTOC {
|
|
@@ -124,15 +123,18 @@ const { toc } = Astro.props
|
|
|
124
123
|
if (display) {
|
|
125
124
|
const heading = link.dataset['shbHeading']
|
|
126
125
|
if (heading) {
|
|
127
|
-
const
|
|
128
|
-
if (
|
|
126
|
+
const badges = deserializeBadges(heading)
|
|
127
|
+
if (badges.length > 0) {
|
|
129
128
|
display.textContent = ''
|
|
130
|
-
display.append(document.createTextNode(`${
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
129
|
+
display.append(document.createTextNode(`${badges[0]?.heading} `))
|
|
130
|
+
|
|
131
|
+
for (const badge of badges) {
|
|
132
|
+
const span = document.createElement('span')
|
|
133
|
+
span.textContent = badge.text
|
|
134
|
+
span.dataset['shbBadge'] = ''
|
|
135
|
+
span.dataset['shbBadgeVariant'] = badge.variant
|
|
136
|
+
display.append(span)
|
|
137
|
+
}
|
|
136
138
|
|
|
137
139
|
return
|
|
138
140
|
}
|
|
@@ -4,11 +4,9 @@
|
|
|
4
4
|
* @see https://github.com/withastro/starlight/blob/1d32c8b0cec0ea5f66351b21b91748dfa78b404b/packages/starlight/components/TableOfContents.astro
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import type { Props } from '@astrojs/starlight/props'
|
|
8
|
-
|
|
9
7
|
import TableOfContentsList from './TableOfContentsList.astro'
|
|
10
8
|
|
|
11
|
-
const { toc } = Astro.
|
|
9
|
+
const { toc } = Astro.locals.starlightRoute
|
|
12
10
|
---
|
|
13
11
|
|
|
14
12
|
{
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
import { Badge } from '@astrojs/starlight/components'
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import { deserializeBadges } from '../libs/badge'
|
|
5
5
|
|
|
6
6
|
interface Props {
|
|
7
7
|
text: string
|
|
@@ -9,14 +9,16 @@ interface Props {
|
|
|
9
9
|
|
|
10
10
|
const { text } = Astro.props
|
|
11
11
|
|
|
12
|
-
const
|
|
12
|
+
const badges = deserializeBadges(text)
|
|
13
13
|
---
|
|
14
14
|
|
|
15
15
|
{
|
|
16
|
-
|
|
16
|
+
badges.length > 0 ? (
|
|
17
17
|
<span class="shb-heading">
|
|
18
|
-
{
|
|
19
|
-
|
|
18
|
+
{badges[0]?.heading.trim()}
|
|
19
|
+
{badges.map((badge) => (
|
|
20
|
+
<Badge size="small" text={badge.text} variant={badge.variant} />
|
|
21
|
+
))}
|
|
20
22
|
</span>
|
|
21
23
|
) : (
|
|
22
24
|
<span>{text}</span>
|
package/index.ts
CHANGED
|
@@ -7,7 +7,7 @@ export default function starlightHeadingBadgesPlugin(): StarlightPlugin {
|
|
|
7
7
|
return {
|
|
8
8
|
name: 'starlight-heading-badges-plugin',
|
|
9
9
|
hooks: {
|
|
10
|
-
setup({ addIntegration, config: starlightConfig, logger, updateConfig }) {
|
|
10
|
+
'config:setup'({ addIntegration, config: starlightConfig, logger, updateConfig }) {
|
|
11
11
|
addIntegration(starlightHeadingBadgesIntegration())
|
|
12
12
|
|
|
13
13
|
updateConfig({
|
package/libs/badge.ts
CHANGED
|
@@ -14,23 +14,34 @@ export function serializeBadge(variant: Variant, text: string) {
|
|
|
14
14
|
serializedBadgeDelimiter,
|
|
15
15
|
variant,
|
|
16
16
|
serializedBadgeDelimiter,
|
|
17
|
-
text.
|
|
17
|
+
text.replaceAll(' ', serializedBadgeSpaceDelimiter),
|
|
18
18
|
serializedBadgeDelimiter,
|
|
19
19
|
].join('')
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
export function
|
|
23
|
-
const
|
|
24
|
-
if (!serializeBadge) return
|
|
22
|
+
export function deserializeBadges(value: string): Badge[] {
|
|
23
|
+
const badges: Badge[] = []
|
|
25
24
|
|
|
26
|
-
const parts =
|
|
25
|
+
const parts = value.split(' ').reverse()
|
|
26
|
+
|
|
27
|
+
for (const part of parts) {
|
|
28
|
+
const badge = deserializeBadge(value, part)
|
|
29
|
+
if (!badge) break
|
|
30
|
+
badges.unshift(badge)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return badges
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function deserializeBadge(heading: string, value: string): Badge | undefined {
|
|
37
|
+
const parts = value.split(serializedBadgeDelimiter)
|
|
27
38
|
const [, variant, text] = parts
|
|
28
39
|
|
|
29
40
|
if (!variant || !isBadgeVariant(variant) || !text) return undefined
|
|
30
41
|
|
|
31
42
|
return {
|
|
32
|
-
heading:
|
|
33
|
-
text: text.
|
|
43
|
+
heading: heading.replace(new RegExp(`${serializedBadgeDelimiter}.*${serializedBadgeDelimiter}`), ''),
|
|
44
|
+
text: text.replaceAll(serializedBadgeSpaceDelimiter, ' '),
|
|
34
45
|
variant,
|
|
35
46
|
}
|
|
36
47
|
}
|
package/libs/plugin.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { HookParameters } from '@astrojs/starlight/types'
|
|
2
2
|
import type { AstroIntegrationLogger } from 'astro'
|
|
3
3
|
|
|
4
4
|
export function overrideComponents(
|
|
@@ -27,4 +27,4 @@ interface ComponentOverride {
|
|
|
27
27
|
fallback: string
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
-
type StarlightUserConfig =
|
|
30
|
+
type StarlightUserConfig = HookParameters<'config:setup'>['config']
|
package/libs/rehype.ts
CHANGED
|
@@ -3,7 +3,7 @@ import 'mdast-util-directive'
|
|
|
3
3
|
import type { ElementContent, Root } from 'hast'
|
|
4
4
|
import { CONTINUE, SKIP, visit } from 'unist-util-visit'
|
|
5
5
|
|
|
6
|
-
import {
|
|
6
|
+
import { deserializeBadges, type Badge } from './badge'
|
|
7
7
|
|
|
8
8
|
export function rehypeStarlightHeadingBadges() {
|
|
9
9
|
return function transformer(tree: Root) {
|
|
@@ -11,14 +11,16 @@ export function rehypeStarlightHeadingBadges() {
|
|
|
11
11
|
if (node.type === 'text') {
|
|
12
12
|
if (index === undefined || !parent) return CONTINUE
|
|
13
13
|
|
|
14
|
-
const
|
|
15
|
-
if (
|
|
14
|
+
const badges = deserializeBadges(node.value)
|
|
15
|
+
if (badges.length === 0) return SKIP
|
|
16
16
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
17
|
+
for (const badge of badges.reverse()) {
|
|
18
|
+
if (badge.heading) {
|
|
19
|
+
node.value = badge.heading
|
|
20
|
+
parent.children.splice(index + 1, 0, ...createBadgeNode(badge))
|
|
21
|
+
} else {
|
|
22
|
+
parent.children.splice(index, 1, ...createBadgeNode(badge))
|
|
23
|
+
}
|
|
22
24
|
}
|
|
23
25
|
|
|
24
26
|
return SKIP
|
|
@@ -29,14 +31,20 @@ export function rehypeStarlightHeadingBadges() {
|
|
|
29
31
|
}
|
|
30
32
|
}
|
|
31
33
|
|
|
32
|
-
function createBadgeNode(badge: Badge): ElementContent {
|
|
33
|
-
return
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
34
|
+
function createBadgeNode(badge: Badge): ElementContent[] {
|
|
35
|
+
return [
|
|
36
|
+
{
|
|
37
|
+
type: 'element',
|
|
38
|
+
tagName: 'span',
|
|
39
|
+
properties: {
|
|
40
|
+
'data-shb-badge': '',
|
|
41
|
+
'data-shb-badge-variant': badge.variant,
|
|
42
|
+
},
|
|
43
|
+
children: [{ type: 'text', value: badge.text }],
|
|
39
44
|
},
|
|
40
|
-
|
|
41
|
-
|
|
45
|
+
{
|
|
46
|
+
type: 'text',
|
|
47
|
+
value: ' ',
|
|
48
|
+
},
|
|
49
|
+
]
|
|
42
50
|
}
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
---
|
|
2
|
-
import type { Props } from '@astrojs/starlight/props'
|
|
3
|
-
|
|
4
2
|
import HeadingBadgesMobileTableOfContents from '../components/HeadingBadgesMobileTableOfContents.astro'
|
|
5
3
|
---
|
|
6
4
|
|
|
7
|
-
<HeadingBadgesMobileTableOfContents
|
|
5
|
+
<HeadingBadgesMobileTableOfContents />
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "starlight-heading-badges",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Starlight plugin to add badges to your Markdown and MDX headings.",
|
|
6
6
|
"author": "HiDeoo <github@hideoo.dev> (https://hideoo.dev)",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"@types/mdast": "^4.0.4"
|
|
27
27
|
},
|
|
28
28
|
"peerDependencies": {
|
|
29
|
-
"@astrojs/starlight": ">=0.
|
|
29
|
+
"@astrojs/starlight": ">=0.32.0"
|
|
30
30
|
},
|
|
31
31
|
"engines": {
|
|
32
32
|
"node": ">=18"
|