Skip to content

Commit 092d1cf

Browse files
louismaximepitonMaxLardenoishannahiss
authored
feat(docs): Add a shortcode to add icon to component types (#3508)
Co-authored-by: Maxime Lardenois <maxime.lardenois@orange.com> Co-authored-by: Hannah Issermann <hannah.issermann@orange.com>
1 parent 080ef2e commit 092d1cf

11 files changed

Lines changed: 65 additions & 21 deletions

File tree

site/src/components/TableOfContents.astro

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
---
22
import type { MarkdownHeading } from 'astro'
33
import { generateToc, type TocEntry } from '@libs/toc'
4+
import { getComponentSVG } from '@libs/utils'
45
56
interface Props {
67
headings?: MarkdownHeading[]
78
entries?: TocEntry[]
9+
types?: string[]
810
}
911
10-
const { entries, headings } = Astro.props
12+
const { entries, headings, types } = Astro.props
1113
12-
const toc = entries ? entries : generateToc(headings ?? [])
14+
const toc = entries ? entries : generateToc(headings ?? [], types)
1315
---
1416

1517
<ul>
1618
{
1719
toc.map(({ children, slug, text }) => {
20+
const isComponent = text.includes('[[comp]]')
1821
return (
1922
<li>
20-
<a href={`#${slug}`}>{text}</a>
23+
<a href={`#${slug}`} class:list={[{'d-flex align-items-center': isComponent}]}>{text.replace('[[comp]]', '')}{isComponent ? <Fragment set:html={getComponentSVG('bs-medium-icon ms-2xsmall')} /> : null}</a>
2124
{children.length > 0 && <Astro.self entries={children} />}
2225
</li>
2326
)

site/src/content/docs/components/alerts.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ In case an alert is dynamically added, there are several use cases:
123123
- The alert is informational and needs a user action but isn’t urgent, you can make it appear inside a `<div role="status">`. The dynamic alert message should reference where to find this action. [More information about the `role="status"`](https://a11y-guidelines.orange.com/en/articles/aria-status-messages/). [See a valid example](#live-example).
124124
- The alert requires a user action, either add the `role="alert"` directly on the `<div class="alert" role="alert">` and add a way to access to it easily, **or**, focus the newly added alert and once it’s closed, give the focus back to the element that triggered the alert. If the second choice is applied here, you may want to add a focustrap to this alert. [More information about the `role="alert"`](https://a11y-guidelines.orange.com/en/articles/aria-live-alert/).
125125

126-
## <svg class="hl-small-icon me-scaled-2xsmall mb-xsmall" width="1rem" height="1rem" aria-hidden="true"><use xlink:href={getVersionedDocsPath('assets/img/ouds-web-sprite.svg#component-atom')} /></svg>Alert message
126+
## [[comp]] Alert message
127127

128128
Alert message is a UI element that provides system feedback, indicates status changes, or prompts required actions through clear, prominent, persistent, and actionable communication.
129129

@@ -713,7 +713,7 @@ myAlert.addEventListener('closed.bs.alert', event => {
713713
})
714714
```
715715

716-
## <svg class="hl-small-icon me-scaled-2xsmall mb-xsmall" width="1rem" height="1rem" aria-hidden="true"><use xlink:href={getVersionedDocsPath('assets/img/ouds-web-sprite.svg#component-atom')} /></svg>Inline alert
716+
## [[comp]] Inline alert
717717

718718
Inline alert is a lightweight UI element integrated into the content flow. It conveys information, system feedback, and status changes through short, prominent, persistent, and non-actionable messages.
719719

site/src/content/docs/components/badges.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ Keep in mind that visually impaired and blind users will lack the visual context
8383

8484
Several examples are provided below, but they need to be adapted for actual use cases.
8585

86-
## <svg class="hl-small-icon me-scaled-2xsmall mb-xsmall" width="1rem" height="1rem" aria-hidden="true"><use xlink:href={getVersionedDocsPath('assets/img/ouds-web-sprite.svg#component-atom')} /></svg>Badge
86+
## [[comp]] Badge
8787

8888
Badge is a UI element that highlights system notifications, statuses, or the categorisation of information through colour alone.
8989

@@ -163,7 +163,7 @@ Badges are available in four sizes. Default size is medium.
163163
<p class="badge"><span class="visually-hidden">Medium badge (default)</span></p>
164164
<p class="badge badge-large"><span class="visually-hidden">Large badge</span></p>`} />
165165

166-
## <svg class="hl-small-icon me-scaled-2xsmall mb-xsmall" width="1rem" height="1rem" aria-hidden="true"><use xlink:href={getVersionedDocsPath('assets/img/ouds-web-sprite.svg#component-atom')} /></svg>Badge - Count
166+
## [[comp]] Badge - Count
167167

168168
Badge - count is a UI element that highlights system notifications, status, or the categorisation of information through colour and numerical value.
169169

@@ -215,7 +215,7 @@ Badges - count are available in two sizes. Add `.badge-large` to have a larger b
215215
<Example class="d-flex align-items-center gap-large" code={`<p class="badge badge-count">12<span class="visually-hidden">errors</span></p>
216216
<p class="badge badge-count badge-large">12<span class="visually-hidden">errors</span></p>`} />
217217

218-
## <svg class="hl-small-icon me-scaled-2xsmall mb-xsmall" width="1rem" height="1rem" aria-hidden="true"><use xlink:href={getVersionedDocsPath('assets/img/ouds-web-sprite.svg#component-atom')} /></svg>Badge - Icon
218+
## [[comp]] Badge - Icon
219219

220220
Badge - icon is a UI element that highlights system notifications, statuses, or the categorisation of information through colour and iconography.
221221

site/src/content/docs/components/buttons.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Generally speaking, you should always ask yourself which tag is appropriate for
6868

6969
When navigation buttons are used for global navigation (between pages or within content), they should be wrapped in a `<nav>` tag, which must have an explicit (accessible) name defined using, for example, an `aria-label` attribute.
7070

71-
## <svg class="hl-small-icon me-scaled-2xsmall mb-xsmall" width="1rem" height="1rem" aria-hidden="true"><use xlink:href={getVersionedDocsPath('assets/img/ouds-web-sprite.svg#component-atom')} /></svg>Button
71+
## [[comp]] Button
7272

7373
Button is a UI element that triggers an action or event, used to initiate tasks or confirm an action. Button comes in various layouts, styles, and states to convey hierarchy or emphasis.
7474

@@ -548,15 +548,15 @@ Loading state can also be combined with all kind of buttons, including icons.
548548
</div>
549549
`} />
550550

551-
### Skeleton
551+
#### Skeleton
552552

553553
<SkeletonRedirect component="button" />
554554

555555
<Example buttonLabel="button skeleton" code={`<div aria-busy="true" inert>
556556
<button type="button" class="btn btn-default">Default</button>
557557
</div>`} />
558558

559-
## Layout
559+
### Layout
560560

561561
#### Block buttons
562562

@@ -594,7 +594,7 @@ Additional utilities can be used to adjust the alignment of buttons when horizon
594594

595595
If you don’t want the button text to wrap, you can add the `.text-nowrap` class to the button. In Sass, you can set `$btn-white-space: nowrap` to disable text wrapping for each button.
596596

597-
## <svg class="hl-small-icon me-scaled-2xsmall mb-xsmall" width="1rem" height="1rem" aria-hidden="true"><use xlink:href={getVersionedDocsPath('assets/img/ouds-web-sprite.svg#component-atom')} /></svg>Navigation button
597+
## [[comp]] Navigation button
598598

599599
Navigation button is a UI element that enables movement between different pages within a multi-page interface. Navigation button is typically arranged in a sequence to indicate the user’s current position and provide controls for accessing previous, next, or specific pages.
600600

site/src/content/docs/components/chips.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ Here is a table summarizing the pseudo elements that are used by the different v
8484

8585
{/* | Filter expand chip | Used | Used | */}
8686

87-
## <svg class="hl-small-icon me-scaled-2xsmall mb-xsmall" width="1rem" height="1rem" aria-hidden="true"><use xlink:href={getVersionedDocsPath('assets/img/ouds-web-sprite.svg#component-atom')} /></svg>Filter chip
87+
## [[comp]] Filter chip
8888

8989
Filter chip is a UI element that enables users to select or deselect options within a series, commonly used to capture filtering decisions.
9090

@@ -309,7 +309,7 @@ document.querySelectorAll('.chip-suggestion').forEach(chipElement => {
309309
})
310310
```
311311

312-
## <svg class="hl-small-icon me-scaled-2xsmall mb-xsmall" width="1rem" height="1rem" aria-hidden="true"><use xlink:href={getVersionedDocsPath('assets/img/ouds-web-sprite.svg#component-atom')} /></svg>Suggestion chip
312+
## [[comp]] Suggestion chip
313313

314314
Suggestion chip is a UI element that presents recommended or predictive options based on the user's input or context, often utilised to capture filtering decisions.
315315

@@ -442,7 +442,7 @@ The following example shows every possible renderings depending on the suggestio
442442

443443
## Layout
444444

445-
This part explains global truth among all the chip variants.
445+
This layout section applies to both chip component types.
446446

447447
### Overflowing
448448

site/src/content/docs/components/tags.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ Keep in mind that visually impaired and blind users will lack the visual context
8787

8888
Several examples are provided below, but they need to be adapted for actual use cases.
8989

90-
## <svg class="hl-small-icon me-scaled-2xsmall mb-xsmall" width="1rem" height="1rem" aria-hidden="true"><use xlink:href={getVersionedDocsPath('assets/img/ouds-web-sprite.svg#component-atom')} /></svg>Tag
90+
## [[comp]] Tag
9191

9292
Tag is a UI element that shows brief information, such as a label, keyword, or category.
9393

@@ -423,7 +423,7 @@ Tags can be added side by side. They require horizontal and vertical spacing bet
423423
<li class="tag">Label 6</li>
424424
</ul>`} />
425425

426-
## <svg class="hl-small-icon me-scaled-2xsmall mb-xsmall" width="1rem" height="1rem" aria-hidden="true"><use xlink:href={getVersionedDocsPath('assets/img/ouds-web-sprite.svg#component-atom')} /></svg>Input tag
426+
## [[comp]] Input tag
427427

428428
Input tag is a UI element that renders an entered value as a tag, allowing it to display brief information like a label, keyword, or category.
429429

site/src/layouts/DocsLayout.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ const multipleComponents = componentsVersions.length > 1 ? 's' : ''
150150
<hr class="d-none d-md-block my-small ms-large" />
151151
<div class="collapse bd-toc-collapse ms-md-xsmall" id="tocContents">
152152
<nav id="TableOfContents" aria-labelledby="docs-toc">
153-
<TableOfContents headings={headings} />
153+
<TableOfContents headings={headings} types={frontmatter.types} />
154154
</nav>
155155
</div>
156156
)

site/src/libs/astro.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type { Element, Text } from 'hast'
99
import rehypeAutolinkHeadings from 'rehype-autolink-headings'
1010
import { getConfig } from './config'
1111
import { rehypeBsTable } from './rehype'
12-
import { remarkBsConfig, remarkBsDocsref } from './remark'
12+
import { remarkBsComp, remarkBsConfig, remarkBsDocsref } from './remark'
1313
import { configurePrism } from './prism'
1414
import {
1515
docsDirectory,
@@ -83,7 +83,7 @@ export function oudsWeb(): AstroIntegration[] {
8383
],
8484
rehypeBsTable
8585
],
86-
remarkPlugins: [remarkBsConfig, remarkBsDocsref]
86+
remarkPlugins: [remarkBsConfig, remarkBsDocsref, remarkBsComp]
8787
}
8888
})
8989
},

site/src/libs/remark.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ import type { Plugin } from 'unified'
44
import { visit } from 'unist-util-visit'
55
import { getConfig } from './config'
66
import { getVersionedDocsPath } from './path'
7+
import { getComponentSVG } from './utils'
78

89
// [[config:foo]]
910
// [[config:foo.bar]]
1011
const configRegExp = /\[\[config:(?<name>[\w\.]+)]]/g
1112
// [[docsref:/foo]]
1213
// [[docsref:/foo/bar#baz]]
1314
const docsrefRegExp = /\[\[docsref:(?<path>[\w\.\/#-]+)]]/g
15+
// [[comp]]
16+
const compRegExp = /\[\[comp\]\]\s*/
1417

1518
// A remark plugin to replace config values embedded in markdown (or MDX) files.
1619
// For example, [[config:foo]] will be replaced with the value of the `foo` key in the `config.yml` file.
@@ -86,6 +89,34 @@ export const remarkBsDocsref: Plugin<[], Root> = function () {
8689
}
8790
}
8891

92+
// A remark plugin to add an svg to component titles.
93+
export const remarkBsComp: Plugin<[], Root> = function () {
94+
return function remarkBsCompPlugin(ast) {
95+
// https://github.com/syntax-tree/mdast#nodes
96+
// https://github.com/syntax-tree/mdast-util-mdx-jsx#nodes
97+
visit(ast, ['text'], (node, index, parent) => {
98+
switch (node.type) {
99+
case 'text': {
100+
if (node.value.match(compRegExp)) {
101+
parent.children = [{
102+
type: 'html',
103+
value: getComponentSVG('hl-small-icon me-scaled-2xsmall mb-xsmall')
104+
}, ...parent.children]
105+
node.value = replaceCompInText(node.value)
106+
}
107+
break
108+
}
109+
}
110+
})
111+
}
112+
}
113+
114+
function replaceCompInText(text: string) {
115+
return text.replace(compRegExp, (_match, path) => {
116+
return ``
117+
})
118+
}
119+
89120
export function replaceConfigInText(text: string) {
90121
return text.replace(configRegExp, (_match, path) => {
91122
const value = getConfigValueAtPath(path)

site/src/libs/toc.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import type { MarkdownHeading } from 'astro'
22
import { getConfig } from './config'
33

44
// Generate a tree like structure from a list of headings.
5-
export function generateToc(allHeadings: MarkdownHeading[]) {
5+
export function generateToc(allHeadings: MarkdownHeading[], types?: string[]) {
66
const headings = allHeadings.filter(
77
(heading) => heading.depth >= getConfig().toc.min && heading.depth <= getConfig().toc.max
88
)
99

10+
const hasComponentTypes: boolean = !!headings.find((heading) => heading.slug === 'component-types')
1011
const toc: TocEntry[] = []
1112

1213
for (const heading of headings) {
@@ -15,6 +16,10 @@ export function generateToc(allHeadings: MarkdownHeading[]) {
1516
continue
1617
}
1718

19+
if (hasComponentTypes && heading.depth === 2 && types?.includes(heading.text)) {
20+
heading.text = `[[comp]] ${heading.text}`
21+
}
22+
1823
const previousEntry = toc[toc.length - 1]
1924

2025
if (heading.depth === previousEntry.depth) {

0 commit comments

Comments
 (0)