Repository navigation
feat(tutorials): copy-pastable CopyBlock and CodeGroup for tutorial pages #210
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
Merged
ccf-factory-reviewer
merged 5 commits into
main
from
factory/stack/wi_2fa6b8bf808100304f9a/1
Nov 28, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2a95116
feat(tutorials): add CopyBlock and CodeGroup for tutorial pages
ccf-factory[bot] 0954500
test(tutorials): cover the snippet text helper of CopyBlock
ccf-factory[bot] 80980a9
fix(tutorials): drop leading blank lines from a snippet
ccf-factory[bot] 31d2176
fix: address review feedback
ccf-factory[bot] 9d1ce79
test(tutorials): keep snippet fixtures to real CLI commands and config
ccf-factory[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| // The files of one tutorial step, shown as tabs so the page stays short: the reader opens the file | ||
| // they need and copies it. Each tab shows one CopyBlock, so every file keeps its own copy button. | ||
| // src/theme/MDXComponents/index.tsx registers it as <CodeGroup>, so tutorial pages use it without | ||
| // an import. | ||
| import React, {type KeyboardEvent, type ReactNode, useId, useRef, useState} from 'react'; | ||
| import clsx from 'clsx'; | ||
| import CopyBlock from './CopyBlock'; | ||
| import {clampTab, initialTab, nextTab} from './codeGroupTabs.mjs'; | ||
|
|
||
| export type CodeGroupItem = { | ||
| /** The tab's label: the file the snippet belongs to, such as `compliance.yaml`. */ | ||
| title: string; | ||
| /** The file's text: what the tab shows, and what its copy button copies. */ | ||
| code: string; | ||
| /** Prism language to highlight the file with, such as `sh`, `yaml` or `json`. */ | ||
| language?: string; | ||
| }; | ||
|
|
||
| export type CodeGroupProps = { | ||
| /** The files, in tab order. */ | ||
| items: CodeGroupItem[]; | ||
| /** Label of the file to open first; the first file when it matches none. */ | ||
| defaultTab?: string; | ||
| /** Classes added to the group. */ | ||
| className?: string; | ||
| }; | ||
|
|
||
| export default function CodeGroup({items, defaultTab, className}: CodeGroupProps): ReactNode { | ||
| const group = useId(); | ||
| const tabs = useRef<(HTMLButtonElement | null)[]>([]); | ||
| const [selected, setSelected] = useState(() => initialTab(items, defaultTab)); | ||
| if (items.length === 0) { | ||
| return null; | ||
| } | ||
| const active = clampTab(selected, items.length); | ||
|
|
||
| // The tablist moves the selection with the arrow keys, and to either end with Home and End; the | ||
| // decision of where a key goes lives in codeGroupTabs.mjs, where `yarn test` covers it. | ||
| function onSelectKey(event: KeyboardEvent<HTMLDivElement>) { | ||
| const moved = nextTab(active, event.key, items.length); | ||
| if (moved === undefined) { | ||
| return; | ||
| } | ||
| event.preventDefault(); | ||
| setSelected(moved); | ||
| tabs.current[moved]?.focus(); | ||
| } | ||
|
|
||
| return ( | ||
| <div | ||
| className={clsx( | ||
| 'ccf-code-group my-4 overflow-hidden rounded-[var(--ifm-code-border-radius)] border', | ||
| 'border-[var(--ifm-color-emphasis-300)] bg-[var(--ifm-background-surface-color)]', | ||
| 'shadow-[shadow:var(--ifm-global-shadow-lw)]', | ||
| className, | ||
| )} | ||
| > | ||
| <div | ||
| role="tablist" | ||
| aria-label={items.map((item) => item.title).join(', ')} | ||
| onKeyDown={onSelectKey} | ||
| className="flex flex-wrap gap-1 border-b border-[var(--ifm-color-emphasis-300)] bg-[var(--ifm-color-emphasis-100)] px-2 pt-2" | ||
| > | ||
| {items.map((item, index) => { | ||
| const isSelected = index === active; | ||
| return ( | ||
| <button | ||
| key={`${item.title}:${index}`} | ||
| ref={(tab) => { | ||
| tabs.current[index] = tab; | ||
| }} | ||
| type="button" | ||
| role="tab" | ||
| id={`${group}-tab-${index}`} | ||
| aria-selected={isSelected} | ||
| aria-controls={`${group}-panel`} | ||
| tabIndex={isSelected ? 0 : -1} | ||
| onClick={() => setSelected(index)} | ||
| className={clsx( | ||
| 'rounded-t-[var(--ifm-global-radius)] border border-b-0 px-3 py-2 font-mono leading-tight', | ||
| 'text-[length:var(--ifm-code-font-size)]', | ||
| isSelected | ||
| ? 'border-[var(--ifm-color-emphasis-300)] bg-[var(--ifm-background-surface-color)] font-semibold' | ||
| : 'border-transparent bg-transparent text-[var(--ifm-font-color-secondary)]', | ||
| 'hover:text-[var(--ifm-font-color-base)]', | ||
| )} | ||
| > | ||
| {item.title} | ||
| </button> | ||
| ); | ||
| })} | ||
| </div> | ||
| <div id={`${group}-panel`} role="tabpanel" aria-labelledby={`${group}-tab-${active}`}> | ||
| {/* No title: the tab above the file already names it. */} | ||
| <CopyBlock code={items[active].code} language={items[active].language} /> | ||
| </div> | ||
| </div> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| // One copy-pastable snippet: commands, config or file contents, shown as a code block the reader | ||
| // can copy in one click. It is Docusaurus's own code block (so it keeps the site's prism | ||
| // github/dracula highlighting and its copy button, made always visible in src/css/custom.css) with | ||
| // a title row and text that copies exactly as it is shown. src/theme/MDXComponents/index.tsx | ||
| // registers it as <CopyBlock>, so tutorial pages use it without an import. | ||
| import React, {type ReactNode} from 'react'; | ||
| import CodeBlock from '@theme/CodeBlock'; | ||
| import clsx from 'clsx'; | ||
| import {snippetText} from './snippetText.mjs'; | ||
|
|
||
| export type CopyBlockProps = { | ||
| /** The snippet. Give this or a plain-text child, not both. */ | ||
| code?: string; | ||
| /** The snippet as text children, e.g. a template literal that spans several lines. */ | ||
| children?: string | Array<string | number>; | ||
| /** Prism language to highlight the snippet with, such as `sh`, `yaml` or `json`. */ | ||
| language?: string; | ||
| /** Label above the snippet: the file it goes in, or what running it does. */ | ||
| title?: string; | ||
| /** Classes added to the block. */ | ||
| className?: string; | ||
| }; | ||
|
|
||
| export default function CopyBlock({code, children, language, title, className}: CopyBlockProps): ReactNode { | ||
| return ( | ||
| <CodeBlock language={language} title={title} className={clsx('ccf-copy-block', className)}> | ||
| {snippetText(code, children)} | ||
| </CodeBlock> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| // The tab selection decisions of CodeGroup. Plain ESM with JSDoc types, like snippetText.mjs, so | ||
| // `yarn test` can cover the lookup, the clamping and the keyboard wrap-around without a browser. | ||
|
|
||
| /** | ||
| * The tab to open first: the index of the item titled defaultTab, or 0 when it matches none, so | ||
| * the group opens the first file. | ||
| * | ||
| * @param {{title: string}[]} items The files, in tab order. | ||
| * @param {string | undefined} defaultTab The label of the file to open first. | ||
| * @returns {number} | ||
| */ | ||
| export function initialTab(items, defaultTab) { | ||
| return Math.max(0, items.findIndex((item) => item.title === defaultTab)); | ||
| } | ||
|
|
||
| /** | ||
| * The tab the selected index must show, clamped to the last item so items shrinking below the | ||
| * selected index, which only happens in a hot reload, cannot crash the group. | ||
| * | ||
| * @param {number} selected The index selected with clicks or the keyboard. | ||
| * @param {number} count The number of items. | ||
| * @returns {number} | ||
| */ | ||
| export function clampTab(selected, count) { | ||
| return Math.min(selected, count - 1); | ||
| } | ||
|
|
||
| /** | ||
| * The tab a key on the tablist moves to, wrapping around at both ends, or undefined for a key the | ||
| * tablist does not handle. | ||
| * | ||
| * @param {number} active The tab currently shown. | ||
| * @param {string} key The key pressed. | ||
| * @param {number} count The number of tabs. | ||
| * @returns {number | undefined} | ||
| */ | ||
| export function nextTab(active, key, count) { | ||
| switch (key) { | ||
| case 'ArrowRight': | ||
| return (active + 1) % count; | ||
| case 'ArrowLeft': | ||
| return (active - 1 + count) % count; | ||
| case 'Home': | ||
| return 0; | ||
| case 'End': | ||
| return count - 1; | ||
| default: | ||
| return undefined; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // node --test src/components/tutorials/codeGroupTabs.test.mjs (or `yarn test`) | ||
| import assert from 'node:assert/strict'; | ||
| import test from 'node:test'; | ||
| import {clampTab, initialTab, nextTab} from './codeGroupTabs.mjs'; | ||
|
|
||
| const files = [{title: 'compliance.yaml'}, {title: 'filter.yaml'}, {title: 'run.sh'}]; | ||
|
|
||
| test('defaultTab opens that file, by title', () => { | ||
| assert.equal(initialTab(files, 'filter.yaml'), 1); | ||
| assert.equal(initialTab(files, 'run.sh'), 2); | ||
| }); | ||
|
|
||
| test('a defaultTab that matches no title, or is absent, opens the first file', () => { | ||
| assert.equal(initialTab(files, 'missing.yaml'), 0); | ||
| assert.equal(initialTab(files, undefined), 0); | ||
| assert.equal(initialTab([], 'compliance.yaml'), 0); | ||
| }); | ||
|
|
||
| test('the first title wins when two files share it', () => { | ||
| assert.equal(initialTab([...files, {title: 'run.sh'}], 'run.sh'), 2); | ||
| }); | ||
|
|
||
| test('selection stays put while it is within the items and clamps when they shrink below it', () => { | ||
| assert.equal(clampTab(1, 3), 1); | ||
| assert.equal(clampTab(2, 3), 2); | ||
| assert.equal(clampTab(4, 3), 2); | ||
| }); | ||
|
|
||
| test('ArrowRight advances and wraps from the last tab to the first', () => { | ||
| assert.equal(nextTab(0, 'ArrowRight', 3), 1); | ||
| assert.equal(nextTab(2, 'ArrowRight', 3), 0); | ||
| }); | ||
|
|
||
| test('ArrowLeft steps back and wraps from the first tab to the last', () => { | ||
| assert.equal(nextTab(1, 'ArrowLeft', 3), 0); | ||
| assert.equal(nextTab(0, 'ArrowLeft', 3), 2); | ||
| }); | ||
|
|
||
| test('Home and End jump to either end', () => { | ||
| assert.equal(nextTab(1, 'Home', 3), 0); | ||
| assert.equal(nextTab(1, 'End', 3), 2); | ||
| assert.equal(nextTab(0, 'End', 1), 0); | ||
| }); | ||
|
|
||
| test('a key the tablist does not handle moves nothing', () => { | ||
| assert.equal(nextTab(1, 'Enter', 3), undefined); | ||
| assert.equal(nextTab(1, 'Tab', 3), undefined); | ||
| assert.equal(nextTab(1, 'ArrowDown', 3), undefined); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| // The text a tutorial block shows and copies, shared by CopyBlock and CodeGroup. Plain ESM with | ||
| // JSDoc types, like the scripts/ the site builds from, so `yarn test` can cover the trimming and | ||
| // dedent it does without a browser. | ||
|
|
||
| /** | ||
| * The text of a snippet: the code, or the children joined. Blank lines at the ends are dropped and | ||
| * the indentation every line shares is removed, so a snippet written in MDX as an indented template | ||
| * literal reads the way its author wrote it, and copies exactly what the reader sees. | ||
| * | ||
| * @param {string | undefined} code The snippet, when it is given as the code prop. | ||
| * @param {string | Array<string | number> | undefined} children The snippet, when it is given as | ||
| * text children. | ||
| * @returns {string} | ||
| */ | ||
| export function snippetText(code, children) { | ||
| const raw = code ?? (Array.isArray(children) ? children.join('') : (children ?? '')); | ||
| const lines = raw.replace(/^(\s*\n)+/, '').replace(/\s+$/, '').split('\n'); | ||
| const indents = lines.filter((line) => line.trim() !== '').map((line) => /^[ \t]*/.exec(line)[0].length); | ||
| const indent = indents.length > 0 ? Math.min(...indents) : 0; | ||
| return lines.map((line) => line.slice(indent)).join('\n'); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| // node --test src/components/tutorials/snippetText.test.mjs (or `yarn test`) | ||
| import assert from 'node:assert/strict'; | ||
| import test from 'node:test'; | ||
| import {snippetText} from './snippetText.mjs'; | ||
|
|
||
| test('the code prop is the text, minus the blank lines at its ends', () => { | ||
| assert.equal(snippetText('kubectl get pods\n', undefined), 'kubectl get pods'); | ||
| assert.equal(snippetText('\n\napi dashboards export f.json\n\n', undefined), 'api dashboards export f.json'); | ||
| assert.equal(snippetText(undefined, ' \n plugins:\n k8s:'), 'plugins:\n k8s:'); | ||
| }); | ||
|
|
||
| test('children are joined, so a snippet can be split across expressions', () => { | ||
| assert.equal(snippetText(undefined, ['name: k8s\n', 'version: 1\n']), 'name: k8s\nversion: 1'); | ||
| assert.equal(snippetText(undefined, ['port: ', 8080]), 'port: 8080'); | ||
| }); | ||
|
|
||
| test('the indentation every line shares is dropped and the rest kept, as an indented MDX literal writes it', () => { | ||
| const drafted = '\n plugins:\n k8s:\n enabled: true\n '; | ||
| assert.equal(snippetText(undefined, drafted), 'plugins:\n k8s:\n enabled: true'); | ||
| }); | ||
|
|
||
| test('a snippet that already starts at column zero is left alone', () => { | ||
| assert.equal(snippetText('plugins:\n k8s:', undefined), 'plugins:\n k8s:'); | ||
| }); | ||
|
|
||
| test('blank lines and shorter-indented lines inside the snippet are kept', () => { | ||
| assert.equal(snippetText('a:\n\n b: 1', undefined), 'a:\n\n b: 1'); | ||
| }); | ||
|
|
||
| test('tabs count as indentation', () => { | ||
| assert.equal(snippetText('\t\tapi digest test\n\t\t --dry-run', undefined), 'api digest test\n --dry-run'); | ||
| }); | ||
|
|
||
| test('code wins when the author gives both it and children', () => { | ||
| assert.equal(snippetText('from code', ['from children']), 'from code'); | ||
| }); | ||
|
|
||
| test('no snippet gives no text', () => { | ||
| assert.equal(snippetText(undefined, undefined), ''); | ||
| assert.equal(snippetText(undefined, []), ''); | ||
| assert.equal(snippetText(' \n \n', undefined), ''); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| // Registers the tutorial components on every page's MDX scope, so a tutorial page writes | ||
| // <CopyBlock> and <CodeGroup> without importing them. Docusaurus reads the MDX component scope | ||
| // from this module, and the wrapper keeps the theme's own mappings (code, a, ul, admonition...). | ||
| import MDXComponents from '@theme-original/MDXComponents'; | ||
| import CopyBlock from '@site/src/components/tutorials/CopyBlock'; | ||
| import CodeGroup from '@site/src/components/tutorials/CodeGroup'; | ||
|
|
||
| export default { | ||
| ...MDXComponents, | ||
| CopyBlock, | ||
| CodeGroup, | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.